如何使用自定义行从ArrayAdapter启动onClick方法中的Activity

时间:2017-09-19 12:26:52

标签: java android android-arrayadapter onclicklistener

因此,我编写了一个自定义ArrayAdapter,以便ListView填充自定义行数据,并将OnClickListener设置为每一行。我在这里遇到两个主要问题:

  1. 指定的活动(InvoiceActivity)在点击时无法启动。
  2. 当我只输出logmessage(并注释intent和startActivity部分)时,点击的反应时间非常慢。
  3. 目前ListView中只有3行虚拟数据。 我很感激在这里提供任何帮助,谢谢!

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

    编辑:这是一行的xml

    <?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>
    

5 个答案:

答案 0 :(得分:4)

我找到了解决方案,这里的问题是你的xml中有edittext但你把它作为Textview获取,你可以在list_elem布局中将textview更改为edittext,你的点击监听器就可以了。否则,您可以在customAdapter中将textview更改为edittext,并为edittext设置两个单击侦听器,如下所示,以使其正常工作

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

答案 1 :(得分:0)

尝试修改您的构造函数,如下所示: -

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

并使用mContext而不是getContext(); 这部分: -

  @Override
        public void onClick(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);
        }

尝试在

之外编写onclick listner
  

if else

       convertView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(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);
        }
    });`

答案 2 :(得分:0)

使用视图上下文启动活动

Intent intent = new Intent(convertView.getContext(), InvoiceActivity.class);
intent.putExtra("id", invoice.getId());
convertView.getContext().startActivity(intent);

答案 3 :(得分:0)

在您设置适配器并显示ListView的Activity类中实现OnClickListener。

答案 4 :(得分:0)

所以我终于找到了solution,简而言之就是将这个监听器设置为ListView的一行内的所有视图:

    /** A definition of a listener that does nothing but to let the click be handled by the parent **/
    View.OnClickListener listener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            ((ListView) parent).performItemClick(v, position, 0);
        }
    };

    date.setOnClickListener(listener);
    total.setOnClickListener(listener);
    paid.setOnClickListener(listener);

还要在MainActivity中实现ListView的OnItemClickedListener