如何gettext
listview
中的OnItemClickListener
??
我无法在listview中获取文字toast work.but gettext();
无法正常工作
这是我的代码。提前谢谢。
listView.setOnItemClickListener(new OnItemClickListener() {
@SuppressWarnings("unused")
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// ListView Clicked item index
int itemPosition = position;
//parent.getChildAt(position - listView.getLastVisiblePosition()).findViewById(R.id.listView1);
String itemValue = (String) listView.getItemAtPosition(position);
//???????????????????????????.getText().toString();
// Show Alert
Toast.makeText(
getApplicationContext(),
"Position :" + itemPosition + " ListItem: " + itemValue,
Toast.LENGTH_LONG
).show();
}
});
答案 0 :(得分:2)
只需输入此代码并在项目中导入textview
即可 String itemValue = (String) listView.getItemAtPosition(position);
String values=((TextView)view).getText().toString();
答案 1 :(得分:1)
试试这个:
lista.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id){
TextView txt = (TextView) view.findViewById(R.id.lblId);
String s = textView.getText().toString();
Toast.makeText(getActivity(), "Your id "+s, Toast.LENGTH_SHORT).show();
}
});
答案 2 :(得分:0)
You have to get the id of the textView from the view parameter like this
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
TextView textView =(TextView) view.findViewById(R.Id.YourTextViewId);
String str=textView.getText();
}
答案 3 :(得分:0)
我想你可以试试这个:
@Override
public void onItemClick(AdapterView<?> view, View v, int position, long arg3) {
ListView l = (ListView) view;
String s = (String) l.getItemAtPosition(position);
}
答案 4 :(得分:0)
而不是从视图中获取文本。从用于将数据绑定到values
的数据集ArrayAdapter
中获取。
String itemValue = values[position];
Toast.makeText(YourActivty.this, "Clicked " + itemValue + " at " + position, Toast.LENGTH_LONG).show();