如何在Fragment中使用Custom Arrayadapter注册Listview上的点击

时间:2014-08-06 01:18:33

标签: android listview android-fragments onclicklistener

我有一个自定义的ArrayAdapter:

public class EditArrayAdapter extends ArrayAdapter<String> {
    private final Activity context;

    public ArrayList<String> strArrC;
    public ArrayList<String> strArrT;

    public EditArrayAdapter(Activity context, ArrayList<String> web,
            ArrayList<String> imageId) {
        super(context, R.layout.list_single, web);
        this.context = context;
        this.strArrC = web;
        this.strArrT = imageId;
    }

    @Override
    public View getView(int position, View view, ViewGroup parent) {
        LayoutInflater inflater = context.getLayoutInflater();
        View rowView = inflater.inflate(R.layout.list_singlefirst, null, true);

        EditText txtTitle = (EditText) rowView
                .findViewById(R.id.singelfirstet1);

        txtTitle.setText(strArrC.get(position));

        return rowView;
    }

}

将其作为list_singlefirst:

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

    <ImageButton
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:src="@drawable/ic_action_photo" />

    <ImageButton

        android:id="@+id/buttonDelete"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:background="@android:color/transparent"
        android:contentDescription="@string/button_delete_row_description"
        android:src="@drawable/testi" />

    <EditText
        android:id="@+id/singelfirstet1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_toLeftOf="@+id/buttonDelete"
        android:layout_toRightOf="@+id/imageView1"
        android:ems="10" >


    </EditText>

</RelativeLayout>

片段上的onCreateView看起来像:

public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        // Inflate the layout for this fragment

        LinearLayout mLinearLayout = (LinearLayout) inflater.inflate(
                R.layout.editlistsfirst, container, false);




        list2 = (ListView) mLinearLayout.findViewById(R.id.editlistsfirstlv);

        count = new ArrayList<String>();
        operations = new ArrayList<String>();

        count = retrievecounter(1);
        operations = retrievecounter(0);

        EditArrayAdapter adapter = new EditArrayAdapter(this.getActivity(), operations, count);

        list2.setAdapter(adapter);

        list2.setOnItemClickListener(this);

        adapter.notifyDataSetChanged();




        return mLinearLayout;
    }

Fragment位于MainActivity中。

这是我的第一个应用程序,所以我对这个主题知之甚少。

我想要做的是创建一个包含2个图像按钮和editText(我能够做到)的行的列表视图,根据我点击的按钮,应该显示带有来自edittext的文本的Toast和其他应该删除edittext中的文本。

我在片段中使用setonitemclicklistener尝试了它,但这并没有真正起作用。

所以问题是如何实现不同的Clicklistener并区分按下哪一行按钮?

提前致谢。

2 个答案:

答案 0 :(得分:1)

您需要为OnClickListener方法中的每个按钮设置getView()

@Override
public View getView(int position, View view, ViewGroup parent) {
    LayoutInflater inflater = context.getLayoutInflater();
    View rowView = inflater.inflate(R.layout.list_singlefirst, null, true);

    EditText txtTitle = (EditText) rowView
            .findViewById(R.id.singelfirstet1);

    txtTitle.setText(strArrC.get(position));


    ImageButton deleteButton = (ImageButton) rowView.findViewById(R.id.buttonDelete);
    deleteButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            // do something here
        }
    }

    return rowView;
}

答案 1 :(得分:1)

在getView()方法中应用OnClickListener。并应用你想做的任务。

@Override
    public View getView(int position, View view, ViewGroup parent) {
        LayoutInflater inflater = context.getLayoutInflater();
        View rowView = inflater.inflate(R.layout.list_singlefirst, null, true);

        EditText txtTitle = (EditText) rowView
                .findViewById(R.id.singelfirstet1);

        txtTitle.setText(strArrC.get(position));
        Button button = (Button) view.findViewById(R.id.yourbutton);
       button.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                //do something. May be call function based on text from edittext or anything else
            }
        });

        return rowView;
    }