我有一个ListView
,我想先选择(突出显示)一个项目,然后在它进入另一个活动之前单击一个按钮。
这是我想做的一个例子:
这就是我制作 ListView 的方式(我从数据库中获取它的条目)
ListView lv = (ListView) findViewById(R.id.lvListOfCustomers);
String strDBName = "db_customers.s3db";
File fileDB = new File(
Environment
.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS),
strDBName);
SQLiteDatabase dbLibrary = SQLiteDatabase.openOrCreateDatabase(fileDB,
null);
// Start - Cursor and Queries of List of Customers(ListView)
String sqlQuery = "select _id, customer_name as cName, customer_address as cAddress, customer_status as cStatus from tbl_customers";
Cursor cur = (Cursor) dbLibrary.rawQuery(sqlQuery, null);
@SuppressWarnings("deprecation")
SimpleCursorAdapter sca = new SimpleCursorAdapter(
this.getApplicationContext(),
R.layout.lv_list_of_customers_txtview, cur, new String[] {
"cName", "cAddress", "cStatus" }, new int[] {
R.id.tvCustomersName, R.id.tvCustomersAddress,
R.id.tvCustomersId }, CursorAdapter.FLAG_AUTO_REQUERY);
lv.setAdapter(sca);
// End - Cursor and Queries of List of Customers(ListView)
// Start - Make the items highlighted
int selectedListItem = getIntent().getIntExtra("PositionInList", -1);
lv.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
lv.setSelection(selectedListItem);
// End - Make the items highlighted
这就是我为我的 Button 所做的
case R.id.bCallRemarks:
// TODO Auto-generated method stub
Button bCRemarks = (Button) findViewById(R.id.bCallRemarks);
bCRemarks.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
ListView.OnItemClickListener oicl = new ListView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> av, View view,
int pos, long id) {
// TODO Auto-generated method stub
Cursor c = (Cursor) av.getItemAtPosition(pos);
String cName = c.getString(c
.getColumnIndexOrThrow("cName"));
Intent intent = new Intent(getApplication(),
CallRemarks.class);
intent.putExtra("CustomerName", cName);
Toast.makeText(av.getContext(), cName,
Toast.LENGTH_SHORT).show();
Intent CallRemarksScreen = new Intent(
getApplicationContext(), CallRemarks.class);
startActivity(CallRemarksScreen);
}
};
}
});
我问这个是因为ListView
底部有一个和多个按钮。所以我必须在选择功能(按钮)之前选择第一项。