我正在使用app,它有多个清单,用户可以从中选择项目,这些项目是从SQLite生成的。
我已经使用了以下内容,但它只是给了我选中的字符串。
这是我的Product.java
list = (ListView)findViewById(android.R.id.list);
list.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
ArrayList<String> results = new ArrayList<String>();
try {
sampleDB = this.openOrCreateDatabase(SAMPLE_DB_NAME, MODE_PRIVATE, null);
sampleDB.execSQL("CREATE TABLE IF NOT EXISTS " +
SAMPLE_TABLE_NAMES +
" (_id INTEGER PRIMARY KEY AUTOINCREMENT, pro_name text);");
sampleDB.execSQL("INSERT INTO " +
SAMPLE_TABLE_NAMES +
" Values ('1','Milk');");
sampleDB.execSQL("INSERT INTO " +
SAMPLE_TABLE_NAMES +
" Values ('2','Sugar');");
sampleDB.execSQL("INSERT INTO " +
SAMPLE_TABLE_NAMES +
" Values ('3','Oil');");
}
finally{
if (sampleDB != null)
sampleDB.execSQL("DELETE FROM " + SAMPLE_TABLE_NAMES);
sampleDB.close();
}
setListAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_checked, new ArrayList()));
new AddStringTask().execute();
}
class AddStringTask extends AsyncTask<Void, String, Void> {
@Override
protected Void doInBackground(Void... unused) {
for (String item : items) {
publishProgress(item);
SystemClock.sleep(100);
}
return (null);
}
@Override
protected void onProgressUpdate(String... item) {
((ArrayAdapter) getListAdapter()).add(item[0]);
}
@Override
protected void onPostExecute(Void unused) {
setSelection(3);
Toast.makeText(Product.this, "Done!", Toast.LENGTH_SHORT).show();
}
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
CheckedTextView check = (CheckedTextView) v;
check.setChecked(!check.isChecked());
String selection = l.getItemAtPosition(position).toString();
Toast.makeText(this, selection, Toast.LENGTH_SHORT).show();
if(check.isChecked())
{
Toast.makeText(Product.this, "Item are Checked!", Toast.LENGTH_SHORT).show();
}
if(!check.isChecked()&&!check.isChecked())
{
Toast.makeText(Product.this, "Item are not checked!", Toast.LENGTH_SHORT).show();
}
}
public View getView(final int position, View convertView, ViewGroup parent)
{
cursor.moveToPosition(position);
LayoutInflater inf=getLayoutInflater();
vi=inf.inflate(R.layout.total, null);
vi.setTag(cursor.getInt(0));
return vi;
}
这是Final.java
setContentView(R.layout.total);
txtEmps=(TextView)findViewById(R.id.txtEmps);
txtPro=(TextView)findViewById(R.id.txtPro);
list = (ListView)findViewById(android.R.id.list);
startPKCounter();
}
@Override
protected void onStart() {
// TODO Auto-generated method stub
try{
dbHelper=new DatabaseHelper(this);
txtEmps.setText(txtEmps.getText()+String.valueOf(dbHelper.getEmployeeCount()));
}
finally
{
}
super.onStart();
}
private void startPKCounter() {
// TODO Auto-generated method stub
Toast.makeText(this, "Counted", Toast.LENGTH_SHORT).show();
}
protected void onListItemClick(ListView l, View v, int position, long id)
{
int primaryValue= (int)v.getTag(cursor);
Toast.makeText(Final.this, primaryValue+"", Toast.LENGTH_SHORT).show();
}
它在v.getTag()上给我错误。
答案 0 :(得分:0)
protected void onListItemClick(ListView l, View v, int position, long id)
请参阅那里的long id
部分? (id
)是您单击的列表项的主键。