ImageButton inside ListView onClick()

こ雲淡風輕ζ 提交于 2019-12-13 16:20:21

问题


I have next listView

onItemClickListener - works fine. onClick method i put into getView in custom Adapter. But it works bad, it works only when position ==0.Why???
public class mySCAdapter extends SimpleCursorAdapter implements OnClickListener {
final String LOG_TAG = "myLogs";
LayoutInflater inflater;
public mySCAdapter(Context context, int layout, Cursor c, String[] from,
        int[] to) {

    super(context, layout, c, from, to);
    inflater = LayoutInflater.from( context );
    // TODO Auto-generated constructor stub
}

@Override
public View getView( int position, View convertView, ViewGroup parent) {
    View v = null;

    if( convertView != null )
        v = convertView;
    else
        v = inflater.inflate( R.layout.add_program_ex_list_item, parent, false);
    ImageButton button = (ImageButton) v
            .findViewById(R.id.add_program_exercise_list);
    button.setTag(position);
    button.setOnClickListener(this); 


    return super.getView(position, convertView, parent);
}

@Override
public void onClick(View v) {
    Log.d(LOG_TAG, "It works, pos=" + v.getTag());

}
}

回答1:


You should use the holder Pattern, cause you are using a listener. I think this improvement could help.

public class mySCAdapter extends SimpleCursorAdapter {

      ....

      static class ViewHolder {
        public ImageButton btn;
        public ImageView image;
      }

     @Override
    public View getView( int position, View convertView, ViewGroup parent) {
        final int a=position;
        View v = convertView;
        if(v == null ){
           LayoutInflater inflater = context.getLayoutInflater();
            v = inflater.inflate( R.layout.add_program_ex_list_item, parent, false);
            ViewHolder viewHolder = new ViewHolder();
            viewHolder.image = (ImageView) v.findViewById(R.id.ImageView01);
            viewHolder.btn = (ImageButton) v.findViewById(R.id.add_program_exercise_list);
            viewHolder.btn.setOnClickListener(new OnClickListener() {
                public void onClick(View v) {
                    Log.d(LOG_TAG, "It works, pos=" + a);
                }
                });
            v.setTag(viewHolder);
        }
        ViewHolder holder = (ViewHolder) v.getTag();
        // here you can get viewholder items
        // eg : holder.btn.setText("button");
        return v;
    }



回答2:


I think you are returning a default view from getView method from custom adapter mySCAdapter.Return customised view rather than calling return super.getView(position, convertView, parent);

@Override
public View getView( int position, View convertView, ViewGroup parent) {
    View v = null;

    if( convertView != null )
        v = convertView;
    else
        v = inflater.inflate( R.layout.add_program_ex_list_item, parent, false);
    ImageButton button = (ImageButton) v
            .findViewById(R.id.add_program_exercise_list);
    button.setTag(position);
    button.setOnClickListener(this); 


    return v;
}



回答3:


Try

 button.setOnClickListener(this);

implement onClickListener in mySCAdapter and add the unimplemented method onClick and type the code.




回答4:


I guess its too late, but was doing the same thing , hope this helps:

The basic code is coped from here: http://www.codelearn.org/android-tutorial/android-listview

However, i've made a few changes to the getView() method so that the imageView attached has an onClick listner.

    List<codeLearnChapter> codeLearnChapterList = getDataForListView();

    /**
     * This method tells the listview the number of rows it will require. 
     * This count can come from your data source. It can be the size of your Data Source. 
     * If you have your datasource as a list of objects, this value will be the size of the list.
     */
    @Override
    public int getCount() 
    {
        //return 0;
        return codeLearnChapterList.size();
    }

    /**
     * We have 2 method implementation of getItem. 
     * This method returns an object. 
     * This method helps ListView to get data for each row. 
     * The parameter passed is the row number starting from 0. 
     * In our List of Objects, this method will return the object at the passed index.
     */
    @Override
    public Object getItem(int arg0) 
    {
        //return null;
        return codeLearnChapterList.get(arg0);
    }

    /**
     * You can ignore this method. 
     * It just returns the same value as passed. 
     * This in general helps ListView to map its rows to the data set elements.
     */
    @Override
    public long getItemId(int position) 
    {
        return position;
    }

    /**
     * This is the most important method. 
     * This method will be called to get the View for each row. 
     * This is the method where we can use our custom listitem and bind it with the data. 
     * The fist argument passed to getView is the listview item position ie row number. T
     * he second parameter is recycled view reference(as we know listview recycles a view, you can confirm through this parameter). 
     * Third parameter is the parent to which this view will get attached to.
     */
    @Override
    public View getView(int position, View convertView, ViewGroup parent) 
    {    
        final int temp = position;

        if(convertView==null)
        {
          LayoutInflater inflater = (LayoutInflater) MainActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
          convertView = inflater.inflate(R.layout.listitem, parent,false);
        }

        TextView chapterName = (TextView) convertView.findViewById(R.id.textView1);
        TextView chapterDesc = (TextView) convertView.findViewById(R.id.textView2);

        final ImageView listImg = (ImageView) convertView.findViewById(R.id.imageView1);
        listImg.setOnClickListener(new OnClickListener() 
        {

            @SuppressWarnings("deprecation")
            @Override
            public void onClick(View v) 
            {
                Toast.makeText(MainActivity.this, "Imagine Dragons" + " " + Integer.toString(temp), Toast.LENGTH_LONG).show();

                if(listImg.getAlpha()==1)
                {
                    listImg.setAlpha(0);
                }else
                {
                    listImg.setAlpha(1);
                }
            }
        });


        codeLearnChapter chapter = codeLearnChapterList.get(position);

        chapterName.setText(chapter.chapterName);
        chapterDesc.setText(chapter.chapterDescription);

        return convertView;
    }


来源:https://stackoverflow.com/questions/17271815/imagebutton-inside-listview-onclick

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!