usd123
usd123

Reputation: 37

Onclick is not working for a button in Listview to fetch TextView in android

I have Two Buttons along with the text in ListView. Onclick Listener is not working when i click on Button.

The following is my code:

list_details.xml

<ListView
        android:id="@+id/list"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="60dp" />

list_item_details.xml

<TextView
android:id="@+id/try_it"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold" 
android:text="tile try" />

<Button
    android:id="@+id/download"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="left"
    />

ListDetailsActivity.java

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.list_details);

productsList = new ArrayList<HashMap<String, String>>();

new LoadAllProducts().execute();

ListView lv = getListView();

Button download = (Button) findViewById(R.id.download);

download.setOnClickListener(new View.OnClickListener() {

     public void onClick(View view) {

         TextView try_txt = (TextView) findViewById(R.id.try_it);

         String try_text_val = try_txt.getText().toString();

        Toast.makeText(getApplicationContext(), try_text_val, Toast.LENGTH_LONG).show();
     }
  });
 }


protected void onPostExecute(String file_url) {

            // updating UI from Background Thread
            runOnUiThread(new Runnable() {

                public void run() {

                 ListAdapter adapter = new SimpleAdapter(

                         ListDetailsActivity.this, productsList,
                    R.layout.list_item_details, new String[] { TAG_TRY_IT},
                    new int[] { R.id.try_it});

                    // updating listview
                    setListAdapter(adapter);
                                    }
            });
   }
  }

Many thanks for any help!

Upvotes: 0

Views: 779

Answers (3)

Rob Meeuwisse
Rob Meeuwisse

Reputation: 2937

You're only setting the OnClickListener on the first Button with id R.id.download. When you use findViewById() it will only return the first view that has the requested id, even if there are more. Because you are calling it on the Activity it has multiple rows in the listview with buttons with that id.

The way to fix it is to do it in your adapter. You are using a SimpleAdapter. Make your own adapter class and override the getView() function to attach the OnClickListener there. Something like this:

private class MyAdapter extends SimpleAdapter {
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = super.getView(position, convertView, parent);
        View button = view.findViewById(R.id.download);
        button.setOnClickListener( /* ... your code ... */ );
        return view;
    }
}

View Holder design pattern

In your click handler you are manipulating the TextView R.id.try_it. So you will need to do a findViewById() for that as well on the view you're returning from getView(). The View Holder design patter is meant for this use case. My advise is to read the Android tutorial on Making ListView Scrolling Smooth to learn how that works.

Upvotes: 0

Budimir Grom
Budimir Grom

Reputation: 766

Consider using android:onClick in your list row layout, for each button. It may be the most convenient way for this purpose, as almost always.

Upvotes: 0

grig
grig

Reputation: 847

You just need to set the onClick event in your custom adapter. (in getView method)

Upvotes: 1

Related Questions