我使用基本适配器创建了自定义列表视图。我想从listview中获取所选项目。我尝试了不同的解决方案,但没有一个能正常工作。我的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
如何获取确切的字符串值。任何人都可以建议我一个想法。欢迎提出所有建议。
答案 0 :(得分:2)
在你的每个列表项中得到它你的SingleRow
类实例。
这样做
mListView.getItemAtPosition(i).mFetchedJobTitle;
答案 1 :(得分:1)
试试这个
SingleRow mSelectedItem = mListView.getItemAtPosition(i);
String mFetchedJobTitle = mSelectedItem.mFetchedJobTitle;
String mFetchedCompanyName = mSelectedItem.mFetchedCompanyName;
String mFetchedExperience = mSelectedItem.mFetchedExperience;
String mFetchedLocation = mSelectedItem.mFetchedLocation;
答案 2 :(得分:0)
adapterView.getSelectedItem()返回对象的引用,并在将其转换为String时返回包含存储对象的类名和内存位置的字符串。为了获得所选项目,您可以使用此方法
public Object getItem(int position) {
return list.get(position);
}