OnItemClick列表错误的项目被选中?

时间:2015-12-10 13:57:59

标签: java android json

  

单击列表时,我将一些值从一个片段传递到另一个片段。当我选择说第0个项目时,我正在获取正确行的Toast消息,但是正在向另一个片段发送错误的值。另外有时会发送一个空值。根据我的代码,我认为问题可能与列表有关,但与json数据有关。这是我的列表视图代码。

 public void showJson(String json){
        final Context billsctx = getActivity();
        ParseBills pb = new ParseBills(json);
        pb.parseJSON();

        BillsCustomList bl = new BillsCustomList((Activity)billsctx,ParseBills.custInfo,ParseBills.invoiceNo,ParseBills.barcode,ParseBills.description,ParseBills.weight,
                ParseBills.rate,ParseBills.makingAmt,ParseBills.net_rate,ParseBills.itemTotal,ParseBills.vat,ParseBills.sum_total,ParseBills.bill_type,ParseBills.date);
        all_listView.setAdapter(bl);
        all_listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view,  int position, long id) {

                //displaytext.setText(ParseBills.invoiceNo[position]);
                // Setting Values for next screen
                Object obj = all_listView.getItemAtPosition(position);

                int pos = all_listView.getPositionForView(view);

                Toast.makeText(getContext(), "This is the object position "+pos, Toast.LENGTH_LONG).show();

                InvoiceNo = String.valueOf(ParseBills.invoiceNo[pos]);
                invoiceDate = ParseBills.date[pos];
                custInfo = ParseBills.custInfo[pos];
                itemVat = ParseBills.vat[pos];
                itemSum = ParseBills.sum_total[pos];
                billType = ParseBills.bill_type[pos];


                Fragment fragment = new View_InvestFragment();
                FragmentManager fragmentMg = getActivity().getSupportFragmentManager();
                FragmentTransaction fragmentTrans = fragmentMg.beginTransaction();
                Bundle args = new Bundle();
                args.putString("INVOICENO",InvoiceNo);
                args.putString("INVOICEDATE",invoiceDate);
                args.putString("CUSTINFO",custInfo);
                args.putString("ITEMVAT",itemVat);
                args.putString("ITEMSUM",itemSum);
                args.putString("BILLTYPE",billType);
                fragment.setArguments(args);
                fragmentTrans.replace(R.id.container_body, fragment,"TABFRAGMENT1");
                fragmentTrans.addToBackStack(null);
                fragmentTrans.commit();
            }
        } );
  

我认为json对象中的值不断移动。就像一个实例的第0个值将是另一个实例的第一个值。这是我的json代码。

public class ParseBills
{
    public static String[] custInfo;
    public static String[] invoiceNo;
    public static String[] barcode;
    public static String[] description;
    public static String[] weight;
    public static String[] rate;
    public static String[] makingAmt;
    public static String[] net_rate;
    public static String[] itemTotal;
    public static String[] vat;
    public static String[] sum_total;
    public static String[] bill_type;
    public static String[] date;


    public static final String JSON_ARRAY ="result";
    public static final String KEY_CUSTINFO ="custInfo";
    public static final String KEY_INVOICENO ="invoiceNo";
    public static final String KEY_BARCODE ="barcode";
    public static final String KEY_DESC ="description";
    public static final String KEY_WEIGHT ="weight";
    public static final String KEY_RATE ="rate";
    public static final String KEY_MAKING ="makingAmt";
    public static final String KEY_NRATE ="net_rate";
    public static final String KEY_TOTAL ="itemTotal";
    public static final String KEY_VAT ="vat";
    public static final String KEY_SUM ="sum_total";
    public static final String KEY_TYPE ="bill_type";
    public static final String KEY_DATE ="date";

    private JSONArray bills = null;

    private String json;

    public ParseBills(String json){
        this.json = json;
    }

    public void parseJSON(){
        JSONObject jsonObject=null;
        try {
            jsonObject = new JSONObject(json);
            bills = jsonObject.getJSONArray(JSON_ARRAY);

            custInfo = new String[bills.length()];
            invoiceNo = new String[bills.length()];
            barcode = new String[bills.length()];
            description = new String[bills.length()];
            weight = new String[bills.length()];
            rate = new String[bills.length()];
            makingAmt = new String[bills.length()];
            net_rate = new String[bills.length()];
            itemTotal = new String[bills.length()];
            vat = new String[bills.length()];
            sum_total = new String[bills.length()];
            bill_type = new String[bills.length()];
            date = new String[bills.length()];


            for(int i=0;i<bills.length();i++){
                JSONObject jo = bills.getJSONObject(i);
                custInfo[i] = jo.getString(KEY_CUSTINFO);
                invoiceNo[i] = jo.getString(KEY_INVOICENO);
                barcode[i] = jo.getString(KEY_BARCODE);
                description[i] = jo.getString(KEY_DESC);
                weight[i] = jo.getString(KEY_WEIGHT);
                rate[i] = jo.getString(KEY_RATE);
                makingAmt[i] = jo.getString(KEY_MAKING);
                net_rate[i] = jo.getString(KEY_NRATE);
                itemTotal[i] = jo.getString(KEY_TOTAL);
                vat[i] = jo.getString(KEY_VAT);
                sum_total[i] = jo.getString(KEY_SUM);
                bill_type[i] = jo.getString(KEY_TYPE);
                date[i] = jo.getString(KEY_DATE);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

}
  

以下是接收另一个片段中的值的代码。

 @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        setHasOptionsMenu(true);
        View viewInvest = inflater.inflate(R.layout.view_invest, container, false);

        String invNo = getArguments().getString("INVOICENO");
        String invDate = getArguments().getString("INVOICEDATE");
        String custInfo = getArguments().getString("CUSTINFO");
        String inVat = getArguments().getString("ITEMVAT");
        String inSum = getArguments().getString("ITEMSUM");
        String type = getArguments().getString("BILLTYPE");

  try {
            if (invNo == null)
            {
                Toast.makeText(getContext(), "This is a wrong value being sent "+invNo, Toast.LENGTH_LONG).show();
            }else {
                sendInvoiceNum(invNo);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }


        tv_invoiceNo = (TextView) viewInvest.findViewById(R.id.tv_invoiceNo);
        tv_invoicedate = (TextView) viewInvest.findViewById(R.id.tv_invDate);
        tv_custInfo = (TextView) viewInvest.findViewById(R.id.tv_invName);
        tv_vat = (TextView) viewInvest.findViewById(R.id.tv_invVat);
        tv_sum = (TextView) viewInvest.findViewById(R.id.tv_invoiceSum);
        inv_listView = (ListView) viewInvest.findViewById(R.id.invest_listView);

        tv_invoiceNo.setText(invNo);
        tv_invoicedate.setText(invDate);
        tv_custInfo.setText(custInfo);
        tv_vat.setText(inVat);
        tv_sum.setText(inSum);

        return viewInvest;
    }
  

这是我发送请求的Volley代码

  public void sendInvoiceNum(final String invNumber) throws JSONException {
        final Context context = getActivity();
        StringRequest stringRequest = new StringRequest(Request.Method.POST, INVOICE_URL,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        //Toast.makeText(context, response, Toast.LENGTH_LONG).show();
                        showInvoice(response);
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        Toast.makeText(context, error.toString(), Toast.LENGTH_SHORT).show();
                    }
                }) {
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String, String> params = new HashMap<>();
                params.put(KEY_INVOICE, invNumber);
                return params;
            }
        };
        RequestQueue rq = Volley.newRequestQueue(context);
        rq.add(stringRequest);
    }

    public void showInvoice(String json) {
        final Context context = getActivity();
        ParseInvoice pi = new ParseInvoice(json);
        pi.parseInvoice();

        InvoiceDetailsList idl = new InvoiceDetailsList((Activity) context, ParseInvoice.barcode, ParseInvoice.description, ParseInvoice.weight, ParseInvoice.rate, ParseInvoice.makingAmt
                , ParseInvoice.net_rate, ParseInvoice.itemTotal);
        final String[] barcodes = ParseInvoice.barcode;

        // Toast.makeText(context, String.valueOf(barcodes[0]), Toast.LENGTH_LONG).show();
        inv_listView.setAdapter(idl);
    }

1 个答案:

答案 0 :(得分:0)

单击时不使用此语句检索视图项的位置:

int pos = all_listView.getPositionForView(view);

只需使用OnItemClick()生成的变量“int position”来检索单击的视图项的位置。