我为具有多个TextViews的ListView提供了一个自定义适配器.我想将onItemClickListener设置为ListView并从TextViews中提取文本.我尝试使用此:
String s =(String) ((TextView) mListView.findViewById(R.id.myNr)).getText();
但按照默认设置,它总是返回第一个列表项的值! (我猜是因为它没有从列表中指定商品ID)
我也试过这个:
String s = (((TextView) mListView.getItemAtPosition(myItemInt)).
findViewById(R.id.myNr)).toString();
没用!有什么建议么?
这是通过以下功能实现的:
mListView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView> myAdapter, View myView,
int myItemInt, long mylng) {
// String s statements
}
编辑:
这是完整的代码:
mListView_myentries.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView> myAdapter, View myView,
int myItemInt, long mylng) {
String workRequestSelected = (String) ((TextView) mListView_myentries.findViewById(R.id.work_request)).getText();
String activitySelected = (String) ((TextView) mListView_myentries.findViewById(R.id.activity)).getText();
String statusSelected = (String) ((TextView) mListView_myentries.findViewById(R.id.status)).getText();
String workRequestDescSelected = (String) ((TextView) mListView_myentries.findViewById(R.id.desc)).getText();
String actualHoursString = (String) ((TextView) mListView_myentries.findViewById(R.id.actual_hours)).getText();
}
解决方法:
试试下面的代码
mListView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView> myAdapter, View myView,int myItemInt, long mylng) {
String s =(String) ((TextView) myView.findViewById(R.id.myNr)).getText();
}
}
标签:android-listview,android
来源: https://codeday.me/bug/20191201/2078500.html