Wajid Usman February 2016

Buttons in a ListView row with two layouts and one activity

I want to add two buttons in each row of a listview. While using only one Activity means that in my code i have one MainActivity.java and its layout (activity_main.xml) and an additional layout (list_item.xml) which have items in each row (Textviews) but when i add two buttons in a list_item.xml and initialize in MainActivity and add listners on it it shows me an exception which is shared below.Help me to solve my problem, Any sort of help will be highly appreciated.

MainActivity.java

public class MainActivity extends ActionBarActivity
{

String myJSON ;
String id;
private static final String TAG_RESULTS="result";
private static final String TAG_ID = "id";
private static final String TAG_NAME = "name";
private static final String TAG_ADD ="address";

JSONArray peoples = null;

ArrayList <HashMap <String, String> > personList;

ListView list;
TextView quant;
int count=0;
Button b_plus,b_minus;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    list = (ListView) findViewById(R.id.listView);
    personList = new ArrayList<HashMap<String,String>>();
    quant=(TextView)findViewById(R.id.quantity);


    b_plus=(Button)findViewById(R.id.ib_plus);
    b_minus=(Button)findViewById(R.id.ib_minus);

    b_plus.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            count++;
            quant.setText(Integer.toString(count));
        }
    });
    b_minus.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            count--;
            quant.setText(Integer.toString(count));
        }
    });

    getData();


}


protected void showList(){
    try {
        JSONObject jsonObj = new JSONObject(myJSON);
        peoples = jsonObj.getJSONArray(TAG_RESULTS);

        for(int i=0;i<peoples.length();i++){
              

Answers


cricket_007 February 2016

Your buttons are not defined in R.layout.activity_main so findViewById will return null since they are not there.

quant is also null, by the way, for the same reason.

You must define your buttons in your SimpleAdapter class because they are in the list_item.xml. (Assuming SimpleAdapter inflates list_item.xml)


Onur Çevik February 2016

You need to inflate the list_item layout first as a view. Then, you need to initialize your ImageButtons by view.findViewById(). Now, you are trying to use the ImageButton's of a layout other than what you have set in onCreate. Or another approach would be to set onClick methods inside the adapter.


Code-Apprentice February 2016

The problem is that list_item.xml is inflated for each row of data in your ListView. At the point in onCreate() where you try to get the button views, they have not yet been created. In order to set listeners to each button, you need to create a custom adapter class. You will then set the listeners in the getView() method.


Kirill K February 2016

You'll have to make your adapter more complicated, because there is not one but many buttons and text fields. Pseudo code like solution below. Replace the line where you create your adapter with something like this:

final ListAdapter adapter = new SimpleAdapter
            (MainActivity.this, personList, R.layout.list_item,
            new String[]{TAG_ID,TAG_NAME,TAG_ADD},
            new int[]{R.id.id, R.id.name, R.id.address}) {

         public View getView(int position, View convertView, ViewGroup parent) {
             View view = super.getView(position, convertView, parent);

             final TextView quant=(TextView)view.findViewById(R.id.quantity);
             final ImageButton b_plus=(ImageButton)view.findViewById(R.id.ib_plus);
             final ImageButton b_minus=(ImageButton)view.findViewById(R.id.ib_minus);

             b_plus.setOnClickListener(new View.OnClickListener() {
                 @Override
                 public void onClick(View view) {
                     count++;
                     quant.setText(Integer.toString(count));
                 }
             });
             b_minus.setOnClickListener(new View.OnClickListener() {
                 @Override
                 public void onClick(View view) {
                     count--;
                     quant.setText(Integer.toString(count));
                 }
             });

             return view;
         }
     };

Remove everything related to quant, b_minus, b_plus from other places in your activity


Marian February 2016

I believe that you might be doing these steps in the wrong place:

quant=(TextView)findViewById(R.id.quantity);
b_plus=(Button)findViewById(R.id.ib_plus);
b_minus=(Button)findViewById(R.id.ib_minus);

I think what's happening is that the code is searching for them in activity_main.xml but because they're not there, they end up being null. Hence, the exception you're seeing.

Since the TextView and the Buttons are related to each ListView item, then that's where the initialization should go. I see you're using a SimpleAdaptor which I am not familiar with. But if you create your custom adaptor for ListView, then you can initialize the TextView and the Buttons in the getView() of the Adaptor kind of like this: Custom Adapter for List View.

You can set the button listeners there as well which will provide you with the position of the ListItem where a certain button was clicked.

Post Status

Asked in February 2016
Viewed 1,951 times
Voted 7
Answered 5 times

Search




Leave an answer