无法从 android 中的自定义列表视图中获取所选项目
Unable to get selected item from custom listview in android
我已经使用基本适配器创建了自定义列表视图。我想从列表视图中获取选定的项目。我尝试了不同的解决方案,但其中 none 有效。我的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
如何获取准确的字符串值。谁能给我一个想法。欢迎所有建议。
知道了,在您的每个列表项中,您正在获取一个 SingleRow
class 实例。
像
那样做
mListView.getItemAtPosition(i).mFetchedJobTitle;
试试这个
SingleRow mSelectedItem = mListView.getItemAtPosition(i);
String mFetchedJobTitle = mSelectedItem.mFetchedJobTitle;
String mFetchedCompanyName = mSelectedItem.mFetchedCompanyName;
String mFetchedExperience = mSelectedItem.mFetchedExperience;
String mFetchedLocation = mSelectedItem.mFetchedLocation;
adapterView.getSelectedItem() returns 对象的引用并将其转换为 String it returns 包含 class 名称和对象所在内存位置的字符串存储。为了获得所选项目,您可以使用此方法
public Object getItem(int position) {
return list.get(position);
}
我已经使用基本适配器创建了自定义列表视图。我想从列表视图中获取选定的项目。我尝试了不同的解决方案,但其中 none 有效。我的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
如何获取准确的字符串值。谁能给我一个想法。欢迎所有建议。
知道了,在您的每个列表项中,您正在获取一个 SingleRow
class 实例。
像
mListView.getItemAtPosition(i).mFetchedJobTitle;
试试这个
SingleRow mSelectedItem = mListView.getItemAtPosition(i);
String mFetchedJobTitle = mSelectedItem.mFetchedJobTitle;
String mFetchedCompanyName = mSelectedItem.mFetchedCompanyName;
String mFetchedExperience = mSelectedItem.mFetchedExperience;
String mFetchedLocation = mSelectedItem.mFetchedLocation;
adapterView.getSelectedItem() returns 对象的引用并将其转换为 String it returns 包含 class 名称和对象所在内存位置的字符串存储。为了获得所选项目,您可以使用此方法
public Object getItem(int position) {
return list.get(position);
}