gpt4 book ai didi

java - Android:删除选定的列表项

转载 作者:行者123 更新时间:2023-12-02 10:34:16 25 4
gpt4 key购买 nike

所以我有一个名为 ShowActivity.java 的类。在本类(class)中,我从 sqlite 数据库获取一个列表。我在此 ListView 显示数据和删除数据上添加了两个按钮。因此,一旦我选择一个列表项,我想根据按钮单击事件执行任何一项操作(显示数据或删除所选行)。

在下面的代码中。我所做的是选择一行,一旦我单击一个行项目,我就会得到一个包含该行信息的完整 toast 。

我想做的是:一旦我选择一行,如果我点击显示数据按钮,我应该得到我现在得到的信息。或者

一旦我选择一行,如果我单击删除数据按钮,我应该从 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();



}
}


}

最佳答案

向 ListView 添加选择模式。 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);
}

关于java - Android:删除选定的列表项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53404347/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com