代码之家  ›  专栏  ›  技术社区  ›  fxvdh

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

  •  1
  • fxvdh  · 技术社区  · 7 年前

    ArrayAdapter 有一个 ListView OnClickListener 到每行。我在这里面临两个主要问题:

    1. 指定的活动( InvoiceActivity )单击时不会启动。
    2. 当我只输出日志消息(并评论意图和开始触觉部分)时,点击的反应时间非常慢。

    目前,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 回复  |  直到 7 年前
        1
  •  4
  •   user6457240 user6457240    7 年前

    我已经找到了解决方案,这里的问题是,您的xml中有edittext,但您将其作为Textview获取,您可以在list\u 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();
                }
            });
    
        2
  •  0
  •   Sharath kumar    7 年前

    使用视图上下文启动活动

    Intent intent = new Intent(convertView.getContext(), InvoiceActivity.class);
    intent.putExtra("id", invoice.getId());
    convertView.getContext().startActivity(intent);
    
        3
  •  0
  •   Ghulam Moinul Quadir    7 年前

        4
  •  0
  •   Nikhil Jadhav    7 年前

    尝试如下修改构造函数:-

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

    本部分:-

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

           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);
            }
        });`
    
        5
  •  0
  •   fxvdh    7 年前

    所以我终于找到了 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);
    

    并实施 OnItemClickedListener 对于MainActivity内的ListView