2

I have a custom listview adapter. I want to include setontemclicklistener to disable a listitem from the listview. I've tried using onitemclicklistener but it doesn't work, can you help me out?

Home class:

public class Home extends Activity {

JSONObject jsonobject;
JSONArray jsonarray;
ListView listview;
ListViewAdapter adapter;

SharedPreferences pref;
String uid;
static String user_id,us;
ArrayList<HashMap<String, String>> arraylist;

//static String BET_ID = "bet_id";
static String QUESTION = "question";
static String QUES_ID = "ques_id";



@SuppressLint("NewApi")
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    pref = PreferenceManager.getDefaultSharedPreferences(this);
    setContentView(R.layout.questionlist);

uid = pref.getString("user_id",null);
Log.d("uid", ""+uid);



    new DownloadJSON().execute();
}


private class DownloadJSON extends AsyncTask<Void, Void, Void> {

    @Override
    protected Void doInBackground(Void... params) {


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

        jsonobject = JSONfunctions
                .getJSONfromURL("http://192.168.1.23/MutilatedPHP/QuizGame/quesdownloadjson.php");

        try {

            jsonarray = jsonobject.getJSONArray("ques");

            for (int i = 0; i < jsonarray.length(); i++) {
                HashMap<String, String> map = new HashMap<String, String>();
                jsonobject = jsonarray.getJSONObject(i);


                map.put("question", jsonobject.getString("question"));  
                map.put("ques_id", jsonobject.getString("ques_id"));    



                arraylist.add(map);
            }
        } catch (JSONException e) {
            Log.e("Error", e.getMessage());
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onPostExecute(Void args) {

        listview = (ListView) findViewById(R.id.listView1);

        adapter = new ListViewAdapter(Home.this, arraylist);

        listview.setAdapter(adapter);


    }
}
}

ListviewAdapter :

public class ListViewAdapter extends BaseAdapter {

Context context;
LayoutInflater inflater;
ArrayList<HashMap<String, String>> data;



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

public ListViewAdapter(Context context,
        ArrayList<HashMap<String, String>> arraylist) {
    this.context = context;
    data = arraylist;

}

@Override
public int getCount() {
    return data.size();

}

@Override
public Object getItem(int position) {
    return null;
}

@Override
public long getItemId(int position) {
    return 0;
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {

    TextView qs1;
    TextView qs2;


    View v = convertView;

    if (v == null) {
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v = inflater.inflate(R.layout.listitem, null);
    }



    resultp = data.get(position);


    qs2 = (TextView) v.findViewById(R.id.qs);



    qs2.setText(resultp.get(Home.QUESTION));





    v.setOnClickListener(new OnClickListener() {


        @Override
        public void onClick(View arg0) {



            resultp = data.get(position);


            Intent intent = new Intent(context, SingleItemView.class);


             intent.putExtra("question", resultp.get(Home.QUESTION));
             intent.putExtra("ques_id", resultp.get(Home.QUES_ID));



             context.startActivity(intent);


        }

    });

    return v;

}



}

Thank you in advance

Leonidas I
  • 119
  • 1
  • 2
  • 9
  • Can you post some code? How are you disabling your items? How are you setting the listener? – Marius Jun 20 '14 at 06:34
  • post your code along with listview xml file. – Spring Breaker Jun 20 '14 at 06:35
  • 2
    what happens when you click on listitem you have `v.setOnClickListener`. Does it not take to `SingleItemView.java`? – Raghunandan Jun 20 '14 at 06:37
  • 1
    Still don't see where you assign your `OnItemClickListener` you mentioned or where you try to disable `ListItem` – Marius Jun 20 '14 at 06:38
  • @Raghunandan : Yes it does but how do i disable an item that is clicked once? – Leonidas I Jun 20 '14 at 06:39
  • @LeonidasI You need to store a boolean value somewhere and check the value when the list is displayed and refresh the list. uDo you want it to disabled or hidden when you open the list again? – Raghunandan Jun 20 '14 at 06:40
  • @Marius: Okay so how do i disable the listitem using the setonclicklistener present in my adapter – Leonidas I Jun 20 '14 at 06:42
  • @Raghunandan : Hidden would be perfect – Leonidas I Jun 20 '14 at 06:43
  • 1
    @LeonidasI similar to this http://stackoverflow.com/questions/20611123/listview-subobject-clickable-confilct. When you click the button it turns blue. similarly set the visibility of the view – Raghunandan Jun 20 '14 at 06:44
  • 1
    Either of these should work, though `setEnabled(false)` will change color too. http://developer.android.com/reference/android/view/View.html#setEnabled(boolean) http://developer.android.com/reference/android/view/View.html#setClickable(boolean) – Marius Jun 20 '14 at 06:45

4 Answers4

2

Try This!

In your MainActivity form where the Adapter class is called Add like this:

  listView1.setOnItemClickListener(new OnItemClickListener() {

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

             // ListView Clicked item index
             int itemPosition     = position;


         }
     }); 
Robi Kumar Tomar
  • 3,418
  • 3
  • 20
  • 27
Krupa Patel
  • 3,309
  • 3
  • 23
  • 28
  • You mean inside `protected void onPostExecute(Void args)` in the class `Home`? – Leonidas I Jun 20 '14 at 06:51
  • 1
    no.and that is not compoulsory that you need to set Adapter calss in Async task class.you can set visible true to your list view in onPostExecute() function of your Async task class. And call listView1.setAdapter(adapter);new Task().execute(); lines in your Create() method – Krupa Patel Jun 20 '14 at 06:52
  • Cool but how do i add the item position here – Leonidas I Jun 20 '14 at 06:58
  • in your onItemClick() you will get the int position parameter which will give you the position you tap – Krupa Patel Jun 20 '14 at 07:17
2

You just simple need to call OnItemClickListener for ListView.

listview.setOnItemClickListener(new OnItemClickListener() {

    @Override
    public void onItemClick(AdapterView<?> a, View v, int position,
            long id) {


    }
});
Piyush
  • 18,895
  • 5
  • 32
  • 63
1

Try this

listView.setOnItemClickListener();

@Override
    public void onItemClick(AdapterView<?> parent, View view, int position,
            long id) {
        //Use your code here
    }
Aman Singh
  • 370
  • 1
  • 9
1

Inside On Create use this method

  listview = (ListView) findViewById(R.id.listView1);

  listview.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> a, View v, int position,
                long id) {
            Object o = listview.getItemAtPosition(position);

        }
    });
Mourice
  • 597
  • 1
  • 5
  • 17