在自定义列表视图中无法获取正确的数据

时间:2012-06-07 08:19:22

标签: android listview

这里我正在使用一个活动,其中有一个列表视图,我正在填充 形成一个xml字符串,人口很好,在listview中有两个按钮

点击no增加和减少。按钮我想要的全部 修改后的数据将显示在submit.e.g中,两个座位被选中2。

代码片段是:

my xml 
private static String seatXml="<?xml version=\"1.0\" encoding=\"UTF-8\"?><seatlist>"
        +"<seatselection><seater>two</seater></seatselection>"
        +"<seatselection><seater>four</seater></seatselection>"
        +"<seatselection><seater>six</seater></seatselection>"
        +"<seatselection><seater>eight</seater></seatselection>"
        +"<seatselection><seater>ten</seater></seatselection>"
        +"</seatlist>";

这里我填写了listview

private void populateSeatList() {
    // TODO Auto-generated method stub
    seatItems=new ArrayList<HashMap<String,String>>();
    XMLParser parser=new XMLParser();
    Document doc=parser.getDomElement(seatXml);
    NodeList nl=doc.getElementsByTagName(KEY_RECORD);
    String seatName="";

    for (int i = 0; i < nl.getLength(); i++) {
        map=new HashMap<String, String>();
        Element e=(Element)nl.item(i);
        // adding each child node to HashMap key => value
        map.put(KEY_SEAT,parser.getValue(e, KEY_SEAT)); //this will fetch the seater value like 2 seater,3 seater
        map.put(KEY_SEAT_QTY,""+QTY);
        seatItems.add(map);
    }

    adapter=new SeatAdapter(this, seatItems);
    seatListView.setAdapter(adapter);
    //      makeAToast(seatName);

}

适配器类中的getview函数

int i=0;
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    View view=convertView;
    if(convertView==null)
        view=inflater.inflate(R.layout.seat_desc,null);
    TextView seatName=(TextView)view.findViewById(R.id.textView_seat_no);
    Button buttonMinus=(Button)view.findViewById(R.id.button_minus);
    Button buttonPlus=(Button)view.findViewById(R.id.Button_plus);
    final TextView seatInput=(TextView)view.findViewById(R.id.textView_seat_input);

    seatInfo=new HashMap<String, String>();
    seatInfo=data.get(position);

    seatName.setText(seatInfo.get(SeatSelectionActivity.KEY_SEAT)+" seater:");
    seatInput.setText(""+i);

    //    menuInfo.get(MenuScreenActivity.KEY_MENUNAME)
    buttonPlus.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated i stub
            i=Integer.parseInt(seatInput.getText().toString());
            if(!(i>=10))
                i++;
            seatInput.setText(""+i);
            //              seatInfo.put(SeatSelectionActivity.KEY_SEAT, seatInfo.get(SeatSelectionActivity.KEY_SEAT));
            seatInfo.put(SeatSelectionActivity.KEY_SEAT_QTY,seatInput.getText().toString());
            data.set(position, seatInfo);
            //              notifyDataSetChanged();

        }
    });

    buttonMinus.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO ,goto-generated method stub
            i=Integer.parseInt(seatInput.getText().toString());
            if(!(i<1))
                i--;
            seatInput.setText(""+i);
            //              seatInfo.put(SeatSelectionActivity.KEY_SEAT, seatInfo.get(SeatSelectionActivity.KEY_SEAT));
            seatInfo.put(SeatSelectionActivity.KEY_SEAT_QTY,seatInput.getText().toString());
            data.set(position, seatInfo);
            //              notifyDataSetChanged();
        }
    });

    return view;
}

if needed my sample project

1 个答案:

答案 0 :(得分:0)

我使用此代码来获取我所有选定的座位和它起作用了

buttonPlus.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated i stub
            i=Integer.parseInt(seatInput.getText().toString());
            if(!(i>=10))
                i++;
            seatInput.setText(""+i);

            data.get(position).put(SeatSelectionActivity.KEY_SEAT_QTY, seatInput.getText().toString());

        }
    });