带有多个按钮的Android列表视图

时间:2015-12-21 11:01:11

标签: android

enter image description here

问题是如果点击第一个按钮然后第十二个按钮也只在移动设备上运行此错误如果在模拟器上运行列表显示所有元素并且运行完美。但在移动列表视图中显示10个项目并需要向下滚动以查看更多`public class MyCustomAdapter扩展BaseAdapter实现ListAdapter {     private ArrayList list = new ArrayList();     私有上下文上下文;

public MyCustomAdapter(ArrayList<String> list, Context context) {
    this.list = list;
    this.context = context;

}

@Override
public int getCount() {
    return list.size();
}

@Override
public Object getItem(int pos) {
    return list.get(pos);
}

@Override
public long getItemId(int pos) {
    return 0; //list.get(pos).getId();
    //just return 0 if your list items do not have an Id variable.
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    View view = convertView;
    if (view == null) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = inflater.inflate(R.layout.list, null);
    }

    //Handle TextView and display string from your list
    TextView listItemText = (TextView)view.findViewById(R.id.name);
    listItemText.setText(list.get(position));

    //Handle buttons and add onClickListeners
    final Button deleteBtn = (Button)view.findViewById(R.id.present);
    final Button addBtn = (Button)view.findViewById(R.id.absent);

    deleteBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //do something
           // list.remove(position); //or some other task
            Toast.makeText(context,"hello",Toast.LENGTH_SHORT).show();
            deleteBtn.setBackgroundColor(Color.GREEN);
            addBtn.setEnabled(false);
          //  notifyDataSetChanged();
        }
    });
    addBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //do something
            addBtn.setBackgroundColor(Color.GREEN);
            deleteBtn.setEnabled(false);
         //   notifyDataSetChanged();
        }
    });

    return view;
}

}`

这是列表项添加

 public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            str = sp1.getSelectedItem().toString();

            //handle listview and assign adapter
            if(str.equals("3rd year")) {
                Toast.makeText(getApplicationContext(), str, Toast.LENGTH_SHORT).show();

               for(int i=0;i<15;i++)
               {
                   list3.add("Item"+i);
               }
            }
            ListView lView = (ListView) findViewById(R.id.listView);
            lView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
            lView.setAdapter(adapter);

        }

2 个答案:

答案 0 :(得分:0)

自定义LIstview与ViewHolder完美配合。问题是您的按钮状态随getView()方法发生变化。如果您使用ViewHolder完成,您的ListView项目将保持其状态。

答案 1 :(得分:0)

这种情况会发生,因为您的视图会被重复使用。模拟器很弱,它不会重复使用视图,而是创建一个新视图。

更改此

View view = convertView;    
    if (view == null) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = inflater.inflate(R.layout.list, null);
    }

到这个

ViewHolder holder = null;
 if (view == null) {
        view = LayoutInflater.from(context).inflate(R.layout.list, viewGroup, false);
        holder = new ViewHolder(view);
        // init your views here, f.e.:
        holder.listItemText = (TextView)view.findViewById(R.id.name);            
        // or just use Butterknife library
        view.setTag(holder);
    } else {
        holder = (ViewHolder) view.getTag();
    }
    // now you can set value
    holder.listItemText.setText(list.get(position));

ViewHolder保存对象,用于存储有关视图的信息

class ViewHolder {
    TextView listItemText ;
    Button deleteBtn;
    Button addBtn;
        public ViewHolder(View view) {
           // I would advice you to use Butterknife, it saves time. http://jakewharton.github.io/butterknife/ 
           //ButterKnife.inject(this, view);
        }
    }