如何在ANDROID自定义Listview适配器中使用setonitemclicklistener

时间:2014-06-20 06:31:32

标签: android android-listview

我有一个自定义列表视图适配器。我想包含setontemclicklistener来禁用listview中的listitem。 我尝试过使用onitemclicklistener,但它不起作用,你可以帮助我吗?

家庭课程:

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;

}



}

提前谢谢

4 个答案:

答案 0 :(得分:2)

试试这个!

在调用Adapter类的MainActivity表单中添加如下:

  listView1.setOnItemClickListener(new OnItemClickListener() {

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

             // ListView Clicked item index
             int itemPosition     = position;


         }
     }); 

答案 1 :(得分:2)

您只需要为ListView调用 OnItemClickListener

listview.setOnItemClickListener(new OnItemClickListener() {

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


    }
});

答案 2 :(得分:1)

试试这个

listView.setOnItemClickListener();

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

答案 3 :(得分:1)

Inside On Create使用此方法

  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);

        }
    });