简体   繁体   中英

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++){
            JSONObject c = peoples.getJSONObject(i);
            id = c.getString(TAG_ID);
            String name = c.getString(TAG_NAME);
            String address = c.getString(TAG_ADD);

            HashMap<String,String> persons = new HashMap<String,String>();

            persons.put(TAG_ID,id);
            persons.put(TAG_NAME,name);
            persons.put(TAG_ADD,address);

            personList.add(persons);
        }

        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}

                );

        list.setAdapter(adapter);
       list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
           @Override
           public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
               switch(i)
               {
               case 0 :
               Intent appInfo = new Intent(MainActivity.this, MainActivity2.class);
               startActivity(appInfo);
               break;
               case 1 :
               Intent ap = new Intent(MainActivity.this, MainActivity3.class);
               startActivity(ap);
               break;
               case 2 :
               Intent Info = new Intent(MainActivity.this, MainActivity2.class);
               startActivity(Info);
               break;
           }}
       });
    } catch (JSONException e) {
        e.printStackTrace();
    }

}

public void getData(){
    class GetDataJSON extends AsyncTask<String, Void, String>{

        @Override
        protected String doInBackground(String... params) {
            DefaultHttpClient httpclient = new DefaultHttpClient(new BasicHttpParams());
            HttpPost httppost = new HttpPost("http://10.0.2.2/in.php");

            // Depends on your web service
            httppost.setHeader("Content-type", "application/json");

            InputStream inputStream = null;
            String result = null;
            try {
                HttpResponse response = httpclient.execute(httppost);
                HttpEntity entity = response.getEntity();

                inputStream = entity.getContent();
                // json is UTF-8 by default
                BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), 8);
                StringBuilder sb = new StringBuilder();

                String line = null;
                while ((line = reader.readLine()) != null)
                {
                    sb.append(line + "\n");
                }
                result = sb.toString();
            } catch (Exception e) {
                // Oops
            }
            finally {
                try{if(inputStream != null)inputStream.close();}catch(Exception squish){}
            }

            return result;

        }

        @Override
        protected void onPostExecute(String result){
            myJSON=result;
            showList();
        }
    }
    GetDataJSON g = new GetDataJSON();
    g.execute();
} }

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"        android:layout_width="match_parent"
 android:layout_height="match_parent"     android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
   android:orientation="vertical"
android:background="#ff3c3f41"
android:paddingBottom="@dimen/activity_vertical_margin"   tools:context=".MainActivity">


<ListView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/listView"
/>

</LinearLayout>

list_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="10dp"
android:paddingLeft="10dp"
android:paddingRight="10dp" >


<TextView
    android:id="@+id/id"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:paddingBottom="2dip"
    android:paddingTop="6dip"
    android:textColor="#fff9f9f9"
    android:textStyle="bold" />


<TextView
    android:id="@+id/name"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:paddingBottom="2dip"
    android:textColor="#fff9f9f9"
    android:textStyle="bold"/>


<TextView
    android:id="@+id/address"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="left"

    android:textColor="#fff9f9f9"
    android:textStyle="bold" />
    <TextView
    android:id="@+id/quantity"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="left"

    android:textColor="#fff9f9f9"
    android:textStyle="bold" />

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="166dp"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:padding="10dp"
    android:paddingLeft="10dp"
    android:paddingRight="10dp" >

    <ImageButton
        android:layout_width="58dp"
        android:layout_height="52dp"
        android:src="@drawable/plus"
        android:layout_gravity="start"
        android:id="@+id/ib_plus" />

    <ImageButton
        android:layout_width="58dp"
        android:layout_height="52dp"
        android:src="@drawable/minus"
        android:id="@+id/ib_minus"
        android:layout_gravity="center_horizontal"
        android:layout_alignBottom="@+id/ib_plus"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:layout_alignTop="@+id/ib_plus" />

</RelativeLayout>

logcat:

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
        at com.example.abdul.zx.MainActivity.onCreate(MainActivity.java:62)

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 )

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.

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.

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM