在列表视图setonlistitem不工作 [英] In Listview setonlistitem not working

查看:127
本文介绍了在列表视图setonlistitem不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个例子,我想过去的ID从一个活动到另一个,在我的第一个活动我列表视图,单击任一项目后,它应该重定向到下一个页面,在我的其他活动一样code的工作但对于本次活动cliking名单上后,它不是目的。

 公共类InterestAccept扩展ListActivity {
    私人ProgressDialog pDialog;
    JSONArray interestreceived = NULL;
            私人的ListView升;
            私人的ArrayList< HashMap的<字符串,字符串>>一个列表;
            私有静态字符串INTEREST_RECEIVE_URL =;
            私有静态最后弦乐INTEREST_RECEIVE =interestreceived;
            私有静态最后弦乐INTEREST_RECEIVEUSER_ID =received_detail_id;
            私有静态最后弦乐INTEREST_RECEIVE_NAME =名;
            私有静态最后弦乐INTEREST_RECEIVE_PROFILE =PROFILE_ID;
            私有静态最后弦乐INTEREST_RECEIVE_IMAGE =形象;
            私有静态最后弦乐INTEREST_RECEIVE_CAST =投;
            私有静态最后弦乐INTEREST_RECEIVE_AGE =时代;
            私有静态最后弦乐INTEREST_RECEIVE_LOCATION =地利;
            私人CustomAdapterReceive适配器;
            私人TextView的nointrecive;

    @覆盖
    保护无效的onCreate(包savedInstanceState){
        // TODO自动生成方法存根
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.list_view);
        nointrecive =(TextView中)findViewById(R.id.no_intrecv);
        字符串strtexts = getIntent()getStringExtra(ID)。
        的System.out.println(<<<<<<<<编号:+ strtexts);
        INTEREST_RECEIVE_URL =XXXXXXXX+ strtexts;
        //listview=(ListView)findViewById(R.id.list);
        // ListView控件列表视图= this.getListView();
        ListView控件列表视图=(ListView控件)findViewById(android.R.id.list);
        新LoadAlbums()执行();
    }

     @覆盖
        公共无效onListItemClick(ListView的L,视图V,INT位置,长的id){
            意向意图=新的意图(getApplicationContext(),InterestDetails.class);
            intent.putExtra(received_detail_id,aList.get(位置)获得(INTEREST_RECEIVEUSER_ID));
            startActivity(意向);
        }

        类LoadAlbums扩展的AsyncTask<字符串,字符串,ArrayList的< HashMap的<字符串,字符串>>> {

            @覆盖
            在preExecute保护无效(){
                super.on preExecute();
                pDialog =新ProgressDialog(InterestAccept.this);
                pDialog.setMessage(载入中...);
                pDialog.setIndeterminate(假);
                pDialog.setCancelable(假);
                pDialog.show();
            }
            受保护的ArrayList< HashMap的<字符串,字符串>> doInBackground(字符串参数... args){
                ServiceHandler SH =新ServiceHandler();

                //制作一个请求URL并得到响应
                ArrayList的< HashMap的<字符串,字符串>>数据=新的ArrayList< HashMap的<字符串,字符串>>();
                字符串jsonStr = sh.makeServiceCall(INTEREST_RECEIVE_URL,ServiceHandler.GET);

                Log.d(回应:,>中+ jsonStr);

                如果(jsonStr!= NULL){
                    尝试 {
                        JSONObject的jsonObj =新的JSONObject(jsonStr);

                        //获取JSON数组节点
                        interestreceived = jsonObj.getJSONArray(INTEREST_RECEIVE);
                        //遍历所有联系人
                        的for(int i = 0; I< interestreceived.length();我++){
                            JSONObject的C = interestreceived.getJSONObject(我);

                            //创建新的HashMap
                            HashMap的<字符串,字符串>图=新的HashMap<字符串,字符串>();

                            //添加每个子节点HashMap中的key =>值
                            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)+年);
                            map.put(INTEREST_RECEIVE_LOCATION,c.getString(INTEREST_RECEIVE_LOCATION));

                            //添加HashList到ArrayList中
                            data.add(图)
                        }
                    }赶上(JSONException E){
                        e.printStackTrace();
                    }
                } 其他 {
                    Log.e(ServiceHandler,无法从URL中得到任何数据);
                }

                返回的数据;
            }
            保护无效onPostExecute(ArrayList中< HashMap的<字符串,字符串>>的结果){

                super.onPostExecute(结果);

                如果(interestreceived == NULL || interestreceived.length()== 0){
                       // Toast.makeText(getApplicationContext(),无应答,Toast.LENGTH_SHORT).show();
                    nointrecive.setText(没有兴趣接收);

                }
                其他
                {
                    nointrecive.setVisibility(View.INVISIBLE);
                }
                让所有专辑后//关闭该对话框
                如果(pDialog.isShowing())
                    pDialog.dismiss();
                //从后台线程更新UI

                如果(ALIST == NULL){
                    ALIST =新的ArrayList< HashMap的<字符串,字符串>>();
                    aList.addAll(结果);
                    适配器=新CustomAdapterReceive(getBaseContext(),结果);
                    setListAdapter(适配器);
                }其他{
                    aList.addAll(结果);
                    adapter.notifyDataSetChanged();
                }
            }

        }

}
 

解决方案

如果ListView项不止一个视图点击监听,那么必须实现,而不是列表项单击监听器适配器getView每个视图自定义数据点击监听器():

  @覆盖
    公共查看getView(最终诠释的立场,观点convertView,ViewGroup中父){
        ViewHolder持有人;
        如果(convertView == NULL){
            持有人=新ViewHolder();
            convertView = LayoutInflater.from(上下文).inflate(R.layout.list_item_interestrecv,NULL);
            holder.btnAccept =(按钮)convertView.findViewById(R.id.btnAccept);
            holder.btnDecline =(按钮)convertView.findViewById(R.id.btnDecline);

            convertView.setTag(保持器);
        }其他{
            支架=(ViewHolder)convertView.getTag();
        }
        holder.btnAccept.setOnClickListener(新View.OnClickListener(){
            @覆盖
            公共无效的onClick(视图v){
                意向意图=新的意图(getApplicationContext(),InterestDetails.class);
                intent.putExtra(received_detail_id,listData.get(位置)获得(INTEREST_RECEIVEUSER_ID));
                startActivity(意向);
            }
        });
        holder.btnDecline.setOnClickListener(新View.OnClickListener(){
            @覆盖
            公共无效的onClick(视图v){
                意向意图=新的意图(getApplicationContext(),InterestDetails.class);
                intent.putExtra(received_detail_id,listData.get(位置)获得(INTEREST_RECEIVEUSER_ID));
                startActivity(意向);
            }
        });

        返回convertView;
    }
    类ViewHolder {
        按钮btnAccept;
        按钮btnDecline;
    }
 

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

        }

}

解决方案

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

这篇关于在列表视图setonlistitem不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆