我有一个微调器包含三个选项" ALL"," PAYMENTS"和"费用"。这些将显示3个列表视图。 " ALL"选项将显示" PAYMENT"和"费用"。
我的问题是我想为这两个列表添加setOnItemClickListener
。请参阅截图。每当我点击每个列表时,我都会传递某些值。每当我点击付款中的列表时,它将转到paymentdetails活动,每当点击列表中的费用时,它将转到费用明细活动。这应该与" ALL"相同。每个列表有三种不同的适配器(即ALL,PAYMENT和费用)。代码如下。
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);
}
});
答案 0 :(得分:1)
您无法为列表中的项目设置onclicklistener!您必须为customAdapter中的每个视图设置onclicklistener
答案 1 :(得分:0)
如果所有3种类型的数据类型相同,请遵循某些开发规则。
为数据创建POJO(模型)类。它可以帮助您以结构化的方式存储数据。
<强> 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;
}
}
使用模型
设置这样的数据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);
并在Listview上设置项目点击监听器(确保它是listview b'z recycleler视图没有这样的方法)
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
}
}
}
});
希望这会有所帮助......