我有一个ListView
,我想首先选择(突出显示)一个项目,然后在进入另一个活动之前点击一个按钮。
以下是我想要做的一个例子:
http://i46.tinypic.com/2rc7gw4.png
这就是我创建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
和多个按钮。所以我必须在选择功能(按钮)之前选择第一项。
答案 0 :(得分:0)
我在按钮的onClick之外放出了ListView的onItemClick
@Override
public void onItemClick(AdapterView<?> av, View view, int pos,
long id) {
// TODO Auto-generated method stub
selectedItem = true;
Cursor c = (Cursor) av.getItemAtPosition(pos);
String cName = c.getString(c.getColumnIndexOrThrow("cName"));
intent = new Intent(getApplication(), CallRemarks.class);
intent.putExtra("CustomerId", id);
Toast.makeText(av.getContext(), "id: " + id, Toast.LENGTH_SHORT)
.show();
}
});
and then here is my 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) {
if (selectedItem == false) {
Toast.makeText(getApplicationContext(),
"No Customer Selected", Toast.LENGTH_SHORT)
.show();
} else {
// Intent intent = new Intent(
// getApplicationContext(), CallRemarks.class);
startActivity(intent);
}
}
});
break;
我初始化了一个布尔标志,以检查是否选中了某个项目,以防我在ListView中没有选择项目的情况下点击了一个按钮。
private boolean selectedItem = false;