自定义适配器列表视图中对ImageView的设置的onclick

自定义适配器列表视图中对ImageView的设置的onclick

本文介绍了使用自定义适配器列表视图中对ImageView的设置的onclick监听器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个活动MainActivity.java和Second.java。我想,当图像的用户点击查看其背景图片会发生变化,当用户点击任何项目列表,第二个活动将打开。
这里是我的code,它不工作.....

rowlaout.xml

 < LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:ID =@ + ID / LinearLayout1
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    机器人:填充=6DIP>    < ImageView的
        机器人:ID =@ + ID /按钮
        机器人:layout_width =22dp
        机器人:layout_height =22dp
        机器人:layout_marginLeft =4DP
        机器人:layout_marginRight =10dp
        机器人:layout_marginTop =4DP
        机器人:SRC =@绘制/ OK>
    < / ImageView的>    <的TextView
        机器人:ID =@ + /标记
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:文字颜色=#FFF
        机器人:TEXTSIZE =30sp>
    < / TextView的>< / LinearLayout中>

dataListAdapter.java

 类dataListAdapter延伸BaseAdapter {        的String []标题;
        上下文语境;
        ImageView的I1;
        TextView的T1;
        公共dataListAdapter(上下文连拍的String []文本){
            标题=文字;
            上下文= CONT;
        }
        公众诠释的getCount(){
            // TODO自动生成方法存根
            返回Title.length;
        }        公共对象的getItem(INT为arg0){
            // TODO自动生成方法存根
            返回null;
        }        众长getItemId(INT位置){
            // TODO自动生成方法存根
            返回的位置;
        }
        @覆盖
        公共查看getView(INT位置,查看convertView,父母的ViewGroup){            LayoutInflater吹气=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);;
            查看排;
            行= inflater.inflate(R.layout.rowlayout,父母,假);
            I1 =(ImageView的)row.findViewById(R.id.button);
            T1 =(TextView中)row.findViewById(R.id.label);
            t1.setText(标题[位置]);
            i1.setOnClickListener(新View.OnClickListener(){                @覆盖
                公共无效的onClick(视图v){
                    // TODO自动生成方法存根
                i1.setImageResource(R.drawable.no);
                }
            });
            返回(行);
        }

}

MainActivity.java

 公共类MainActivity延伸活动{    意图意图;
    ListView控件列表;
    INT []状态;
    诠释宝;
    @覆盖
    保护无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_main);        名单=(ListView控件)findViewById(R.id.list);
         的String []值=新的String [] {机器人,iPhone,的WindowsMo​​bile
                    黑莓,WebOS的,Ubuntu的,Windows7的,最大OS X
                    的Linux,OS / 2};
         dataListAdapter适配器=新dataListAdapter(这一点,值);
         list.setAdapter(适配器);
         list.setOnItemClickListener(新AdapterView.OnItemClickListener(){             @覆盖
              公共无效onItemClick(适配器视图<>母公司,最终视图来看,
                  INT位置,长的id){             字符串itemValue =(字符串)list.getItemAtPosition(位置);
             捆绑sendBundle =新包();
             sendBundle.putString(价值,itemValue);
             意图=新意图(MainActivity.this,Second.class);
             intent.putExtras(sendBundle);
             startActivity(意向);
             PO =位置;
            }
         });    }    @覆盖
    公共布尔onCreateOptionsMenu(菜单菜单){
        //充气菜单;如果是present这增加了项目操作栏。
        。getMenuInflater()膨胀(R.menu.main,菜单);
        返回true;
    }}


解决方案

首先,你应该改变的ImageView I1即知名度不应该是可见出getView方法。

 上下文的背景下;
    // ImageView的I1; //从这里删除该声明
    TextView的T1;@覆盖
公共查看getView(INT位置,查看convertView,父母的ViewGroup){
        LayoutInflater吹气=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);;
        查看排;
        行= inflater.inflate(R.layout.rowlayout,父母,假);
      最后ImageView的I1 =(ImageView的)row.findViewById(R.id.button);
        T1 =(TextView中)row.findViewById(R.id.label);
        t1.setText(标题[位置]);
        i1.setOnClickListener(新View.OnClickListener(){            @覆盖
            公共无效的onClick(视图v){
                // TODO自动生成方法存根
            i1.setImageResource(R.drawable.no);            // 要么
            //((ImageView的)视图).setImageResource(R.drawable.no);
            }
        });
        返回(行);
    }

在此之后尝试使用处理器中的onClick设置imageResource()方法

 公共无效的onClick(视图v){
 // TODO自动生成方法存根
      新的处理程序()。后(新的Runnable(){        @覆盖
        公共无效的run(){
            i1.setImageResource(R.drawable.no);
        }
    });
 }

这应该解决您的问题。

I have two activities MainActivity.java and Second.java. I want that when the user clicks on image view its background image will change and when user clicks on any item in list, Second Activity will open.Here is My code, it's not working.....

rowlaout.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/LinearLayout1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="6dip" >

    <ImageView
        android:id="@+id/button"
        android:layout_width="22dp"
        android:layout_height="22dp"
        android:layout_marginLeft="4dp"
        android:layout_marginRight="10dp"
        android:layout_marginTop="4dp"
        android:src="@drawable/ok" >
    </ImageView>

    <TextView
        android:id="@+id/label"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#fff"
        android:textSize="30sp" >
    </TextView>

</LinearLayout>

dataListAdapter.java

class dataListAdapter extends BaseAdapter {

        String[] Title;
        Context context;
        ImageView i1;
        TextView t1;
        public dataListAdapter(Context cont,String[] text) {
            Title = text;
            context=cont;
        }
        public int getCount() {
            // TODO Auto-generated method stub
            return Title.length;
        }

        public Object getItem(int arg0) {
            // TODO Auto-generated method stub
            return null;
        }

        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return position;
        }
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);;
            View row;
            row = inflater.inflate(R.layout.rowlayout, parent, false);
            i1=(ImageView)row.findViewById(R.id.button);
            t1=(TextView) row.findViewById(R.id.label);
            t1.setText(Title[position]);
            i1.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                i1.setImageResource(R.drawable.no);
                }
            });
            return (row);
        }

}

MainActivity.java

public class MainActivity extends Activity {

    Intent intent;
    ListView list;
    int [] state;
    int po;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        list=(ListView)findViewById(R.id.list);
         String[] values = new String[] { "Android", "iPhone", "WindowsMobile",
                    "Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X",
                    "Linux", "OS/2" };
         dataListAdapter adapter = new dataListAdapter(this, values);
         list.setAdapter(adapter);
         list.setOnItemClickListener(new AdapterView.OnItemClickListener() {

             @Override
              public void onItemClick(AdapterView<?> parent, final View view,
                  int position, long id) {

             String  itemValue= (String) list.getItemAtPosition(position);
             Bundle sendBundle = new Bundle();
             sendBundle.putString("value", itemValue);
             intent=new Intent(MainActivity.this,Second.class);
             intent.putExtras(sendBundle);
             startActivity(intent);
             po=position;
            }
         });

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}
解决方案

First you should change visibility of ImageView i1 i.e. it should not be visible out of getView method.

    Context context;
    // ImageView i1; // remove this declaration from here
    TextView t1;

@Override
public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);;
        View row;
        row = inflater.inflate(R.layout.rowlayout, parent, false);
      final ImageView i1=(ImageView)row.findViewById(R.id.button);
        t1=(TextView) row.findViewById(R.id.label);
        t1.setText(Title[position]);
        i1.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
            i1.setImageResource(R.drawable.no);

            // OR
            // ((ImageView)view).setImageResource(R.drawable.no);
            }
        });
        return (row);
    }

after this try to set imageResource using handler in onClick() method

 public void onClick(View v) {
 // TODO Auto-generated method stub
      new Handler().post(new Runnable() {

        @Override
        public void run() {
            i1.setImageResource(R.drawable.no);
        }
    });
 }

This should resolve your issue.

这篇关于使用自定义适配器列表视图中对ImageView的设置的onclick监听器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 03:50