gpt4 book ai didi

java - 列表中的 OnItemClick 选择了错误的项目?

转载 作者:太空宇宙 更新时间:2023-11-04 13:08:22 25 4
gpt4 key购买 nike

On click of a list I am passing some values from one fragment to the other.When I select say the 0th item I am getting a Toast msg for the correct row but a wrong value is being sent to the other fragment.Also sometimes a null value gets sent.According to my code I think is the problem might not be with the list but with the json data.This is my list view code.

 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();
}
} );

I think that the values within the json objects keep moving.Like the 0th value for one instance will be the 1st value for another instance.This is my json code.

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();
}
}

}

And here is the code for receiving the values in another fragment.

 @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;
}

And this is my Volley code for send request

  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);
}

最佳答案

不要使用此语句来检索单击时 View 项的位置:

int pos = all_listView.getPositionForView(view);

只需使用 OnItemClick() 生成的变量“intposition”来检索单击的 View 项的位置。

关于java - 列表中的 OnItemClick 选择了错误的项目?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34203782/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com