在listview中填充微调器并将微调器值传递给另一个活动

时间:2015-03-23 05:39:51

标签: android listview android-spinner

我正在从MySql数据创建多个动态微调器项并在listview中填充它们。一切正常但是当将任何随机微调器数据传递给另一个活动时,我的代码从最后一个listview项传递值。

这是我的listview适配器类,用于填充listview中的多个微调器

 public class ItemListViewAdapter extends BaseAdapter implements  AdapterView.OnItemSelectedListener {


List<String> separated_q,separated_p;
Context context;
LayoutInflater inflater;
ArrayList<HashMap<String, String>> data;
ImageLoader imageLoader;
private String item,pot,pc,qr;
HashMap<String, String> resultp = new HashMap<String, String>();

public ItemListViewAdapter(Context context,
                       ArrayList<HashMap<String, String>> arraylist) {
    this.context = context;
    data = arraylist;
    imageLoader = new ImageLoader(context);
}

@Override
public int getCount() {
    return data.size();
}

@Override
public Object getItem(int position) {
    return position;
}

@Override
public long getItemId(int position) {
    return position;
}

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


    inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    View itemView = inflater.inflate(R.layout.item, parent, false);
    Spinner spinner = (Spinner) itemView.findViewById(R.id.spinner);
    spinner.setOnItemSelectedListener(this);
   add=(Button) itemView.findViewById(R.id.Add);
    resultp = data.get(position);

    List<String> p= new ArrayList<String>();

     separated_q = Arrays.asList(resultp.get(Gtems.sz).split("\\s*,\\s*"));
     separated_p = Arrays.asList(resultp.get(Gtems.Pr).split("\\s*,\\s*"));
    for(int i=0;i<separated_q.size();i++) {
        p.add(separated_q.get(i)+" - "+separated_p.get(i));
    }



    ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(context,
            R.layout.spinner, p);
    dataAdapter.setDropDownViewResource(R.layout.spinner_drop);
    spinner.setAdapter(dataAdapter);


    dukannaa = (TextView) itemView.findViewById(R.id.rank);
    taadscntu = (TextView) itemView.findViewById(R.id.country);



    imgurl = (ImageView) itemView.findViewById(R.id.flag);


    dukannaa.setText(resultp.get(Gtems.Br));

    taadscntu.setText(resultp.get(Gtems.pr));

    imageLoader.DisplayImage(resultp.get(Gtems.IMAGE), imgurl);

    add.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // Get the position
            resultp = data.get(position);
    ViewGroup selectedRowView=(ViewGroup)arg0.getParent();
            Spinner currentRowSpinner=(Spinner)selectedRowView.findViewById(R.id.spinner);
            int selectedPos=currentRowSpinner.getSelectedItemPosition();

            pc = separated_p.get(selectedPos);
            qr = separated_q.get(selectedPos);



            Intent intent = new Intent(context, Gsingle.class);
            intent.putExtra("qr",qr);
            intent.putExtra("pc",pc);
            intent.putExtra("prn",resultp.get(Gitems.prid));


            context.startActivity(intent);

        }
    });

    return itemView;
   }
  @Override
  public void onItemSelected(AdapterView<?> parent, View view, int position, long  id) {
    // On selecting a spinner item
    item = parent.getItemAtPosition(position).toString();




}

public void onNothingSelected(AdapterView<?> arg0) {
    // TODO Auto-generated method stub

}

 }

如果将单个项目填充到列表视图上但在填充多个listview项目时,只有最后一个listview项目中的微调器值传递给Gsingle类。请建议在哪里犯错误

2 个答案:

答案 0 :(得分:0)

尝试获取单击按钮行Spinner和所选索引:

  @Override
   public void onClick(View arg0) {
    ViewGroup selectedRowView=(ViewGroup)view.getParent();
    Spinner currentRowSpinner=(Spinner)selectedRowView.findViewById(R.id.spinner);
    int selectedPos=currentRowSpinner.getSelectedItemPosition();
    pc = separated_p.get(selectedPos);
    qr = separated_q.get(selectedPos);
       intent.putExtra("qr",qr);
       intent.putExtra("pc",pc);
       //... your code here...
       context.startActivity(intent);

    }

答案 1 :(得分:0)

解决了!!!将微调器项目位置和arraylist传递给所需的活动,并根据微调器项目的位置得到逗号分割和显示值

这里是答案

  public class ItemListViewAdapter extends BaseAdapter implements  AdapterView.OnItemSelectedListener {


 List<String> separated_q,separated_p;
 Context context;
 LayoutInflater inflater;
 ArrayList<HashMap<String, String>> data;
  ImageLoader imageLoader;
  private String item,pot,pc,qr;
  HashMap<String, String> resultp = new HashMap<String, String>();

  public ItemListViewAdapter(Context context,
                   ArrayList<HashMap<String, String>> arraylist) {
   this.context = context;
   data = arraylist;
   imageLoader = new ImageLoader(context);
   }

 @Override
 public int getCount() {
 return data.size();
 }

 @Override
 public Object getItem(int position) {
 return position;
 }

 @Override
 public long getItemId(int position) {
 return position;
 }

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


     inflater = (LayoutInflater) context
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

  View itemView = inflater.inflate(R.layout.item, parent, false);
 Spinner spinner = (Spinner) itemView.findViewById(R.id.spinner);
  spinner.setOnItemSelectedListener(this);
add=(Button) itemView.findViewById(R.id.Add);
resultp = data.get(position);

List<String> p= new ArrayList<String>();

 separated_q = Arrays.asList(resultp.get(Gtems.sz).split("\\s*,\\s*"));
 separated_p = Arrays.asList(resultp.get(Gtems.Pr).split("\\s*,\\s*"));
 for(int i=0;i<separated_q.size();i++) {
    p.add(separated_q.get(i)+" - "+separated_p.get(i));
 }



ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(context,
        R.layout.spinner, p);
dataAdapter.setDropDownViewResource(R.layout.spinner_drop);
spinner.setAdapter(dataAdapter);


dukannaa = (TextView) itemView.findViewById(R.id.rank);
taadscntu = (TextView) itemView.findViewById(R.id.country);



imgurl = (ImageView) itemView.findViewById(R.id.flag);


dukannaa.setText(resultp.get(Gtems.Br));

taadscntu.setText(resultp.get(Gtems.pr));

imageLoader.DisplayImage(resultp.get(Gtems.IMAGE), imgurl);

add.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View arg0) {
        // Get the position
        resultp = data.get(position);
ViewGroup selectedRowView=(ViewGroup)arg0.getParent();
        Spinner currentRowSpinner=(Spinner)selectedRowView.findViewById(R.id.spinner);
        int selectedPos=currentRowSpinner.getSelectedItemPosition();

        pc = resultp.get(Gitems.Pr);
        qr = resultp.get(Gitems.Sz);
     xy=String.valueOf(selectedPos);


        Intent intent = new Intent(context, Gsingle.class);
        intent.putExtra("qr",qr);
        intent.putExtra("pc",pc);
     intent.putExtra("xy",xy);
        intent.putExtra("prn",resultp.get(Gitems.prid));


        context.startActivity(intent);

    }
});

return itemView;
  }
    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position,   long  id) {
// On selecting a spinner item
   item = parent.getItemAtPosition(position).toString();




 }

 public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub

}

}

在Gsingle类

 Intent gt=getIntent();

    pc=gt.getStringExtra("pc");
    qr=gt.getStringExtra("qr");
 xy=gt.getStringExtra("xy");
 x=Integer.parseInt(xy);


    separated_q = Arrays.asList(qr.split("\\s*,\\s*"));
    separated_p = Arrays.asList(pc.split("\\s*,\\s*"));



     pm.setText(separated_p.get(x));
     qt.setText(separated_q.get(x));