带有自定义适配器的ListView,textview和spinner无法获取数据

时间:2014-11-17 15:04:35

标签: android android-listview android-spinner android-adapter

我有一个ListView,它有一个自定义适配器。列表中的每一行都有3个文本字段和一个微调器。一旦用户选择了微调项目,我需要在textview中获取相关数据。 所以想象如下listView:| texView | textView | textView | Spinner | 在spinner列中选择某些内容时,我需要获取3个textview数据。但我无法做到这一点。

我很想知道如何让这个工作。这是CustomAdapter代码:

private class ViewHolder {

   Spinner rateSpinner;
   TextView pView;
   TextView nView;
   TextView cView;

 }


 @Override
 public View getView(int position, View convertView, ViewGroup parent) {

 ViewHolder holder = null;


 if (convertView == null) {
    LayoutInflater vi =       
          (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    convertView = vi.inflate(R.layout.todolist_item, null);

    holder = new ViewHolder();
    holder.pView = (TextView)convertView.findViewById(R.id.r1);
    holder.nView = (TextView)convertView.findViewById(R.id.r2);
    holder.cView = (TextView)convertView.findViewById(R.id.r3); 
    holder.rateSpinner = (Spinner)convertView.findViewById(R.id.spinner2);


    convertView.setTag(holder);

    holder.rateSpinner.setOnItemSelectedListener(
       new AdapterView.OnItemSelectedListener() {
           @Override
           public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
               Object item = parent.getItemAtPosition(pos);


               selectedRating = item.toString();

               Log.v("Chosen rating from spinner", selectedRating); //<-this works correctly

               Log.v("**********************","**");// <------- Problem: Need to get list view's textview entries with the associated spinner. But its just not working. Always printing the same wrong textview data

               Log.v("Order Name",item._nText);
               Log.v("Order Count",item._cText);
               Log.v("Order Price",item._pText);

               Log.v("**********************","**");

           }
           @Override
           public void onNothingSelected(AdapterView<?> parent) {

           }
       }
   );


   }
   else {
        holder = (ViewHolder) convertView.getTag();
    }

  holder.rateSpinner = (Spinner)convertView.findViewById(R.id.spinner2);
  ArrayAdapter<String> adapter = new ArrayAdapter<String>(c, 
                    android.R.layout.simple_spinner_item, list);
  holder.rateSpinner.setAdapter(adapter);
  fItem item = getItem(position);



  holder.pView.setText(item._pText);
  holder.nView.setText(item._nText);
  holder.cView.setText(item._cText);


  return convertView;

}

列表中的textViews首先从数据库加载。然后要求用户从微调器中选择等级。用户选择评级后,我需要找到相关的textView项,我不明白如何获取此行数据。请参见微调器的onItemSelectedListener()

中的Print语句

非常感谢你帮助解决这个问题。

2 个答案:

答案 0 :(得分:0)

当您为微调器创建适配器时,您将传递一个在给定代码中找不到的列表。

可能是列表总是一样的,这就是为什么你为每个微调器获得相同的项目的原因?

答案 1 :(得分:0)

只需在您的适配器中尝试此操作即可

@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = new ViewHolder();
View vi = convertView;

if (vi == null) {
    vi = getContext().getLayoutInflater().inflate(R.layout.todolist_item, null);
    holder.pView = (TextView)vi.findViewById(R.id.r1);
    holder.nView = (TextView)vi.findViewById(R.id.r2);
    holder.cView = (TextView)vi.findViewById(R.id.r3); 
    holder.rateSpinner = (Spinner)vi.findViewById(R.id.spinner2);
    holder.personSpinner = (Spinner)vi.findViewById(R.id.spinner2); 
    vi.setTag(holder);
}
else {
    holder = (ViewHolder) vi.getTag();
}

ArrayAdapter<String> adapter = new ArrayAdapter<String>(c, 
                android.R.layout.simple_spinner_item, list);
holder.rateSpinner.setAdapter(adapter);
fItem item = getItem(position);

holder.pView.setText(item._pText);
holder.nView.setText(item._nText);
holder.cView.setText(item._cText);

return vi;
}