要从ListView中获取SQLite的rowId,你可以在ListView的适配器中使用setTag()方法来存储rowId,并在点击事件中获取它。
以下是一个示例代码:
public class CustomAdapter extends ArrayAdapter {
private ArrayList dataList;
private ArrayList rowIdList;
private Context mContext;
public CustomAdapter(Context context, ArrayList data, ArrayList rowIds) {
super(context, 0, data);
mContext = context;
dataList = data;
rowIdList = rowIds;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = LayoutInflater.from(mContext).inflate(R.layout.list_item, parent, false);
}
TextView textView = convertView.findViewById(R.id.textView);
textView.setText(dataList.get(position));
convertView.setTag(rowIdList.get(position)); // 存储rowId到视图中
convertView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
long rowId = (long) v.getTag(); // 获取存储的rowId
// 在这里处理点击事件,并使用rowId进行操作
}
});
return convertView;
}
}
public class MainActivity extends AppCompatActivity {
private ListView listView;
private CustomAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = findViewById(R.id.listView);
// 从SQLite数据库中获取数据和rowId
ArrayList dataList = new ArrayList<>();
ArrayList rowIdList = new ArrayList<>();
// 假设从数据库中获取了数据和rowId,并存储到dataList和rowIdList中
adapter = new CustomAdapter(this, dataList, rowIdList);
listView.setAdapter(adapter);
}
}
现在,当你点击ListView中的项时,你可以通过获取存储的rowId来执行相应的操作。