在按钮上单击特定位置时增加和减少textview内的值

时间:2017-03-16 10:30:45

标签: android listview

我想在特定位置单击按钮时增加和减少textview中的值。我已经制作了自定义listview.Problem是当我点击第一项的+按钮时,数量增加1,奖金增加20但是当我点击第二项,数量直接从0增加到2,奖金直接增加到40.我希望书必须增加20,笔必须增加30,铅笔增加5,同样是减少。我是新的android plz帮帮我。谢谢你的帮助。以下是我的代码: -

order.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent">

    <LinearLayout
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:id="@+id/item_name"
       android:orientation="horizontal"
       android:layout_toRightOf="@+id/sr_no"

       >
       <TextView
           android:layout_width="150dp"
           android:layout_height="50dp"
           android:layout_toRightOf="@+id/srno"
           android:layout_below="@+id/breakfast"
           android:text="ITEM NAME"
           android:id="@+id/itemname"
           android:gravity="center"
           android:textStyle="bold"
           android:textColor="#000"
           />

   </LinearLayout>
   <LinearLayout
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:id="@+id/qtity"
       android:orientation="horizontal"
       android:layout_toRightOf="@+id/item_name"

       >
       <TextView
           android:layout_width="40dp"
           android:layout_height="50dp"
           android:layout_toRightOf="@+id/itemname"
           android:layout_below="@+id/lunch"
           android:text="QTY"
           android:id="@+id/quantity"
           android:gravity="center"
           android:textStyle="bold"
           android:textColor="#000"
           />

   </LinearLayout>
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:id="@+id/count"
        android:layout_toRightOf="@+id/qtity"

        >
        <TextView
            android:layout_width="50dp"
            android:layout_height="50dp" />

    </LinearLayout>
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/rate"
        android:orientation="horizontal"
        android:layout_toRightOf="@+id/count"

        >
        <TextView
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:layout_toRightOf="@+id/quantity"
            android:layout_below="@+id/snacks"
            android:text="PRICE"
            android:id="@+id/price"
            android:gravity="center"
            android:textStyle="bold"
            android:textColor="#000"
            />

    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/sr_no"
        android:orientation="horizontal"

        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true">
        <TextView
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:layout_below="@+id/breakfast"
            android:layout_margin="2dp"
            android:layout_marginTop="10dp"
            android:text="SR.NO"
            android:gravity="center"
            android:textStyle="bold"
            android:textColor="#000"
            android:id="@+id/srno"
            />

    </LinearLayout>

    <ListView
        android:layout_width="match_parent"
        android:layout_height="350dp"
        android:id="@+id/listdetails"
        android:scrollbars="vertical"
        android:scrollbarStyle="insideOverlay"
        android:layout_below="@+id/item_name"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true">


    </ListView>


</RelativeLayout>


customlist.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent">

    <TextView
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:id="@+id/s_no"
        android:layout_marginTop="5dp"
        android:gravity="center"
        android:textColor="#000"
        android:textStyle="bold"
        />
    <TextView
        android:layout_width="150dp"
        android:layout_height="50dp"
        android:gravity="center"
        android:textColor="#000"
        android:textStyle="bold"
        android:id="@+id/i_name"
        android:layout_marginTop="5dp"
        android:layout_toRightOf="@+id/s_no"
        />
    <TextView
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:id="@+id/qty"
        android:layout_toRightOf="@id/i_name"
        android:gravity="center"
        android:textColor="#000"
        android:textStyle="bold"
        android:layout_marginTop="5dp"
        />
    <Button
        android:layout_width="25dp"
        android:layout_height="25dp"
        android:id="@+id/pl"
        android:layout_marginTop="5dp"
        android:layout_toRightOf="@+id/qty"
        android:background="@drawable/plus1"
        />
    <Button
        android:layout_width="25dp"
        android:layout_height="25dp"
        android:id="@+id/min"
        android:layout_marginTop="3dp"
        android:layout_below="@id/pl"
        android:layout_toRightOf="@id/qty"
        android:background="@drawable/minus1"
        />

    <TextView
        android:layout_width="60dp"
        android:layout_height="50dp"
        android:id="@+id/pr"
        android:gravity="center"
        android:textColor="#000"
        android:textStyle="bold"
        android:layout_marginLeft="10dp"
        android:layout_alignBottom="@+id/min"
        android:layout_toRightOf="@+id/pl"
        android:layout_toEndOf="@+id/pl" />
</RelativeLayout>


Order.java
public class Order extends Activity {

    ListView list_item;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.order);

        list_item=(ListView)findViewById(R.id.listdetails);

        int sr[]={1,2,3};
       // int sr=1;
      //  String[] item_name={"book","pen","notebook","pendrive","rubber","scale","pencil","slate","crayons","compass box"};
        String [] itemname={"book","pen","pencil"};
        int [] quantity={0,0,0};
        //int[] price={20,20,25,25,30,30,20,10,25,10};
        int [] price={20,30,5};



            Custom_Adapter ca=new Custom_Adapter(this,sr,itemname,price,quantity);
            list_item.setAdapter(ca);
    }
}


CustomAdapter.java
public class Custom_Adapter extends BaseAdapter {
    Activity a;
   int sr [];
int quantity[];

    String itemname[];
    int price[];

    TextView qt;
    public static int count=0,count2=0,cal;


    private String data,pricedata;



    public Custom_Adapter(Activity a, int[] sr, String[] itemname, int[] price, int quantity[]) {
        this.a = a;
        this.sr = sr;
        this.itemname = itemname;
        this.price = price;
        this.quantity = quantity;

    }

    @Override
    public int getCount() {
        return itemname.length;
    }

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

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

        TextView srh,item,qt,rate;
        Button plus,minus;
    }

    @NonNull
    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final viewHolder viewHolder=new viewHolder();
        LayoutInflater li=a.getLayoutInflater();
        View view=li.inflate(R.layout.customlist,parent,false);
        viewHolder.srh=(TextView)view.findViewById(R.id.s_no);
        viewHolder.item=(TextView)view.findViewById(R.id.i_name);
        viewHolder.qt=(TextView)view.findViewById(R.id.qty);
        viewHolder.plus=(Button)view.findViewById(R.id.pl);
        viewHolder.minus=(Button)view.findViewById(R.id.min);
        viewHolder.rate=(TextView)view.findViewById(R.id.pr);




        viewHolder.srh.setText(Integer.toString(sr[position]));
        viewHolder.item.setText(itemname[position]);
        viewHolder.qt.setText(Integer.toString(quantity[position]));

        viewHolder.rate.setText(Integer.toString(price[position]));





        viewHolder.plus.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                   count+=1;
                count2+=20;
                viewHolder.qt.setText(String.valueOf(count));
                viewHolder.rate.setText(String.valueOf(count2));



            }
        });
        viewHolder.minus.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {


                if (count>0 && count2>0) {
                    count = count - 1;
                    count2-=20;
                    viewHolder.qt.setText(String.valueOf(count));
                    viewHolder.rate.setText(String.valueOf(count2));
                }

            }
        });

        return view;
    }
}

4 个答案:

答案 0 :(得分:0)

到目前为止你的问题是你的java代码问题不在xml中,所以如果你发布你的java代码我们可以帮助你

问题可能是您可能在整个列表视图数据集中重复使用模型对象

答案 1 :(得分:0)

据我所知,您的代码逻辑不正确。首先,如果您从列表中选择了书籍,则根据维护数量[]数组获得您选择的项目更新您的数量[1,0,0]如果你再次选择书籍,那么拳头数量计数增量值为1并更新数量[2,0,0]。

答案 2 :(得分:0)

你的listview中的java代码中有错误你在每一行和textview上都有按钮如果是,你必须在适配器中声明你的整数,如果它在listview的外面那么你可以在你的主类中声明它。我认为你的每个listview项都有相同的变量只是在你的适配器中初始化一个整数它将解决问题。您可以显示您的Java代码以获得进一步的帮助。希望你得到我想说的话,它可以帮助你。好运的好友!

答案 3 :(得分:0)

public class Model {
        int sr;
        String itemName;
        float price;
        float singlePiecePrice;
        int quantity;

        public Model(int sr, String itemName, float singlePiecePrice, int quantity) {
            this.sr = sr;
            this.itemName = itemName;
            this.singlePiecePrice = singlePiecePrice;
            this.quantity = quantity;
        }
    }


    public class Order extends Activity {

        ListView list_item;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.order);

            list_item = (ListView) findViewById(R.id.listdetails);

            ArrayList<Model> dataSet = new ArrayList<>();
            dataSet.add(new Model(1, "book", 0, 20));
            dataSet.add(new Model(2, "pen", 0, 30));
            dataSet.add(new Model(3, "pencil", 0, 5));

            Custom_Adapter ca = new Custom_Adapter(this, dataSet);
            list_item.setAdapter(ca);
        }
    }


    public class Custom_Adapter extends BaseAdapter {


        private ArrayList<Model> dataSet;
        private LayoutInflater layoutInflater;

        public Custom_Adapter(Context context, ArrayList<Model> dataSet) {
            layoutInflater = LayoutInflater.from(context);
            this.dataSet = dataSet;
        }

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

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

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

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

            final ViewHolder holder;
            if (convertView == null) {
                convertView = layoutInflater.inflate(R.layout.customlist, parent, false);

                holder = new ViewHolder();

                holder.plus.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                        Model model = dataSet.get(position);
                        model.quantity++;
                        model.price += model.singlePiecePrice;
                        dataSet.set(position, model);
                        holder.qt.setText(String.valueOf(model.quantity));
                        holder.rate.setText(String.valueOf(model.price));


                    }
                });
                holder.minus.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Model model = dataSet.get(position);
                        if (model.quantity > 0 && model.price > 0) {
                            model.quantity--;
                            model.price -= model.singlePiecePrice;
                            dataSet.set(position, model);
                            holder.qt.setText(String.valueOf(model.quantity));
                            holder.rate.setText(String.valueOf(model.price));
                        }

                    }
                });


            }

            return convertView;
        }

        public class ViewHolder {

            TextView srh, item, qt, rate;
            Button plus, minus;
        }

    }