所以我有一个叫做ShowActivity.java的类。在这一节课中,我从sqlite数据库获取列表。 我在此ListView的“显示数据”和“删除数据”上添加了两个按钮。因此,一旦我选择了一个列表项,我便希望根据按钮单击事件执行任何一项操作(“显示数据”或“删除所选的行”)。
在下面的代码中。我所做的是我选择了一行,一旦单击某行项目,我就会获得完整的吐司以及该行的信息。
我想做的是: 一旦我选择了一行,如果我单击显示数据按钮,我应该得到一连串的信息,我现在正在得到什么。或者
一旦我选择一行,如果我单击删除数据按钮,我应该从列表视图以及数据库中删除该数据。
public class ShowlistActivity extends Activity {
String myBtnListener;
private ListView listView;
Button showBtn =findViewById(R.id.show_btn);
Button deleteBtn=findViewById(R.id.delete_btn)
@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.showlist_ehr);
listView = (ListView) this.findViewById(R.id.checkout_listview);
listView.setOnItemClickListener(new ItemClickListener());
// get the sql string delivered from the QueryActivity
Intent intent = this.getIntent();
String sql = intent.getStringExtra("sql");
// execute the sql
Cursor cursor = DBOperator.getInstance().execQuery(sql);
// bind the data to list
SimpleCursorAdapter adapter = new SimpleCursorAdapter(
getApplicationContext(), R.layout.listitem_ehr, cursor,
new String[] { "PaFirstName", "VisitDate", "MedIssue" }, new
int[] {
R.id.stname, R.id.coduedate, R.id.lbtitle },
SimpleCursorAdapter.IGNORE_ITEM_VIEW_TYPE);
listView.setAdapter(adapter);
showBtn.setOnClickListener(myBtnListener);
button2.setOnClickListener(myBtnListener);
View.OnClickListener myBtnListener= new View.OnClickListener() {
@Override
public void onClick(View view) {
switch(view.getId()){
case R.id.button1_id:
//do button 1 action
break;
case R.id.button2_id:
//do button 2 action
break;
}
}
}
}
private class ItemClickListener implements OnItemClickListener {
public void onItemClick(AdapterView<?> parent, View view, int position,long id) {
Cursor cursor = (Cursor) listView.getItemAtPosition(position);
String stid = cursor.getString(0);
String lbtitle = cursor.getString(1);
String coduedate = cursor.getString(2);
//String coreturned = cursor.getString(3);
// cofine = cursor.getString(4);
//String stname = cursor.getString(5);
Toast.makeText(getApplicationContext(),"Student ID: " + stid + "\nStudent Name: " + lbtitle+ "\nBook Title: " + coduedate , Toast.LENGTH_LONG).show();
}
}
}
答案 0 :(得分:1)
将选择模式添加到列表视图。 rounded_border
是您制作的自定义可绘制对象,用作选定项目指示符。
android:choiceMode="singleChoice"
android:listSelector="@drawable/rounded_border"
更新您的onCreate
@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.showlist_ehr);
...
...
View.OnClickListener myBtnListener= new View.OnClickListener() {
@Override
public void onClick(View view) {
if(listView.getCheckedItemPosition() >= 0) {
//get the object
Cursor selectedCursor = (Cursor) listView.getItemAtPosition(listView.getCheckedItemPosition());
switch(view.getId()){
case R.id.show_btn:
//show selectedCursor details
break;
case R.id.delete_btn:
String stid = selectedCursor.getString(0);
//change the stud_id to your database field id name
String sql = "DELETE FROM Student WHERE stud_id = '" + stid + "'";
Cursor cursor = DBOperator.getInstance().execQuery(sql);
adapter.changeCursor(cursor);
break;
}
}
}
}
showBtn.setOnClickListener(myBtnListener);
deleteBtn.setOnClickListener(myBtnListener);
}