在按钮单击时从自定义ListView项获取数据

时间:2017-10-22 15:47:59

标签: java android listview

我想为ListView中的每个项添加一个简单的按钮。为了实现这一点,我为ListView创建了一个自定义布局和类。

XML(listview_search_result.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="match_parent"
    android:weightSum="4"
    android:orientation="horizontal">

    <TextView
        android:id="@+id/lv_sr_item"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="3"
        android:layout_marginStart="8dp"
        android:textSize="18sp"
        android:textStyle="bold" />

    <Button
        android:id="@+id/lv_sr_btn_add"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginEnd="18dp"
        android:layout_weight="1"
        android:minHeight="0dp"
        android:text="Add"
        android:textSize="12sp" />
</LinearLayout>

Class(ListViewSearchResult.java):

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListAdapter;
import android.widget.TextView;
import java.util.ArrayList;

class ListViewSearchResult extends BaseAdapter implements ListAdapter {

    private ArrayList<String> list = new ArrayList<>();
    private Context context;

    ListViewSearchResult(ArrayList<String> list, Context context) {
        this.list = list;
        this.context = context;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int pos) {
        return list.get(pos);
    }

    @Override
    public long getItemId(int pos) {
        return 0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        View view = convertView;
        if (view == null) {
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = inflater.inflate(R.layout.listview_search_result, null);
        }

        TextView listItemText = (TextView)view.findViewById(R.id.lv_sr_item);
        listItemText.setText(list.get(position));

        Button addBtn = (Button)view.findViewById(R.id.lv_sr_btn_add);
        addBtn.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                //Do something here??
            }
        });

        return view;
    }

}

以下是我在活动中应用上述代码的方法:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.searchresult_layout);

    returnList = new ArrayList<>();     //I wish to add the text from the ListView item to this list.

    ArrayList<String> list = new ArrayList<>();
    ListView lv = (ListView)findViewById(R.id.ListView_searchResultList);
    ListViewSearchResult listAdapter = new ListViewSearchResult(list, this);      //Use custom class to inflate custom layout
    lv.setAdapter(listAdapter);
    //other stuff..
}

一切正常。我可以在ListView中正确地看到每个项目,并且每个项目都有一个按钮,表示&#34;添加&#34;。 - 到目前为止一切都很好。

问题是:我需要从项目中获取文本,并在用户单击&#34;添加&#34;时,以某种方式将其返回到当前活动。按钮。

1 个答案:

答案 0 :(得分:0)

  1. 在ListViewSearchResult.java中添加一个回调接口。
  2. public interface OnAddListener {
        public void onAdd(int position, String text);
    }
    
    1. 在Adapter类(ListViewSearchResult.java)中添加一个setter方法。
    2. private OnAddListener onAddListener;
      public void setOnAddListener(OnAddListener listener) {
          this.onAddListener = listener;
      }
      
      1. 单击AddBtn时发布Click。 (ListViewSearchResult.java)
      2. @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            View view = convertView;
            if (view == null) {
                LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                view = inflater.inflate(R.layout.listview_search_result, null);
            }
        
            final TextView listItemText = (TextView)view.findViewById(R.id.lv_sr_item);
            listItemText.setText(list.get(position));
        
            Button addBtn = (Button)view.findViewById(R.id.lv_sr_btn_add);
            addBtn.setOnClickListener(new View.OnClickListener(){
                @Override
                public void onClick(View v) {
                    onAddListener.onAdd(position, listItemText.getText().toString());
                }
            });
        
            return view;
        }
        
        1. 在活动中获取onAdd事件。
        2. @Override
          protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.searchresult_layout);
          
              returnList = new ArrayList<>();     //I wish to add the text from the ListView item to this list.
          
              ArrayList<String> list = new ArrayList<>();
              ListView lv = (ListView)findViewById(R.id.ListView_searchResultList);
              ListViewSearchResult listAdapter = new ListViewSearchResult(list, this);      //Use custom class to inflate custom layout
              lv.setAdapter(listAdapter);
          
              listAdapter.setOnAddListener(new ListViewSearchResult.OnAddListener() {
                  @Override
                  public void onAdd(int position, String text) {
                      // do something ...
                  }
              });
          }