在列表视图setonlistitem不工作(In Listview setonlistitem no

2019-11-03 11:43发布

我创建了一个例子我传递的ID从一个活动到另一个,在我的第一个活动我有列表视图,单击任一项目后,它应该重定向到下一个页面,在我的其他活动同样的代码工作,但对于cliking后,本次活动在列表中,它不是目的..

public class InterestAccept extends ListActivity{   
    private ProgressDialog pDialog;
    JSONArray interestreceived=null;
            private ListView l;
            private ArrayList<HashMap<String,String>> aList;
            private static String INTEREST_RECEIVE_URL = "";
            private static final String INTEREST_RECEIVE="interestreceived";
            private static final String INTEREST_RECEIVEUSER_ID="received_detail_id";
            private static final String INTEREST_RECEIVE_NAME="name";
            private static final String INTEREST_RECEIVE_PROFILE="profile_id";
            private static final String INTEREST_RECEIVE_IMAGE="image";
            private static final String INTEREST_RECEIVE_CAST="cast";
            private static final String INTEREST_RECEIVE_AGE="age";
            private static final String INTEREST_RECEIVE_LOCATION="location";
            private CustomAdapterReceive adapter;
            private TextView nointrecive;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.list_view);
        nointrecive=(TextView)findViewById(R.id.no_intrecv);
        String strtexts = getIntent().getStringExtra("id");
        System.out.println("<<<<<<<< id : " + strtexts);
        INTEREST_RECEIVE_URL = "xxxxxxxx"+strtexts;
        //listview=(ListView)findViewById(R.id.list);
        //ListView listview = this.getListView();
        ListView listview = (ListView)findViewById(android.R.id.list);
        new LoadAlbums().execute();
    }

     @Override
        public void onListItemClick(ListView l, View v, int position, long id) {
            Intent intent = new Intent(getApplicationContext(), InterestDetails.class);
            intent.putExtra("received_detail_id", aList.get(position).get(INTEREST_RECEIVEUSER_ID));
            startActivity(intent);
        }

        class LoadAlbums extends AsyncTask<String, String, ArrayList<HashMap<String,String>>> {

            @Override
            protected void onPreExecute() {
                super.onPreExecute();
                pDialog = new ProgressDialog(InterestAccept.this);
                pDialog.setMessage("Loading...");
                pDialog.setIndeterminate(false);
                pDialog.setCancelable(false);
                pDialog.show();
            }
            protected ArrayList<HashMap<String,String>> doInBackground(String... args) {
                ServiceHandler sh = new ServiceHandler();

                // Making a request to url and getting response
                ArrayList<HashMap<String,String>> data = new ArrayList<HashMap<String, String>>();
                String jsonStr = sh.makeServiceCall(INTEREST_RECEIVE_URL, ServiceHandler.GET);

                Log.d("Response: ", "> " + jsonStr);

                if (jsonStr != null) {
                    try {
                        JSONObject jsonObj = new JSONObject(jsonStr);

                        // Getting JSON Array node
                        interestreceived = jsonObj.getJSONArray(INTEREST_RECEIVE);
                        // looping through All Contacts
                        for (int i = 0; i < interestreceived.length(); i++) {
                            JSONObject c = interestreceived.getJSONObject(i);

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

                            // adding each child node to HashMap key => value
                            map.put(INTEREST_RECEIVEUSER_ID, c.getString(INTEREST_RECEIVEUSER_ID));
                            map.put(INTEREST_RECEIVE_NAME,c.getString(INTEREST_RECEIVE_NAME));
                            map.put(INTEREST_RECEIVE_PROFILE, c.getString(INTEREST_RECEIVE_PROFILE));
                            map.put(INTEREST_RECEIVE_IMAGE, c.getString(INTEREST_RECEIVE_IMAGE));
                            //map.put(INTEREST_RECEIVE_CAST, c.getString(INTEREST_RECEIVE_CAST));
                            map.put(INTEREST_RECEIVE_AGE, c.getString(INTEREST_RECEIVE_AGE)+" years");
                            map.put(INTEREST_RECEIVE_LOCATION, c.getString(INTEREST_RECEIVE_LOCATION));

                            // adding HashList to ArrayList
                            data.add(map);
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                } else {
                    Log.e("ServiceHandler", "Couldn't get any data from the url");
                }

                return data;
            }
            protected void onPostExecute(ArrayList<HashMap<String,String>> result) {

                super.onPostExecute(result);

                if(interestreceived == null || interestreceived.length() == 0){
                       // Toast.makeText(getApplicationContext(), "No response", Toast.LENGTH_SHORT).show();
                    nointrecive.setText("  No Interest Receive  ");

                }
                else
                {
                    nointrecive.setVisibility(View.INVISIBLE);
                }
                // dismiss the dialog after getting all albums
                if (pDialog.isShowing())
                    pDialog.dismiss();
                // updating UI from Background Thread

                if(aList == null){
                    aList = new ArrayList<HashMap<String, String>>();
                    aList.addAll(result);
                    adapter = new CustomAdapterReceive(getBaseContext(), result);
                    setListAdapter(adapter);
                }else{
                    aList.addAll(result);
                    adapter.notifyDataSetChanged();
                }
            }

        }

}

Answer 1:

如果ListView项不止一个视图点击收听,然后必须执行的,而不是列表项点击监听器,适配器getView每个视图自定义数据点击监听器():

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.list_item_interestrecv, null);
            holder.btnAccept = (Button) convertView.findViewById(R.id.btnAccept);
            holder.btnDecline = (Button) convertView.findViewById(R.id.btnDecline);

            convertView.setTag(holder);
        }else{
            holder = (ViewHolder) convertView.getTag();
        }
        holder.btnAccept.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getApplicationContext(), InterestDetails.class);
                intent.putExtra("received_detail_id", listData.get(position).get(INTEREST_RECEIVEUSER_ID));
                startActivity(intent);
            }
        });
        holder.btnDecline.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getApplicationContext(), InterestDetails.class);
                intent.putExtra("received_detail_id", listData.get(position).get(INTEREST_RECEIVEUSER_ID));
                startActivity(intent);
            }
        });

        return convertView;
    }
    class ViewHolder{
        Button btnAccept;
        Button btnDecline;
    }


文章来源: In Listview setonlistitem not working