In Listview setonlistitem not working

2019-09-04 03:03发布

问题:

I am creating one example where I am passing id from one activity to another,in my first activity I have Listview,after click on any item it should redirect to next page,in my other activities same code is working but for this activity after cliking on list,its not intent..

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

        }

}

回答1:

If ListView item more then one view click listener then have to implement each view custom data click listener in adapter getView() instead of list item click listener :

    @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;
    }