Search code examples
javaandroidandroid-arrayadapteronclicklistener

How to start an Activity in onClick method from ArrayAdapter with custom rows


So I wrote a custom ArrayAdapter to have a ListView populate with custom row data, and also to set the OnClickListener to each row. I'm facing two major problems here:

  1. the specified Activity (InvoiceActivity) does not start up on click.
  2. The reaction time on clicks when I only output the logmessage (and comment the intent and startActivity part) is really slow.

For now there are only 3 rows of dummy data in the ListView. I would be grateful for any help here, thanks!

public class InvoiceListAdapter extends ArrayAdapter<InvoiceShort> {

public InvoiceListAdapter(Context context, ArrayList<InvoiceShort> invoices) {
    super(context, R.layout.list_elem, invoices);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    final InvoiceShort invoice = getItem(position);
    if (convertView == null) {
        convertView = LayoutInflater.from(getContext()).inflate(R.layout.list_elem, parent, false);
        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(final View parent) {

                Log.d("item click", "invoice id: " + invoice.getId());
                Intent intent = new Intent(getContext(), InvoiceActivity.class);
                intent.putExtra("id", invoice.getId());
                getContext().startActivity(intent);
            }
        });
    }

    // Lookup view for data population
    TextView date = (TextView) convertView.findViewById(R.id.textDate);
    TextView total = (TextView) convertView.findViewById(R.id.textTotal);
    CheckBox paid = (CheckBox) convertView.findViewById(R.id.checkBoxPaid);

    // populate data
    DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
    date.setText(formatter.format(invoice.getDate()));
    total.setText("€ " + new Long(invoice.getTotal()).toString());
    paid.setChecked(invoice.isPaid());

    return convertView;
    }
}

EDIT: Here is the xml for one row

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:layout_centerHorizontal="false"
    android:orientation="horizontal">

    <CheckBox
        android:id="@+id/checkBoxPaid"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:checked="true"
        android:elevation="0dp"
        android:inputType="none" />

    <EditText
        android:id="@+id/textDate"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="5"
        android:background="@null"
        android:ems="10"
        android:focusable="false"
        android:inputType="none"
        android:text="2017-12-30" />

    <EditText
        android:id="@+id/textTotal"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="2"
        android:background="@null"
        android:ems="10"
        android:focusable="false"
        android:inputType="none"
        android:text="10€"
        android:textAlignment="viewStart" />

</LinearLayout>

Solution

  • I have found the solution, problem here is you have edittext in your xml but your fetching it as Textview, you can change textview to edittext in list_elem layout your click listener will work . Otherwise you can change textview to edittext in your customAdapter and set two click listener for edittext as below to make it work

    date.setOnClickListener(new View.OnClickListener() {
    
                @Override
                public void onClick(View v) {
                    View parent = (View) v.getParent();
                    parent.performClick();
                }
            });
            total.setOnClickListener(new View.OnClickListener() {
    
                @Override
                public void onClick(View v) {
                    View parent = (View) v.getParent();
                    parent.performClick();
                }
            });