2011-09-23 60 views
1

我试图实现从适配器列表中打开具有相关信息的自定义对话框。在这里我使用onclicklistener,它工作正常我得到自定义对话框,我的问题是我没有得到正确的信息。如果我点击对话框中列表中的任何项目,它将显示最后一个项目的详细信息。Onitemclick getview方法的监听器

在生成列表时它显示了logcat中的位置。但是当我试图点击细节textview它正在采取最后一个项目的位置。

public View getView(int position, View convertView, ViewGroup parent) { 
      // TODO Auto-generated method stub 
      View v = convertView; 

      if(v == null){ 
       LayoutInflater vl = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
       v = vl.inflate(R.layout.listItem, null); 
      } 
      Fields o = results.get(position); 

      if (o != null) { 
       TextView iv = (TextView)v.findViewById(R.id.toptext); 
       TextView tv_link = (TextView)v.findViewById(R.id.toptext1);    
       ImageView tv_Image = (ImageView)v.findViewById(R.id.Locimage); 

       tv_link.setText("Details >>"); 
       tv_link.setOnClickListener(new OnClickListener() { 
        @Override 
        public void onClick(View v) { 
         // TODO Auto-generated method stub 
         Dialog dialog = new Dialog(mContext); 

         dialog.setContentView(R.layout.locationdetails); 
         dialog.setTitle("Title"); 

         System.out.println("Position "+pos); 

         TextView LocName = (TextView) dialog.findViewById(R.id.LocDescName); 
         LocName.setText(o.getLocationName()); 

ImageView LocDescImage = (ImageView) dialog.findViewById(R.id.LocDescImage); 
         Bitmap bitmap; 
         try { 
          bitmap = BitmapFactory.decodeStream((InputStream) new URL(o.getLocationImage()).getContent()); 
          LocDescImage .setImageBitmap(bitmap); 
         } catch (IOException e) { 
          // TODO Auto-generated catch block 
          e.printStackTrace(); 
         } 

         dialog.show(); 
         } 
       }); 

       } 
      DbLoc.close();  
      return v; 
     }  
    } 

回答

1

尝试使用setTag(对象o)和getTag()方法上的TextView,它可以帮助你 我的意思是

tv_link.setTag(o); 

的onClickListener内,获得使用v.getTag该对象( );

Fields o=(Fields)v.getTag(); 
LocName.setText(o.getLocationName()); 

它可以解决你的问题。

+0

非常感谢Sunriser,它解决了我的问题。我现在得到正确的结果。 – atluriajith

0

这是因为tv_link.setOnClickListener中的int:pos未正确管理。 为什么你没有在这里添加相关的代码。

无论如何,如果通过tv_link.setTag(your_pbject)传递单个对象将足够根据您的需求,通过它,否则创建内部类,它将实现View.onClickListener并在设置时通过构造函数传递相关数据这个onclickListenet为每个视图。

+0

感谢您的信息shailendra,反正我得到了解决方案。 – atluriajith