1

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

enter image description here

Michael
  • 3,093
  • 7
  • 39
  • 83

2 Answers2

1

you can not set onclicklistener for items in list ! you must set onclicklistener for each view in customAdapter

mostafa3dmax
  • 997
  • 7
  • 18
0

If you have same type of data for all 3 types follow certain development rules.

Create POJO (model) class for data. that help you in storing data in structured manner.

InvoiceData.java

public class InvoiceData {

    private String Id;
    private String EntryNo;
    private String Company;
    private String Date;
    private String PaymentMode;
    private String Amount;
    private String Type; // "ALL", "Income", "Expense"
    private String ApprovalDate;
    private String GeneralNotes;

    public String getId() {
        return Id;
    }

    public void setId(String id) {
        Id = id;
    }

    public String getEntryNo() {
        return EntryNo;
    }

    public void setEntryNo(String entryNo) {
        EntryNo = entryNo;
    }

    public String getCompany() {
        return Company;
    }

    public void setCompany(String company) {
        Company = company;
    }

    public String getDate() {
        return Date;
    }

    public void setDate(String date) {
        Date = date;
    }

    public String getPaymentMode() {
        return PaymentMode;
    }

    public void setPaymentMode(String paymentMode) {
        PaymentMode = paymentMode;
    }

    public String getAmount() {
        return Amount;
    }

    public void setAmount(String amount) {
        Amount = amount;
    }

    public String getType() {
        return Type;
    }

    public void setType(String type) {
        Type = type;
    }

    public String getApprovalDate() {
        return ApprovalDate;
    }

    public void setApprovalDate(String approvalDate) {
        ApprovalDate = approvalDate;
    }

    public String getGeneralNotes() {
        return GeneralNotes;
    }

    public void setGeneralNotes(String generalNotes) {
        GeneralNotes = generalNotes;
    }
}

Set data like this with model

final ArrayList<InvoiceData> invoiceListData = new ArrayList<>();

JSONObject jsonObject1;

InvoiceData rowObject;

for (int i = 0; i < invoices.length(); i++) {

    jsonObject1 = invoices.getJSONObject(i);

    rowObject = new InvoiceData();

    rowObject.setId(jsonObject1.getString("ID"));
    rowObject.setEntryNo(jsonObject1.getString("EntryNo"));
    rowObject.setCompany(jsonObject1.getString("Company"));
    rowObject.setDate(jsonObject1.getString("Date"));
    rowObject.setPaymentMode(jsonObject1.getString("PaymentMode"));
    rowObject.setAmount(jsonObject1.getString("Amount"));
    rowObject.setApprovalDate(jsonObject1.getString("ApprovalDate"));
    rowObject.setGeneralNotes(jsonObject1.getString("GeneralNotes"));
    rowObject.setType("Expense");

    invoiceListData.add(rowObject);
}

adapter = new CustomAdapter(getContext(), invoiceListData, Common.PREVIOUSPAYMENTS);

invoiceList.setAdapter(adapter);

And to set on item click listener on Listview (Make sure it is listview b'z recycler view have no such method)

invoiceList.setOnItemClickListener(new OnItemClickListener()
{
    @Override 
    public void onItemClick(AdapterView<?> arg0, View arg1,int position, long arg3)
    { 
        InvoiceData rowObject = adapter.getItem(position);

        if(rowObject != null) {

            if(rowObject.getType().equals("Expense")) {
                // write different code for Expense here

            } else if(rowObject.getType().equals("Income")) {
                // write different code for Income here
            }


        }

    }
});

Hope this will help...

Wasim K. Memon
  • 5,979
  • 4
  • 40
  • 55