ListView onClick()中的ImageButton

时间:2013-06-24 09:09:56

标签: android listview onclick adapter

我有下一个listView listView exemple onItemClickListener - 工作正常。 我在自定义适配器中放入getView的onClick方法。但它很糟糕,只有当位置== 0时才有效。

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());

}
}

4 个答案:

答案 0 :(得分:1)

你应该使用持有者模式,因为你正在使用一个监听器。我认为这种改进可能有所帮助。

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

答案 1 :(得分:1)

我认为你从自定义适配器mySCAdapter.Return自定义视图返回getView方法的默认视图而不是调用 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;
}

答案 2 :(得分:0)

尝试

 button.setOnClickListener(this);

在mySCAdapter中实现onClickListener并添加未实现的方法onClick并输入代码。

答案 3 :(得分:0)

我想它太晚了,但是做了同样的事情,希望这会有所帮助:

从这里开始处理基本代码:http://www.codelearn.org/android-tutorial/android-listview

但是,我对getView()方法进行了一些更改,以便附加的imageView具有onClick列表器。

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