如何从每行listview中的一个开始在计数器中获得行方式增量

时间:2016-07-20 09:41:49

标签: android listview

我想根据数量计算每个项目的行总数。我的问题是我想从1开始每行增加但是如果我在第一行增加例如从1到5然后在下一行它从6开始,但我希望它从1开始。我怎么解决这个问题?

代码是:

OrderScreenActivity.java

package com.example.veer.quickpos.activity;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.example.veer.quickpos.R;
import com.example.veer.quickpos.adapter.ListAdapter;
import com.example.veer.quickpos.adapter.ListItemAdapter;
import com.example.veer.quickpos.utility.Utils;
import com.example.veer.quickpos.utils.app.Constants;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by Veer on 12/07/2016.
 */
public class OrderScreenActivity extends  BaseActivity {
    public static String CmpId, GroupID, GroupName, UnderGroupId, ParentGroupName,ItemID,ItemName,CategoryID,SellingPrice;
    public static ArrayList<String> CmpIdList, GroupIDList, GroupNameList, UnderGroupIdList, ParentGroupNameList;
    public static LinearLayout ll;
    public EditText edtitem;
    public ListView lstitem;
    public ListView listItemqty;
    public static String item;
    ListAdapter adapter;
    public int total;
    ListItemAdapter adapter2;
    public static ArrayList<String> nameList,priceLIst,ItemIDList,ItemNameList,CategoryIDList,SellingPriceList,qtyList;
    ProgressDialog pDialog;
    /**
     * ATTENTION: This was auto-generated to implement the App Indexing API.
     * See https://g.co/AppIndexing/AndroidStudio for more information.
     */


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_order);
        lstitem = (ListView)findViewById(R.id.lstitem);
        edtitem = (EditText)findViewById(R.id.edtitem);
        listItemqty = (ListView)findViewById(R.id.listviewItem);

        makeJsonStringRequest();



    }

    public void makeJsonStringRequest() {


        String URL = Utils.BASE_URL + Utils.GET_GROUP + Utils.AUTH_KEY;
        Log.e("URL =>", URL);

        StringRequest stringRequest = new StringRequest(Request.Method.GET, URL,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        try {
                            Log.i("Response", response);
                            JSONArray mResponse = new JSONArray(response);


                            CmpIdList = new ArrayList<String>();
                            GroupIDList = new ArrayList<String>();
                            GroupNameList = new ArrayList<String>();
                            UnderGroupIdList = new ArrayList<String>();
                            ParentGroupNameList = new ArrayList<String>();

                            if (mResponse.length() != 0) {
                                for (int i = 0; i < mResponse.length(); i++) {
                                    JSONObject obj = mResponse.optJSONObject(i);

                                    CmpId = obj.optString("CmpId");
                                    GroupID = obj.optString("GroupID");
                                    GroupName = obj.optString("GroupName");
                                    UnderGroupId = obj.optString("UnderGroupId");
                                    ParentGroupName = obj.optString("ParentGroupName");


                                    Log.d("CmpId", "" + CmpId);
                                    Log.d("GroupID", "" + GroupID);
                                    Log.d("GroupName", "" + GroupName);
                                    Log.d("UnderGroupId", "" + UnderGroupId);
                                    Log.d("ParentGroupName", "" + ParentGroupName);

                                    CmpIdList.add(CmpId);
                                    GroupIDList.add(GroupID);
                                    GroupNameList.add(GroupName);
                                    UnderGroupIdList.add(UnderGroupId);
                                    ParentGroupNameList.add(ParentGroupName);
                                    /*GetGodowns getGodowns = new GetGodowns();
                                    getGodowns.setGoDownId(_id);
                                    getGodowns.setName(_name);
                                    getGodowns.setDate(CurrentDate);
                                    getGodowns.setSyncData(_Sync);
                                    listGetGodowns.add(getGodowns);*/

                                }

                                for (int i = 0; i < CmpIdList.size(); i++) {
                                    ll = (LinearLayout) findViewById(R.id.llgroup);
                                    ll.setOrientation(LinearLayout.HORIZONTAL);
                                    Button txv = new Button(OrderScreenActivity.this);
                                    txv.setWidth(290);
                                    txv.setHeight(180);
                                    txv.setTextColor(getResources().getColor(R.color.darksky));
                                    txv.setText(GroupNameList.get(i));
                                    ll.addView(txv);
                                    txv.setTextSize(15);
                                    txv.setOnClickListener(new View.OnClickListener() {
                                        @Override
                                        public void onClick(View v) {
                                            Log.d("in onclick:","");
                                            item = edtitem.getText().toString();
                                            Log.d("value of edittext text:",""+item);
                                            makeJsonStringRequestItem(item);

                                        }
                                    });
                                    Log.d("Id of textview :", "" + txv.getId());


                                }

                                //myDbHelper.InsertGoDown(listGetGodowns);

                                // bindSpinnerData();
                                hideProgress();


                                // myDbHelper.InsertGoDown(listGetGodowns);

                                // bindSpinnerData();
                                // hideProgress();

                            }

                            hideProgress();
                            Log.e(Constants.TAG, "GetTill response => " + mResponse.toString(4));

                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        hideProgress();
                        Toast.makeText(OrderScreenActivity.this, "Error in fetching data, PLease try again", Toast.LENGTH_LONG).show();
                        Log.v("Error String Request", "" + error.toString());
                    }
                }) {
            @Override
            protected Map<String, String> getParams() {
                return new HashMap<>();
            }

        };

        RequestQueue requestQueue = Volley.newRequestQueue(this);
        requestQueue.add(stringRequest);
        showProgress();
    }
    public void makeJsonStringRequestItem(String Item) {


        Log.d("item group id in item api:",""+GroupID);
        String URL = Utils.BASE_URL + Utils.GET_ITEMS + Utils.AUTH_KEY +"&ItemName="+ Item +"&GroupId=0";
        Log.e("URL =>", URL);

        StringRequest stringRequest = new StringRequest(Request.Method.GET, URL,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        try {
                            Log.i("Response", response);
                            JSONArray mResponse = new JSONArray(response);


                            CmpIdList = new ArrayList<String>();
                            ItemIDList = new ArrayList<String>();
                            ItemNameList = new ArrayList<String>();
                            CategoryIDList = new ArrayList<String>();
                            SellingPriceList = new ArrayList<String>();

                            if (mResponse.length() != 0) {
                                for (int i = 0; i < mResponse.length(); i++) {
                                    JSONObject obj = mResponse.optJSONObject(i);

                                    CmpId = obj.optString("CmpId");
                                    ItemID = obj.optString("ItemID");
                                    ItemName = obj.optString("ItemName");
                                    CategoryID = obj.optString("CategoryID");
                                    SellingPrice = obj.optString("SellingPrice");


                                    Log.d("CmpId", "" + CmpId);
                                    Log.d("ItemID", "" + ItemID);
                                    Log.d("ItemName", "" + ItemName);
                                    Log.d("CategoryID", "" + CategoryID);
                                    Log.d("SellingPrice", "" + SellingPrice);

                                    CmpIdList.add(CmpId);
                                    ItemIDList.add(ItemID);
                                    ItemNameList.add(ItemName);
                                    CategoryIDList.add(CategoryID);
                                    SellingPriceList.add(SellingPrice);


                                    /*GetGodowns getGodowns = new GetGodowns();
                                    getGodowns.setGoDownId(_id);
                                    getGodowns.setName(_name);
                                    getGodowns.setDate(CurrentDate);
                                    getGodowns.setSyncData(_Sync);
                                    listGetGodowns.add(getGodowns);*/

                                }
                                Log.d("Itemnamelist value:",ItemNameList.toString());
                                Log.d("SellingPriceList value:",SellingPriceList.toString());
                                adapter = new ListAdapter(OrderScreenActivity.this,ItemNameList,SellingPriceList);
                                lstitem.setAdapter(adapter);
                                adapter2 = new ListItemAdapter(OrderScreenActivity.this,ItemNameList,SellingPriceList);
                                listItemqty.setAdapter(adapter2);


                                for (int i = 0; i < CmpIdList.size(); i++) {
                                    ll = (LinearLayout) findViewById(R.id.llgroup);
                                    ll.setOrientation(LinearLayout.HORIZONTAL);
                                    Button txv = new Button(OrderScreenActivity.this);
                                    txv.setWidth(290);
                                    txv.setHeight(180);
                                    txv.setTextColor(getResources().getColor(R.color.darksky));
                                    txv.setText(GroupNameList.get(i));
                                    ll.addView(txv);
                                    txv.setTextSize(15);
                                    txv.setOnClickListener(new View.OnClickListener() {
                                        @Override
                                        public void onClick(View v) {

                                        }
                                    });
                                    Log.d("Id of textview :", "" + txv.getId());

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


                                        }
                                    });
                                }

                                //myDbHelper.InsertGoDown(listGetGodowns);

                                // bindSpinnerData();
                                hideProgress();


                                // myDbHelper.InsertGoDown(listGetGodowns);

                                // bindSpinnerData();
                                // hideProgress();

                            }

                            hideProgress();
                            Log.e(Constants.TAG, "Getgroup item response => " + mResponse.toString(4));

                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        hideProgress();
                        Toast.makeText(OrderScreenActivity.this, "Error in fetching data, PLease try again", Toast.LENGTH_LONG).show();
                        Log.v("Error String Request", "" + error.toString());
                    }
                }) {
            @Override
            protected Map<String, String> getParams() {
                return new HashMap<>();
            }

        };

        RequestQueue requestQueue = Volley.newRequestQueue(this);
        requestQueue.add(stringRequest);
        showProgress();
    }
    private void showProgress() {
        pDialog = new ProgressDialog(OrderScreenActivity.this);
        pDialog.setMessage("Wait a moment");
        pDialog.setCancelable(false);
        pDialog.show();


    }

    private void hideProgress() {
        if (null != pDialog) {
            pDialog.dismiss();
        }
    }


}

ListItemAdapter

package com.example.veer.quickpos.adapter;

import android.app.Activity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.example.veer.quickpos.R;

import java.util.ArrayList;


public class ListItemAdapter extends BaseAdapter {
    Activity context;
    public ArrayList<String> NameList;
    public ArrayList<String> PriceList;
    public ArrayList<String> totalList;
    public  int flag;
    public static int qty;
    public static ArrayList<String> qtyList;
    public  int total,grandtotal;


    public ListItemAdapter(Activity a, ArrayList<String> nameList,ArrayList<String> plist) {
        this.context = a;
        this.NameList = nameList;
        this.PriceList = plist;

    }
    /*private view holder class*/
    class ViewHolder {

        public TextView txtname,txtprice,txttotal;
        public TextView txtqty;
        public Button btnplus, btnminus;
    }

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

        final LayoutInflater mInflater = (LayoutInflater)
                context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.activity_item_qty, null);
            holder = new ViewHolder();
            holder.btnplus = (Button) convertView.findViewById(R.id.btnplus);
            holder.btnplus.setTag(position);
            holder.btnminus = (Button) convertView.findViewById(R.id.btnminus);
            holder.txtname = (TextView) convertView.findViewById(R.id.item);
            holder.txtqty = (TextView) convertView.findViewById(R.id.itemqty);
            holder.txtqty.setTag(R.id.itemqty,position);

            holder.txtprice = (TextView) convertView.findViewById(R.id.txtprice);


            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        qtyList = new ArrayList<String>();
        Log.d("Total row:",""+getCount());
        Log.d("current position:",""+position);
        Log.d("Item name at position:", "" + position + NameList.get(position));
        holder.txtname.setText(NameList.get(position));
        holder.txtprice.setText(PriceList.get(position));
        holder.txtqty.setText(String.valueOf(flag));
       // qty = Integer.parseInt(holder.txtqty.getText().toString());
       // qtyList.add(String.valueOf(qty));

        qty=1;
        Log.d("value of qty before button plus click:",""+qty);
        Log.d("value of flag before button plus click:",""+flag);
       // final int pos=(Integer)convertView.getTag();

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

                int total = 0;
                flag = qty;
                Log.d("value of flag in button plus :",""+flag);
                int itempos = holder.txtqty.getId();
                holder.txtqty.setText(String.valueOf(flag));
                Log.d("value of textqty in button plus :",""+holder.txtqty.getText().toString());
                int pos=(Integer)v.getTag();


                total = total + (Integer.parseInt(PriceList.get(pos))* (Integer.parseInt(holder.txtqty.getText().toString())));
                Log.d("value of total in button plus :",""+total);
                flag++;



            }
        });


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

                if(qty>0) {
                    qty = qty - 1;
                    Log.d("Value of qty in minus button:", "" + qty);
                    holder.txtqty.setText(String.valueOf(qty));
                    total = total + (Integer.parseInt(PriceList.get(position)) * (qty));
                    Log.d("total is:", "" + total);

                }


            }
        });



        return convertView;
    }

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

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

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

在适配器中我在btnplus点击事件中为计数器做了代码。我想在textview txtqty的每一行中设置数量1,并且在每行中从1开始增量并根据数量我将得到总数但是如果在第一行我设置数量5从下一个加号按钮增加它从6开始但是我希望它从第一行的1开始我如何解决它?

0 个答案:

没有答案