我的ListView有一个自定义适配器,它有多个TextView。我想将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();
}
答案 0 :(得分:3)
尝试以下代码
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();
}
}
答案 1 :(得分:0)
这对我来说很完美,但是当我直接打电话给我的listContacts(listview)时,它总是给我第一张唱片,但是当我把视图传递给我时,它就像一个魅力。
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
String item = (((TextView)view.findViewById(R.id.txtVwConID)).getText().toString());
Toast.makeText(getApplicationContext(), "you selected: " + item, Toast.LENGTH_SHORT).show();
如果您对我的代码有疑问,请点击此处