开发者

Getting access to a ListView control from the another onClick method control

How can I get access to the, for example, TextView, from the onClick method assigned to the, for example, ImageView? The TextView and ImageView make up the ListView item.

In my item.xml I have:

<?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>

And in my ListActivity I have onImgClick method:

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

But the View v - this is ImageView, so in this method I have Fatal Exception:

java.lang.NullPointerException

on the line:

tv.setText("Hello world!");

I know this isn't correct way to get access to the TextView. And I know that I can use onListItemClick(ListView l, View v, int position, long id) according to the whole ListView. but I want to achieve this from the ImageView onClick method.


I have resolved my problem. I need only to set id on my LinearLayout:

android:id="@+id/main_layout"

And next do something like that:

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


Well, your easiest way to access the TextView is through a Handler and use sendMessage:

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

Alternatively, depending on how you've implemented it, you might want to look into getParent(), as in v.getParent().findViewById(r.id.text);

I am not sure if that works for your case though.


set on an onclick listener the listview in the getView method of your listview.


I recommend that you override the ListAdapter and in getView() set the listener to the parent View. Use setTag() to make identity easier.

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

You can then get the TextView with v.getKey("TEXT_VIEW") in the onClick()... but it seems like a good potential for memory leaks.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜