无法从android中的自定义列表视图中获取所选项目
Posted
技术标签:
【中文标题】无法从android中的自定义列表视图中获取所选项目【英文标题】:Unable to get selected item from custom listview in android 【发布时间】:2015-12-31 18:21:04 【问题描述】:我使用基本适配器创建了一个自定义列表视图。我想从列表视图中获取所选项目。我尝试了不同的解决方案,但没有一个有效。我的 ListView 代码如下所示:
class SingleRow
String mFetchedJobTitle;
String mFetchedCompanyName;
String mFetchedExperience;
String mFetchedLocation;
SingleRow(String mFetchedJobTitle, String mFetchedCompanyName, String mFetchedExperience, String mFetchedLocation)
this.mFetchedJobTitle = mFetchedJobTitle;
this.mFetchedCompanyName = mFetchedCompanyName;
this.mFetchedExperience = mFetchedExperience;
this.mFetchedLocation = mFetchedLocation;
class MyAdapter extends BaseAdapter
ArrayList<SingleRow> list;
Context context;
MyAdapter(Context c)
context = c;
list = new ArrayList<SingleRow>();
String[] mFetchedJobTitle1 = searchJobTitle;
String[] mFetchedCompanyName1 = searchCompanyName;
String[] mFetchedExperience1 = searchExperience;
String[] mFetchedLocation1 = searchLocation;
for (int i = 0; i < mArrayLength; i++)
list.add(new SingleRow(mFetchedJobTitle1[i], mFetchedCompanyName1[i], mFetchedExperience1[i], mFetchedLocation1[i] ));
@Override
public int getCount()
return list.size();
@Override
public Object getItem(int position)
return list.get(position);
@Override
public long getItemId(int position)
return position;
@Override
public View getView(int position, View convertView, ViewGroup parent)
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View row = inflater.inflate(R.layout.custom_search_row, parent, false);
TextView title = (TextView) row.findViewById(R.id.textViewJobTitle);
TextView companyName = (TextView) row.findViewById(R.id.textViewCompanyName);
TextView experience = (TextView) row.findViewById(R.id.textViewExperience);
TextView location = (TextView) row.findViewById(R.id.textViewLocation);
SingleRow temp = list.get(position);
title.setText(temp.mFetchedJobTitle);
companyName.setText(temp.mFetchedCompanyName);
experience.setText(temp.mFetchedExperience);
location.setText(temp.mFetchedLocation);
return row;
ListView OnItemClickListener 代码:
mListView.setOnItemClickListener(new AdapterView.OnItemClickListener()
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l)
mSelectedItem = mListView.getItemAtPosition(i).toString();
String mselected = (String) adapterView.getSelectedItem();
Log.d("selecteditem", ""+mselected);
Log.d("selecteditem", ""+mSelectedItem);
);
我将 mSelectedItem 的值设为:
SearchResultsActivity$SingleRow@2399b34e
如何获得准确的字符串值。任何人都可以建议我一个想法。欢迎所有建议。
【问题讨论】:
mSelectedItem
的类型是什么
【参考方案1】:
知道了,在您的每个列表项中,您正在获取一个 SingleRow
类实例。
像这样做
mListView.getItemAtPosition(i).mFetchedJobTitle;
【讨论】:
【参考方案2】:试试这个
SingleRow mSelectedItem = mListView.getItemAtPosition(i);
String mFetchedJobTitle = mSelectedItem.mFetchedJobTitle;
String mFetchedCompanyName = mSelectedItem.mFetchedCompanyName;
String mFetchedExperience = mSelectedItem.mFetchedExperience;
String mFetchedLocation = mSelectedItem.mFetchedLocation;
【讨论】:
【参考方案3】:adapterView.getSelectedItem() 返回对象的引用,并在将其转换为 String 时返回包含类名和存储对象的内存位置的字符串。为了得到选中的项目你可以使用这个方法
public Object getItem(int position)
return list.get(position);
【讨论】:
以上是关于无法从android中的自定义列表视图中获取所选项目的主要内容,如果未能解决你的问题,请参考以下文章
如何将最新数据附加到android中的自定义基本适配器列表视图?
如何从listview android的主列表中获取所选项目的文本