android-如何从listVi上的选定项目中提取文本
我有一些物品的清单视图。 我想从所选项目中获取文本。
这是我的列表适配器和onItemClickListener:
ListView lv = (ListView)findViewById(R.id.listView1);
lv.setAdapter(new ArrayAdapter(
this,R.layout.list_black_text,R.id.list_content, values));
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView> parent, View view,
int position, long id) {
??????
}
您能告诉我如何从所选项目中获取字符串。
方法((TextView)view).getText()不起作用,我有一个
ClassCastException: android.widget.LinearLayout
我找到了解决方案,也许有人会需要它:
ListView lv = (ListView)findViewById(R.id.listView1);
lv.setAdapter(new ArrayAdapter(
this,R.layout.list_black_text,R.id.list_content, values));
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView> parent, View view,
int position, long id) {
TextView textView = (TextView) view.findViewById(R.id.list_content);
String text = textView.getText().toString();
System.out.println("Choosen Country = : " + text);
}});
12个解决方案
63 votes
用这个:
String selectedFromList = (lv.getItemAtPosition(position));
希望它会有所帮助。 :)
Android Killer answered 2020-08-06T06:56:04Z
14 votes
为此,您需要编写以下内容:
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView> parent, View view,
int position, long id) {
TextView textView = (TextView) view.findViewById(R.id.list_content);
String text = lv.get(position).toString().trim();
System.out.println("Chosen Country = : " + text);
Rishi answered 2020-08-06T06:56:24Z
4 votes
其他答案看起来不错,但我认为@Rishi可以成为一个完整的答案。
有@Rishi可以实现此目标,这还取决于您是从simple listView还是从Custom ListView(with custom_list_item.xml).获取文本
对于简单的ListView
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView> parent, View view,
int position, long id) {
String text = lv.get(position).tostring().trim();//first method
final String text = ((TextView)view).getText();// second method
}});
对于自定义ListView
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView> parent, View view,
int position, long id) {
TextView textView = (TextView) view.findViewById(R.id.list_content);
//where list_content is the id of TextView in listview_item.xml
}});
别人的问题答案
@Rishi字符串缺少转换。
@Rishi没有提供有关使用R.id.list_content的详细信息
Xar E Ahmer answered 2020-08-06T06:57:11Z
3 votes
您好我正在使用带有已注册上下文菜单的自定义ListView。 在这种情况下,访问自定义列表行中的项目的方式将是:
@Override
public boolean onContextItemSelected(MenuItem item) {
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
switch (item.getItemId()) {
case R.id.add:
TextView textView = (TextView) info.targetView.findViewById(R.id.yourItem);
String text = textView.getText().toString();
Toast.makeText(getApplicationContext(), "Selected " + text, Toast.LENGTH_LONG).show();
default:
return super.onContextItemSelected(item);
}
其中R.id.yourItem是自定义行内的textView
PS:这是我的第一篇文章,希望对您有所帮助;-)
Guille answered 2020-08-06T06:57:40Z
2 votes
这是给任何偶然发现此问题的人的参考。就我而言,我有一个自定义适配器类,其类型为我拥有的POJO类。 另外,我还想传递给适配器并在Activity类的Activity中显示的项目。
我成功地将数据传递给ListView,但还想获取当前选定的文本。
例如:我通过的数据是一位讲师在其上的学校的清单,因此他必须选择当时他想与之合作的学校,并在登录时希望将意图通过新的Activity 讲师选择的当前学校。
因此,我的ListView onClick():
private void loginSuccess() {
progressDialog.dismiss();
if (mySchoolsList.size() > 1) {
schoolsListView = new ListView(MainActivity.this);
schoolsArrayAdapter = new SchoolListAdapter(MainActivity.this, android.R.layout.simple_list_item_1, mySchoolsList);
schoolsListView.setAdapter(schoolsArrayAdapter);
dialog = new Dialog(MainActivity.this);
dialog.setContentView(schoolsListView);
dialog.setTitle("Welcome " + staff.getFullName());
dialog.show();
schoolsListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView> parent, View view, int position, long id) {
//the .getName() is accessed from the School POJO class.
String schoolName = schoolsArrayAdapter.getItem(position).getName();
intent = new Intent(MainActivity.this, NavMainActivity.class);
intent.putExtra("sentIntent", schoolName);
startActivity(intent);
} else {
intent = new Intent(MainActivity.this, NavMainActivity.class);
intent.putExtra("sentIntent", recieveName);
startActivity(intent);
}
希望这能节省一天的时间,因为这里的所有解决方案都不适合我。 干杯!
Peterstev Uremgba answered 2020-08-06T06:58:19Z
2 votes
肯定会的!希望您满意!
ListView lv=(ListView)findViewById(R.id.listView1);
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
String text = (String) lv.getItemAtPosition(arg2);
Toast.makeText(getApplicationContext(), text, Toast.LENGTH_SHORT).show();
SivaRamaKrishnan T answered 2020-08-06T06:58:39Z
0 votes
如果使用列表在onItemClick()方法中填充ListView。
String s = list.get(position);
您需要能够在内部类中访问此列表
Rajdeep Dua answered 2020-08-06T06:59:03Z
0 votes
尝试这样做,将其插入onItemClickListener,我不确定您的Country类是什么样的:
String s = values.get(position).getCountryName();
Log.e("LISTVIEW", "selected item text = "+s);
String s = values.get(position).toString();
Log.e("LISTVIEW", "selected item text = "+s);
luciferche answered 2020-08-06T06:59:24Z
0 votes
public void onItemClick(**AdapterView**> parent, View view,
int position, long id) {
}
参见TextView .....--- >>>该类
只需要这样做:
TextView selectedText=(TextView) parent.findViewById(R.id.textView2);
看...您直接得到TextView
zhejunliu answered 2020-08-06T06:59:52Z
0 votes
试试这个代码:
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView> parent, View view, int position, long id) {
Toast.makeText(getApplicationContext(),(String)parent.getItemAtPosition(position),Toast.LENGTH_SHORT).show();
Hossam Ali answered 2020-08-06T07:00:12Z
0 votes
myListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView> av,
View view, int position, long l) {
String text = String.valueOf(myListView.getItemAtPosition(position));
Mester Hassan answered 2020-08-06T07:00:30Z
0 votes
它是如此简单
@Override
public void onItemClick(AdapterView> adapterView, View view, int i, long l) {
TextView text1 = (TextView) findViewById(R.id.Text1);
TextView text2 = (TextView) findViewById(R.id.Text2);
String txt_1 = ""+text1.getText().toString().trim();
String txt_2 = ""+text2.getText().toString().trim();
//Other_Related_Work
Kunal Chikte answered 2020-08-06T07:00:53Z