1

I am a newbie in android programming.In my program onItemClick event is not responding.I am loading data through JSON.When i removed the radiobutton from xml layout the code works properly.

 public class Taketest extends ListActivity``
    {

        private ProgressDialog pDialog;
        String question,ans1,ans2,ans3,ans4,ans5;

        ArrayList<HashMap<String, String>> productsList;

        @Override
        public void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);

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

            new LoadAllProducts().execute();


            ListView lv = getListView();


            list.setOnItemClickListener(new OnItemClickListener()

            {    @Override


                public void onItemClick(AdapterView<?> parent, View view,int position, long id)
                {


                    String selected = ((TextView) view.findViewById(R.id.textView2)).getText().toString();


                }
            });



        }




        class LoadAllProducts extends AsyncTask<String, String, String> {

            /**
             * Before starting background thread Show Progress Dialog
             * */
            @Override
            protected void onPreExecute() {
                super.onPreExecute();
                pDialog = new ProgressDialog(Taketest.this);
                pDialog.setMessage("Loading Questions... Please wait...");
                pDialog.setIndeterminate(false);
                pDialog.setCancelable(false);
                pDialog.show();
            }

            /**
             * getting All products from url
             * */
            protected String doInBackground(String... args) {

                List<NameValuePair> params = new ArrayList<NameValuePair>();


                try {
                    int success = 1;// or 0

                    if (success == 1) {

                        products = json.getJSONArray(TAG);// load product through json

                        for (int i = 0; i < products.length(); i++) 
                        {
                            JSONObject c = products.getJSONObject(i);

                            String id = c.getString("id");
                            question =  c.getString("question");
                            ans1 = c.getString("op1");
                            ans2 = c.getString("op2");
                            ans3 = c.getString("op3");
                            ans4 = c.getString("op4");
                            ans5 = c.getString("op5");



                            // creating new HashMap
                            HashMap<String, String> map = new HashMap<String, String>();

                            // adding each child node to HashMap key => value
                            map.put("id", id);
                            map.put("ques", question);
                            map.put("a",ans1);
                            map.put("b",ans2);
                            map.put("c",ans3);
                            map.put("d",ans4);
                            map.put("e",ans5);


                            // adding HashList to ArrayList
                            productsList.add(map);
                        }
                    } 
                    else {

                        Intent i = new Intent(getApplicationContext(),
                                Signup.class);

                        i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                        startActivity(i);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }

                return null;
            }


            protected void onPostExecute(String file_url) 
            {
                // dismiss the dialog after getting all products
                pDialog.dismiss();
                // updating UI from Background Thread
                runOnUiThread(new Runnable() {
                    public void run() 
                    {

                        ListAdapter adapter =
                                new SimpleAdapter(Taketest.this, productsList, R.layout.list_item, new String[]{"id", "ques","a","b","c","d","e"},
                                        new int[]{R.id.pid, R.id.name, R.id.rb1, R.id.rb2, R.id.rb3, R.id.rb4, R.id.rb5});
                        // updating listview
                        setListAdapter(adapter);
                    }
                });



            }

        }
    }

and my xml is

<?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" >

   <TextView
        android:id="@+id/pid"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:visibility="gone" />


    <TextView
        android:id="@+id/name"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:paddingTop="6dip"
        android:paddingLeft="6dip"
        android:textSize="17dip"
        android:textStyle="bold" />
    <RadioGroup
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:weightSum="1"
        android:id="@+id/rg">
    <RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/rb1"
        android:checked="false" />

    <RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/rb2"
        android:checked="false" />

    <RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/rb3"
        android:checked="false" />

    <RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/rb4"
        android:checked="false" />

    <RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/rb5"
        android:checked="false" />

    </RadioGroup>

</LinearLayout>
  • The radio buttons blocks the item click. check http://stackoverflow.com/questions/1121192/android-custom-listview-unable-to-click-on-items and http://stackoverflow.com/questions/9885803/listview-onclicklistener-does-not-work-after-adding-radiobutton – nbaroz Jan 20 '16 at 07:59

1 Answers1

0

If your are extends BaseAdapter for your adatper class which is use for fill listview data than you can use :-

  public View getView(int i, View convertView, ViewGroup viewGroup) {

        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //Data
            }
        });
}