2011-07-17 54 views
0

如何从分配给例如ImageView的onClick方法访问例如TextView? TextView和ImageView组成了ListView项目。从另一个onClick方法控件访问ListView控件

在我item.xml我:

<?xml version="1.0" encoding="UTF-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="horizontal" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
> 
<ImageView 
    android:id="@+id/img" 
    android:layout_width="wrap_content" 
    android:layout_height="fill_parent" 
    android:onClick="onImgClick" 
    />  
<TextView android:id="@+id/text" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    /> 
</LinearLayout> 

在我ListActivity我有onImgClick方法:

public void onImgClick(View v) { 
    TextView tv = (TextView) v.findViewById(R.id.text); 
tv.setText("Hello world!"); 
} 

但视图V - 这是ImageView的,所以在这个方法我有致命例外:

java.lang.NullPointerException 

就行:

tv.setText("Hello world!"); 

我知道这是不正确的方式来访问TextView。而且我知道我可以根据整个ListView使用onListItemClick(ListView l,View v,int position,long id)。但我想从ImageView onClick方法实现这一点。

+0

如果的ImageView和TextView中形成ListView项,那么他们在列表视图的项目子视图,所以你正在获得与父母的孩子...你可以实现(我猜是这样),但onclick方法将只分配在列表视图的顶部而不是所有项目... –

+0

是的,我可以做那样的事情。或者根据整个ListView使用onListItemClick(ListView l,View v,int position,long id)方法。但我想从ImageView onClick方法中找到解决方案。 – woyaru

回答

5

我解决了我的问题。我只需要设置ID对我的LinearLayout:

android:id="@+id/main_layout" 

而接下来做这样的事情:

public void onImgClick(View v) { 
    LinearLayout linearLayout = (LinearLayout) v.getParent();  
    TextView textView = (TextView) linearLayout.findViewById(R.id.text);   
    textView.setText("Hello world!"); 
} 
0

好了,访问的TextView你的最简单的方法是通过处理程序和使用的sendMessage:

http://developer.android.com/reference/android/os/Handler.html

或者,这取决于你是如何实现它,你可能要考虑的getParent() ,如在v.getParent().findViewById(r.id.text);

我不确定这是否适用于您的情况。

+0

第二种方法:'v.getParent()。' - 这里没有findViewById()方法。顺便说一下,当我做这样的事情︰'Toast.makeText(getApplicationContext(),v.getParent()。toString(),0).show();'我可以看到作为父视图android.widget.LinearLayout。 – woyaru

+0

如果我想使用Handler和sendMessage,我必须在我的类中声明我的TextView(如果我理解它的话)。但我有ListView的项目计数,并且当我单击ImageView时,我无法访问ImageView onClick方法中的TextView。 – woyaru

0

在onclick监听器上设置listview的getView方法中的listview。

+0

你能给我更多的细节吗?你的意思是像mbarnes向我推荐的东西? – woyaru

0

我建议您重写ListAdapter,并在getView()中将侦听器设置为父视图。使用setTag()使标识更容易。

LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

... 

getView (int position, View convertView, ViewGroup parent){ 
LinearLayout thisview = null; 
if(convertView == null){ 
    convertView = inflater.inflate(R.layout.item, parent); 
} 

TextView tv = convertView.findViewById(R.id.text); 

ImageView img = convertView.findViewById(R.id.img); 
img.setTag("uniqueid_"+position); 

//this will put a reference to the TextView in the ImageView 
img.setKey("TEXT_VIEW", tv); 

img.setOnClickListener(this); 
... 
return convertView; 
} 

然后你可以在onclick()与v.getKey TextView的(“TEXT_VIEW”)...但它似乎是内存泄漏很好的潜力。

+0

但我如何在我的onImgClick方法中使用getView(int position,View convertView,ViewGroup parent)方法?在onImgClick我只有一个参数 - 视图。 – woyaru

相关问题