【问题标题】:Image Viewnot working properly in list view图像视图在列表视图中无法正常工作
【发布时间】:2011-09-01 15:07:56
【问题描述】:

我正在尝试实现在每一行中具有一个可点击图像视图的列表视图,为此我创建了自定义适配器并覆盖了它的所有方法。现在我的问题是 当我单击此图像视图而不单击该行时,它无法正常运行,我的代码在下面

protected void onListItemClick(ListView l, View v, final int position, long id) {

    super.onListItemClick(l, v, position, id);

     RowData rowObject = (RowData) l.getItemAtPosition(position);
     contactId= rowObject.getTextId(); 

//contactId gives the position of the row clicked in the list view.

     Log.e("on item clicked", contactId.toString());

     ImageView nextView = rowObject.getImageobject();
     nextView.setClickable(true);
     nextView.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
                // TODO Auto-generated method stub
            Log.i("Value of row id", String.valueOf(position));
                Intent myintent = new Intent(v.getContext(),Info.class);
                myintent.putExtra("ContactId", contactId);
                v.getContext().startActivity(myintent);

        }
    });

如果我先单击该行然后单击图像视图,则它工作正常。但是如果我点击图像视图而不点击该行(即,没有选择该行),那么它会采用最后点击的行的位置。

请帮帮我。

【问题讨论】:

    标签: android android-listview


    【解决方案1】:

    查看我的 getView 函数。因为我有一个对象出现在每个列表中。 . .当我点击通话按钮时,我可以获得点击。

    public View getView(final int position, View convertView, ViewGroup parent) 
            {
    
                View v = convertView;
                if (v == null) 
                {
                    try
                    {
                    LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                    v = vi.inflate(R.layout.pantry_list_row, null);
    
    
    
                        }
                    catch(Exception e)
                    {
                        System.out.println("Excption Caught"+e);
                    }
                }
                PantreisInfo p = items.get(position);
    
    
                if (p != null) 
                {
                    TextView name = (TextView) v.findViewById(R.id.pantryName);
                    TextView add1= (TextView) v.findViewById(R.id.pantryAddress1);
                    TextView add2=(TextView)v.findViewById(R.id.pantryAddress2);
                    TextView phno = (TextView) v.findViewById(R.id.pantryPhno);
                    TextView distTime = (TextView) v.findViewById(R.id.distTime);
    

    //

                    if (name != null)
                    {
                        name.setText(p.getName().get(0).toString());
                    }
                    if (add1 != null) 
                    {
                        add1.setText(p.getAddress1().get(0).toString());
                    }
                    if (add2 != null) 
                    {
                        add2.setText(p.getAddress2().get(0).toString());
                    }
                    if (phno != null) 
                    {
                        phno.setText(p.getPhone().get(0).toString());
    
                    }
                    if (distTime!= null) 
                    {
                        distTime.setText("Distribution Time: "+p.getDistTime().get(0).toString());
                    }
                }
                    ImageView phbtn=(ImageView)v.findViewById(R.id.phbtn);
                    phbtn.setOnClickListener(new View.OnClickListener() 
                    {
    
                        @Override
                        public void onClick(View arg0) 
                        {
    
                            final AlertDialog.Builder dialog=new AlertDialog.Builder(PantriesListActivity.this);
                            PantreisInfo p=items.get(position);
    
                            String tempPhone=p.getPhone().get(0).toString();
                            String phoneNumber="";
                            for(int i=0;i<tempPhone.length();i++)
                            {
                                 if (Character.isDigit(tempPhone.charAt(i)))
                                 {
                                     System.out.println(tempPhone.charAt(i));
                                     phoneNumber=phoneNumber+tempPhone.charAt(i);
                                 }
    
                            }
    
                            dialog.setMessage("Press ok to Dial "+phoneNumber+",otherwise click on cancel to discard.");
                            final String number="tel:" +phoneNumber.trim();
    
                            dialog.setPositiveButton("Ok",new DialogInterface.OnClickListener() 
                            {
    
                                @Override
                                public void onClick(DialogInterface arg0, int arg1) 
                                {
                                    // TODO Auto-generated method stub
    
                                      Intent callIntent = new Intent(Intent.ACTION_CALL,Uri.parse(number)); 
                                        startActivity(callIntent);
    
                                    dialog.setCancelable(true);
                                }
                            });
                            dialog.setNegativeButton("Cancel",new DialogInterface.OnClickListener() {
    
                                @Override
                                public void onClick(DialogInterface dfdialog, int which)
                                {
                                    // TODO Auto-generated method stub
                                    dialog.setCancelable(true);
                                }
                            });
    
                            dialog.show();
                            //Toast.makeText(PantriesListActivity.this,""+p.getPhone().get(0), Toast.LENGTH_SHORT).show();
    
    
    
                        }
                    });
    
    
                    return v;
            }
    

    【讨论】:

    • 您好,谢谢您的帮助。但我真正想要的是,当我单击任何行的图像视图时,应该单击整行(这样我将能够获得该行的位置并将该位置传递给下一个活动),你能告诉我任何方式吗这样做
    • 你应该关闭 ImageView 的属性。只需将 ImageView 的属性设置为 android:clickable="false"
    【解决方案2】:

    好吧,终于在列表视图中挖掘了这么多,我自己找到了一个解决方案,那就是自定义适配器的 getView 方法,现在每当我单击图像时,getview 的 (int position) 参数给出了该行的位置clicked.here 是我稍后实现的代码。

    private class CustomAdapter extends ArrayAdapter<RowData> {
        public CustomAdapter(Context context,int resource,int textViewResourceId,List<RowData> objects) {
            super(context, resource, textViewResourceId, objects);
        }
    
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder = null;
            TextView Name     = null;
            TextView JobTitle   = null;
            ImageView Emailimage = null;
            ImageView Phoneimage = null;
            ImageView nextView = null;
            RowData rowData   = getItem(position);
    
            if(null == convertView){
                mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                convertView = mInflater.inflate(R.layout.contact_found_list_item_row, null);
                holder = new ViewHolder(convertView);
                convertView.setTag(holder);
            }
    
            String positionId = String.valueOf(position);
            holder = (ViewHolder) convertView.getTag();
            Name = holder.gettitle();
            Name.setText(rowData.mTitle);
            JobTitle = holder.getdetail();
            JobTitle.setText(rowData.mDetail);                                                     
            Emailimage=holder.getImage();
            Emailimage.setImageResource(rowData.phoneId);
            Phoneimage = holder.getphoneImage();
            Phoneimage.setImageResource(rowData.emailId);
            nextView = holder.getNextViewObject();
            nextView.setTag(positionId);
            nextView.setOnClickListener(new OnClickListener() {
    
                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    ImageView iv = (ImageView)v;
                    String rowid = (String)iv.getTag();
                    Intent myintent = new Intent(v.getContext(),Info.class);
                    myintent.putExtra("ContactId", rowid);
                    v.getContext().startActivity(myintent);
                }
            });
    
            return convertView;
        }
    
        private class ViewHolder {
            private View mRow;
    
            private TextView Name   = null;
            private TextView JobTitle = null;
            private ImageView Emaillogo = null; 
            private ImageView phonelogo = null;
            private ImageView nextViewlogo = null;
    
            public ViewHolder(View row) {
                mRow = row;
            }
    
            public TextView gettitle() {
                if(null == Name){
                    Name = (TextView) mRow.findViewById(R.id.txtfirstname);
                }
                return Name;
            }     
    
            public TextView getdetail() {
                if(null == JobTitle){
                    JobTitle = (TextView) mRow.findViewById(R.id.txtjobtitle);
                }
                return JobTitle;
            }
    
            public ImageView getImage() {
                if(null == Emaillogo){
                    Emaillogo = (ImageView) mRow.findViewById(R.id.emaillogo);
                }
                return Emaillogo;
            }
            public ImageView getphoneImage(){
                if(null == phonelogo){
                    phonelogo = (ImageView) mRow.findViewById(R.id.mobilelogo);
                }return phonelogo;
            }  
            public ImageView getNextViewObject(){
                if(null == nextViewlogo){
                    nextViewlogo = (ImageView)mRow.findViewById(R.id.nextbutton);
                }return nextViewlogo;
            }
    
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-01-09
      • 1970-01-01
      • 1970-01-01
      • 2017-04-24
      • 1970-01-01
      • 1970-01-01
      • 2012-06-01
      相关资源
      最近更新 更多