nigelorg
nigelorg

Reputation: 15

How to registre clicks on Listview with Custom Arrayadpter inside Fragment

I got a custom 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;
    }

}

with this as the 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>

The onCreateView on my Fragment looks like:

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

The Fragment is inside the MainActivity.

This is my first App so i'm not too knowledgeable on the subject.

What I want to do is create a listview with my rows which contain 2 imagebuttons and a editText (which i was able to do) and depending on which button I click one should show a Toast with the text from the edittext and the other should delete the text within the edittext.

I tried it with setonitemclicklistener in the Fragment but this didn't really work.

So the Question is how do I implement the different Clicklistener and distinguish between what Button on which Row has been pressed?

Thanks in advance.

Upvotes: 1

Views: 74

Answers (2)

Nabin
Nabin

Reputation: 11776

Apply OnClickListener inside getView() method. And apply what ever task you want to do.

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

Upvotes: 1

Code-Apprentice
Code-Apprentice

Reputation: 83567

You need to set OnClickListeners for each button in your getView() method:

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

Upvotes: 1

Related Questions