2

我有下一个 listView 列表视图示例 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

4 回答 4

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;
}
于 2013-06-24T11:45:28.303 回答
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;
    }
于 2013-06-24T09:31:15.177 回答
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;
    }
于 2014-03-14T17:10:46.010 回答
0

尝试

 button.setOnClickListener(this);

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

于 2013-06-24T09:25:58.627 回答