我有一个listView,其中每行包含一个textView和一个Spinner。我可以填充这两个,并且可以从Spinner中进行选择,没有任何问题。当用户从微调器中进行选择时,我可以使用onItemSelected((读取选择。
我的问题是,我不知道如何识别listView中的哪一行被选中。
我尝试过使用setTag((和setId((,但都无法获得它们中的任何一个。
我还尝试在行上放置一个透明按钮,并使用onClickListener,但该按钮会遮挡微调器,因此您实际上不会激活微调器。
下面是我的代码。如果能给我任何帮助或指引我前进的方向,我将不胜感激。
我在SO上看了几十篇相关的帖子,但找不到一篇真正回答这个问题的。如果我错过了,我深表歉意。
感谢
适配器中getView((的代码。
public class AdapterSelectEventPayment extends BaseAdapter {
private ArrayList<String> mSpinnerItems;
private ArrayList<String> mDataSource;
private Context mContext;
private AdapterView.OnItemSelectedListener mSpinnerItemSelectedListener;
public AdapterSelectEventPayment(ArrayList<String> data, ArrayList<String> spinnerItems, Context context) {
mDataSource = data;
mContext = context;
mSpinnerItems = spinnerItems;
}
@Override
public int getCount() {
return mDataSource.size();
}
@Override
public Object getItem(int position) {
return mDataSource.get(position);
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
if (view == null) {
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.select_event_charge_items, null);
}
TextView textView = (TextView) view.findViewById(R.id.row_item_textview);
Spinner spinner = (Spinner) view.findViewById(R.id.row_item_spinner);
textView.setText(mDataSource.get(position));
ArrayAdapter<String> adapter = new ArrayAdapter<String>(mContext, android.R.layout.simple_spinner_dropdown_item, mSpinnerItems);
spinner.setAdapter(adapter);
spinner.setTag(position);
spinner.setOnItemSelectedListener(mSpinnerItemSelectedListener);
return view;
}
public void setOnSpinnerItemSelectedListener(AdapterView.OnItemSelectedListener listener){
this.mSpinnerItemSelectedListener = listener;
}
}
主活动中的代码
private void sortArrayList() {
AdapterSelectEventPayment adapter = new AdapterSelectEventPayment(participants, chargeArray, this);
adapter.setOnSpinnerItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
System.out.println("The spinner has been spun... and position is " + position + " and the id = " + id);
System.out.println("Tag is " + selectedItemView.getTag());
}
@Override
public void onNothingSelected(AdapterView<?> parentView) {
System.out.println("The spinner has NOT been spun...");
}
});
lvSpinner.setAdapter(adapter);
}
不要在selectedItemView
上调用getTag
,而是在parentView
上调用它
我建议您创建一个界面,用Spinner选择的所有相关信息+点击的项目的位置来调用您
interface ListSpinnerListener() {
void onSpinnerItemSelected(int rowPos, int selectedItemPos, ...);
}
您的"活动"可以按照分配AdapterView.OnItemSelectedListener
的方式进行分配,而是直接在getView()
方法内部使用侦听器
适配器:
private ListSpinnerListener listSpinnerListener;
...
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
if (view == null) {
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.select_event_charge_items, null);
}
TextView textView = (TextView) view.findViewById(R.id.row_item_textview);
Spinner spinner = (Spinner) view.findViewById(R.id.row_item_spinner);
textView.setText(mDataSource.get(position));
ArrayAdapter<String> adapter = new ArrayAdapter<String>(mContext, android.R.layout.simple_spinner_dropdown_item, mSpinnerItems);
spinner.setAdapter(adapter);
spinner.setTag(position);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int itemPosition, long id) {
System.out.println("The spinner has been spun... and position is " + position + " and the id = " + id);
System.out.println("Tag is " + selectedItemView.getTag());
listSpinnerListener.onSpinnerItemSelectedListener(position, itemPosition, ...);
}
@Override
public void onNothingSelected(AdapterView<?> parentView) {
System.out.println("The spinner has NOT been spun...");
}
});
return view;
}
这样,你就可以确保通过一个简单的界面随时获得所需的所有相关信息,而不必通过标签之类的方式破解答案。