I have a spinner contains three options "ALL","PAYMENTS" and "EXPENSES". These will show 3 listviews. "ALL" option will show the mixture of both "PAYMENT" and "EXPENSES".
My problem is I want to add setOnItemClickListener
for both of the lists. Please refer the screenshot. I will pass certain values whenever I click on each list. Whenever I click on list in payments, it will go to paymentdetails activity, and whenever click on list in expense it will go to expense details activity. This should be same from "ALL". There are three different adapter for each list (i.e., ALL, PAYMENT and expense). The code is below.
try {
final JSONArray invoices = new JSONArray(common.json);
if (invoices.length() == 0) {
(rootView.findViewById(R.id.no_items)).setVisibility(View.VISIBLE);
return;
}
final ArrayList<String[]> invoiceListData = new ArrayList<>();
for (int i = 0; i < invoices.length(); i++) {
JSONObject jsonObject1 = invoices.getJSONObject(i);
String[] data = new String[9];
data[0] = jsonObject1.getString("ID");
data[1] = jsonObject1.getString("EntryNo");
data[2] = jsonObject1.getString("Company");
data[3] = jsonObject1.getString("Date");
data[4] = jsonObject1.getString("PaymentMode");
data[5] = jsonObject1.getString("Amount");
data[6] = jsonObject1.getString("Type");
data[7] = jsonObject1.getString("ApprovalDate");
data[8] = jsonObject1.getString("GeneralNotes");
invoiceListData.add(data);
}
adapter = new CustomAdapter(getContext(), invoiceListData, Common.PREVIOUSPAYMENTS);
invoiceList.setAdapter(adapter);
(rootView.findViewById(R.id.list_card)).setVisibility(View.VISIBLE);
final ArrayList<String[]>PaymentListData=new ArrayList<>();
final ArrayList<String[]>ExpenseListData=new ArrayList<>();
for (int i = 0; i < invoiceListData.size(); i++) {
if (invoiceListData.get(i)[6].equals("Payment")) {
PaymentListData.add(invoiceListData.get(i));
invoiceList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent approvalDetailsIntent = new Intent(getContext(),ApprovalDetails.class);
approvalDetailsIntent.putExtra(Common.APPROVALID,invoiceListData.get(position)[0]);
approvalDetailsIntent.putExtra(Common.ENTRYNO,invoiceListData.get(position)[1]);
approvalDetailsIntent.putExtra(Common.PAYMENT_MODE,invoiceListData.get(position)[2]);
approvalDetailsIntent.putExtra(Common.PAYMENT_DATE,invoiceListData.get(position)[3]);
approvalDetailsIntent.putExtra(Common.AMOUNT,invoiceListData.get(position)[4]);
approvalDetailsIntent.putExtra(Common.COMPANY_DETAILS,invoiceListData.get(position)[5]);
approvalDetailsIntent.putExtra(Common.GENERAL_NOTES,invoiceListData.get(position)[8]);
startActivity(approvalDetailsIntent);
}
});
} else if(invoiceListData.get(i)[6].equals("Expense")) {
(rootView.findViewById(R.id.list_card)).setVisibility(View.VISIBLE);
invoiceList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent approvalDetailsIntent = new Intent(getContext(),ApprovalExpenseDetails.class);
approvalDetailsIntent.putExtra(Common.APPROVALID,ExpenseListData.get(position)[0]);
approvalDetailsIntent.putExtra(Common.REFNO,ExpenseListData.get(position)[1]);
startActivity(approvalDetailsIntent);
}
});
}
}
Padapter = new CustomAdapter(getContext(),PaymentListData,Common.PREVIOUSPAYMENTS); //Global variable
invoiceList.setAdapter(Padapter);
(rootView.findViewById(R.id.list_card)).setVisibility(View.VISIBLE);
Eadapter = new CustomAdapter(getContext(),ExpenseListData,Common.PREVIOUSPAYMENTS);
invoiceList.setAdapter(adapter);
(rootView.findViewById(R.id.list_card)).setVisibility(View.VISIBLE);
// Values passing for payments only.I dont know whether invoiceListData.get()..is correct or not.
invoiceList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent approvalDetailsIntent=new Intent(getContext(),ApprovalDetails.class);
approvalDetailsIntent.putExtra(Common.APPROVALID,invoiceListData.get(position)[0]);
approvalDetailsIntent.putExtra(Common.ENTRYNO,invoiceListData.get(position)[1]);
approvalDetailsIntent.putExtra(Common.PAYMENT_MODE,invoiceListData.get(position)[2]);
approvalDetailsIntent.putExtra(Common.PAYMENT_DATE,invoiceListData.get(position)[3]);
approvalDetailsIntent.putExtra(Common.AMOUNT,invoiceListData.get(position)[4]);
approvalDetailsIntent.putExtra(Common.COMPANY_DETAILS,invoiceListData.get(position)[5]);
approvalDetailsIntent.putExtra(Common.GENERAL_NOTES,invoiceListData.get(position)[8]);
startActivity(approvalDetailsIntent);
}
});
//values passing for expense only
invoiceList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent approvalDetailsIntent = new Intent(getContext(),ApprovalExpenseDetails.class);
approvalDetailsIntent.putExtra(Common.APPROVALID,ExpenseListData.get(position)[0]);
approvalDetailsIntent.putExtra(Common.REFNO,ExpenseListData.get(position)[1]);
startActivity(approvalDetailsIntent);
}
});