首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >从一个活动(数据库)转移到另一个活动

从一个活动(数据库)转移到另一个活动
EN

Stack Overflow用户
提问于 2012-03-21 06:49:30
回答 2查看 399关注 0票数 0
代码语言:javascript
运行
复制
import android.app.Activity;
import android.app.ListActivity;
import android.database.Cursor;
import android.os.Bundle;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;

public class Database extends ListActivity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.main);

        DatabaseHelper dbh = new DatabaseHelper(this);
        Cursor myCursor = dbh.getReadableDatabase()
                        .rawQuery("SELECT _id, " + DatabaseHelper.NAME +
                                  ", " + DatabaseHelper.VALUE + 
                                  " FROM " + DatabaseHelper.TABLE, null);
        String[] dataFrom = {DatabaseHelper.NAME, DatabaseHelper.VALUE};
        int[] dataTo = {R.id.name, R.id.value};

        SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, 
                R.layout.row, myCursor, dataFrom, dataTo);

        setListAdapter(adapter);

        /*TextView tv = new TextView(this);
        tv.setText("Hello, Android");
        setContentView(tv);*/



    }

    public void onListItemClick()
    {

    }



}

我正在尝试在列表视图的底部插入一个按钮,以转到另一个活动。是否可以在此列表视图中插入按钮?我需要的按钮来访问下一个活动,这将是一个包含选项的菜单。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-03-21 18:02:24

如果您想在Activity上添加一个额外的按钮,那么您必须替换这一行--

代码语言:javascript
运行
复制
 public class Database extends ListActivity 

从这行开始--

代码语言:javascript
运行
复制
 public class Database extends Activity 

你还得用这句话--

代码语言:javascript
运行
复制
setContentView(R.layout.main);

因为如果你想添加额外的控件,那么设置布局是必要的。

之后,只需在main.xml中--

代码语言:javascript
运行
复制
<Button android:id="@+id/button" android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:gravity="center"
        android:text="Next Activity" />

<ListView android:id="@android:id/list" android:layout_width="fill_parent"
            android:layout_height="fill_parent"/> 

在Activity use中,还要添加这个--

代码语言:javascript
运行
复制
ListView lv=(ListView)findViewById(R.id.list);
lv.setClickable(true);

//设置按钮

代码语言:javascript
运行
复制
Button button1 = (Button)findViewById(R.id.button1);
button1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent=new Intent(this,NextActivity.class);
    startActivity(intent);

    }
 });

然后把这一行--

代码语言:javascript
运行
复制
 setListAdapter(adapter);

来自--

代码语言:javascript
运行
复制
 lv.setAdapter(adapter);

我已经解决了你的problem....whereever,我告诉你的错误....做了相应的修改...

票数 1
EN

Stack Overflow用户

发布于 2012-03-21 13:54:26

在XML中:

代码语言:javascript
运行
复制
<ListView android:id="@android:id/list" android:layout_width="fill_parent"
                android:layout_height="fill_parent" 
                android:dividerHeight="1dip" 
                android:cacheColorHint="#00000000" />

<Button android:id="@+id/button1" android:layout_width="wrap_content"
            android:layout_height="wrap_content" android:gravity="center"
            android:text="Next" />

在活动中:

代码语言:javascript
运行
复制
Button button1 = (Button)findViewById(R.id.button1);
button1.setOnClickListener(new OnClickListener() {
    public void onClick(View v) {

    }
});
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9796211

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档