更新列表视图中的单个项目数据在添加另一个项目后不会受到影响

时间:2015-05-20 14:09:35

标签: android android-listview listviewitem

每个具有名称,价格,数量= 1.i等字段的项目都要在项目数量字段中更改当前项目的数量ex:1到30。它正在改变但是何时添加另一个项目列表查看列表中的总项目查看数量字段中的更改为1。请帮帮我如何修改单个项目而不更改其他项目。这里使用自定义适配器。

My Custom Adapter:
-------------------   

    public class    Item Adapter    extends   Base Adapter {
    private Array List<Hash Map<String, String>> val;
    private Context context;
    static View Holder holder 1;

    Layout Inf later inf later=null;
    static class View Holder{
        public Text View p name;
        public Edit Text quantity;
        public Text View price;
        public Text View total p r c;


    }
public Item Adapter(Context context, Array List<Hash Map<String, String>> val){
        ///super(context, R.layout.row item, list);
        this.context=context;
        this.val=val;
             inf later=(Layout Inf later)context.get System Service(Context.LAYOUT_INF LATER_SERVICE);


    }

    @Override
    public int get Count() {
        //-generated method stub
        return val.size();
    }

    @Override
    public Object get Item(int position) {
        // Auto-generated method stub
        return position;
    }

    @Override
    public long get Item Id(int position) {
        //  Auto-generated method stub
        return position;
    }

    @Override
    public View get View(int position, View convert View, View Group parent) {
        //  Auto-generated method stub
        View row View=convert View;
        if(convert View==null){
            row View=inf later.inflate(R.layout.row item, null);
            View Holder holder=new View Holder();
            holder.p name=(Text View)row View.find View By Id(id.text View 1);
        holder.quantity=(Edit Text)row View. find View By Id(id.edit Text 1);
            holder. p r i c=(Text View)row View.find View By Id(id.p);
    holder. total p r c=(Text View) row View. find View By Id(id.total p r c);



        row View.set Tag(holder);

        }
         holder 1=new View Holder();
        holder 1=(View Holder) row View. get Tag();
        Hash Map<String, String> data=new Hash Map<String, String>();
        data=val.get(position);
        //String[] s s={data};

        holder 1. p name.set Text(data.get(Capture Activity. D t as));
        holder 1. quantity.set Text(data.get(Capture Activity. F w at));
        holder 1. p r i c. set Text(data.get(Capture Activity.A m ts));

        return row View;
    }



    public void clear data(){
        val.clear();
    }

}

Capture Activity :
------------------------

 val=new Array List<Hash Map<String, String>>();
    db=open Or Create Database("MB B S DB", Context.MODE_PRIVATE, null);
    db.exec SQL("CREATE TABLE IF NOT EXISTS store m b(Product Id VAR CHAR,Product Name VAR CHAR,Product Price VAR CHAR,Product Offer VAR CHAR,Product Discount VAR CHAR,Product Quantity VAR CHAR);");


    Cursor c=db.raw Query("SELECT * FROM store m b WHERE Product Id ='"+raw Result.get Text()+"'", null);
                     if(c.get Count()==0)
                        {
Toast.make Text(get Application Context(), "Error", Toast.LENGTH_SHORT).show();
                            return;
                        }

                    if(c. move To First())
                    {
                        //r no.set Text(c.get String(0));
                    Hash Map<String, String> map=new Hash Map<String, String>();

                        map.put(D t as, c.get  String(1));
                        map.put(A m ts, c.get String(2));
                        map.put(F w at, c.get String(5));
                            val.add(0,map);

                        a d d a p();

                    }

                    else
                    {
                        Toast.make Text(get Application Context(), "no", Toast.LENGTH_SHORT).show();


                    }

                }


            } );
     private void a d d a p() {
    //  Auto-generated method stub
      adapter=new Item Adapter(get Application Context(), val);
      adapter.notify Data Set Changed();
      l v.set Adapter(adapter);
      l v.smooth Scroll To Position(0);
    show Number Of Items();


}
  private void show Number Of Items() {
         int count = adapter.get Count();
         item count.set Text(String.value Of(count));
    }


row item. layout:
---------------------

    <? x ml version="1.0" encoding="u t f-8"?>

<Relative 
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:descendant Focus ability="blocks Descendants" >

    <Text View
        android:id="@+id/text View 1"
        android:layout_width="match_parent"
        android:layout_height="30 d p"
        android:text Size="20 s p" 
        android:layout_margin Bottom="5 d p"/>

    <Text View
        android:id="@+id/text View 2"
        android:layout_width="wrap_content"
        android:layout_height="30 d p"
        android:layout_below="@+id/text View 1"
        android:gravity="bottom|start"
        android:text="Q n t y"
        android:text Color="#000000"
        android:text Size="18 s p" />

    <Edit Text
        android:id="@+id/edit Text 1"
        android:layout_width="35 d p"
        android:layout_height="30 d p"
        android:layout_align Right="@+id/text View 6"
        android:layout_below="@+id/text View 1"
        android:layout_margin Left="2 d p"
        android:layout_to Right Of="@+id/text View 2"
        android:background="@ draw able /rectangle"
        android:ems="10"
        android:gravity="center"
        android:input Type="number"
        android:text Size="15 s p"
        android:text Style="bold" >

    </Edit Text>

    <Text View
        android:id="@+id/text View 3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_align Bottom="@+id/edit Text 1"
        android:layout_below="@+id/text View 1"
        android:layout_margin Right="10 d p"
        android:layout_margin Left="2 d p"
        android:layout_to Right Of="@+id/edit Text 1"
        android:gravity="bottom|end"
        android:text="X"
        android:text Style="bold" />

    <Text View
        android:id="@+id/text View 4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_align Bottom="@+id/edit Text 1"
        android:layout_align Left="@+id/total p r c"
        android:layout_below="@+id/text View 1"
        android:gravity="bottom|start"
        android:text="Rs."
        android:text Color="#000000"
        android:text Size="20 s p" />

    <Text View
        android:id="@+id/p"
        android:layout_width="wrap_content"
        android:layout_height="30 d p"
        android:layout_align Bottom="@+id/text View 4"
        android:layout_align Parent Right="true"
        android:layout_below="@+id/text View 1"
        android:layout_to Right Of="@+id/text View 4"
        android:gravity="bottom|center|start"
        android:text Color="#000000"
        android:text Size="20 s p"
        android:text  Style="bold" />

    <Text View
        android:id="@+id/text View 6"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_align Parent Left="true"
        android:layout_below="@+id/text View 2"
        android:layout_margin Top="5 d p"
        android:text="Total Price"
        android:text Size="20 s p" />

    <Text View
        android:id="@+id/total p r c"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_align Bottom="@+id/text View 6"
        android:layout_align Parent Right="true"
        android:layout_below="@+id/text View 4"
        android:layout_to Right Of="@+id/text View 3"
        android:gravity="bottom|start"
        android :text Color="#000000"
        android  :text Size="20 s p" />

</Relative Layout>'

finally i want to change quantity of selected list View item, only selected item want to change not remaining items in list view.and it will not change  when new item is added to list view.

0 个答案:

没有答案