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()
{
}
}我正在尝试在列表视图的底部插入一个按钮,以转到另一个活动。是否可以在此列表视图中插入按钮?我需要的按钮来访问下一个活动,这将是一个包含选项的菜单。
发布于 2012-03-21 18:02:24
如果您想在Activity上添加一个额外的按钮,那么您必须替换这一行--
public class Database extends ListActivity 从这行开始--
public class Database extends Activity 你还得用这句话--
setContentView(R.layout.main);因为如果你想添加额外的控件,那么设置布局是必要的。
之后,只需在main.xml中--
<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中,还要添加这个--
ListView lv=(ListView)findViewById(R.id.list);
lv.setClickable(true);//设置按钮
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);
}
});然后把这一行--
setListAdapter(adapter);来自--
lv.setAdapter(adapter);我已经解决了你的problem....whereever,我告诉你的错误....做了相应的修改...
发布于 2012-03-21 13:54:26
在XML中:
<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" />在活动中:
Button button1 = (Button)findViewById(R.id.button1);
button1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
}
});https://stackoverflow.com/questions/9796211
复制相似问题