gpt4 book ai didi

java - Android光标适配器获取listview的checkbox和数据

转载 作者:太空宇宙 更新时间:2023-11-04 13:15:36 25 4
gpt4 key购买 nike

我有这段使用光标适配器的代码:

   public class TemplateActivity extends Activity {

Button btnSort, btnDel;

private ListViewAdapter listAdapter;
private RetailerDatabaseHelper dbHelper;
private ListView listView;

private static final String TAG = TemplateActivity.class.getSimpleName();

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_template);

btnSort = (Button) findViewById(R.id.btnSort);
btnDel = (Button) findViewById(R.id.btnDelete);

dbHelper = new RetailerDatabaseHelper(this);
listView = (ListView) findViewById(R.id.listViewData);

listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Log.d(TAG, "clicked on item: " + position);


}
});

new Handler().post(new Runnable() {
@Override
public void run() {
listAdapter = new ListViewAdapter(TemplateActivity.this, dbHelper.getAllData());
listView.setAdapter(listAdapter);
}
});

我的问题是,当单击数据时,如何从 ListView 中获取数据的值。

这是我的适配器:

public class ListViewAdapter extends CursorAdapter {

public ListViewAdapter (Context context, Cursor c) {
super(context, c);
}

@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
// when the view will be created for first time,
// we need to tell the adapters, how each item will look
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
View retView = inflater.inflate(R.layout.custom_dialog_box, parent, false);

return retView;
}

@Override
public void bindView(View view, Context context, Cursor cursor) {
// here we are setting our data
// that means, take the data from the cursor and put it in views

TextView textViewPersonName = (TextView) view.findViewById(R.id.checkBox1);
textViewPersonName.setText(cursor.getString(cursor.getColumnIndex(cursor.getColumnName(1))));

TextView textViewPersonPIN = (TextView) view.findViewById(R.id.number);
textViewPersonPIN.setText(cursor.getString(cursor.getColumnIndex(cursor.getColumnName(2))));
}
}

另一件事。我将如何在这里实现,当我选中一个复选框并按下按钮时,它将获取该复选框的数据?谢谢大家的帮助。

最佳答案

我没有在 ListViewAdapter 上工作过,但我的情况与你的情况相同。我使用了 CustomAdapter 类扩展 Baseadapter 并使用自定义布局。通过这种方式,您可以为每个 View 单独设置 OnClicklistners(复选框、按钮、总布局...)。

关于java - Android光标适配器获取listview的checkbox和数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33560818/

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