0

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!

usd123
  • 37
  • 1
  • 8

3 Answers3

1

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

grig
  • 848
  • 6
  • 15
0

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.

Budimir Grom
  • 756
  • 6
  • 12
0

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.

Rob Meeuwisse
  • 2,847
  • 1
  • 17
  • 21
  • Thanks a lot. I created a custom adapter but i am facing issue with when starting intent using onClick from getView(). below is the code: bt1.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) { Uri uri = Uri.parse("google.com"); Intent intent = new Intent(Intent.ACTION_VIEW, uri); context.startActivity(intent); } }); if you have any suggestions please let me know. – usd123 Dec 20 '14 at 15:10
  • Put the protocol in the uri: `Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://google.com"));` – Rob Meeuwisse Dec 20 '14 at 15:44