【问题标题】:Android list view with multiple buttons带有多个按钮的 Android 列表视图
【发布时间】:2015-12-21 11:01:11
【问题描述】:

问题是,如果单击第一个按钮,那么第十二个按钮也仅在移动设备上运行此错误,如果在模拟器上运行,列表显示所有元素并完美运行。但在移动列表视图中显示 10 个项目,需要向下滚动才能查看更多`public class MyCustomAdapter 扩展 BaseAdapter 实现 ListAdapter { 私有 ArrayList 列表 = 新 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);

        }

【问题讨论】:

  • 你尝试了什么,你是如何发布代码的?
  • 这是列表视图适配器中的问题,使用boolean flag 来识别您使用 id 或某些东西单击了哪个项目
  • 代码没有错误,但在模拟器上运行良好...但是如果使用小尺寸手机则需要滚动列表以查看更多项目...然后问题发生了
  • 问题可能是适配器的位置,发布你的代码
  • 只有当列表项未显示完整项时才会出现问题

标签: android


【解决方案1】:

自定义 LIstview 与 ViewHolder 完美配合。这里的问题是您的按钮状态使用 getView() 方法更改。如果您使用 ViewHolder 完成此操作,您的 ListView 项目将保持其状态。

【讨论】:

    【解决方案2】:

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

    改一下

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

    【讨论】:

      【解决方案3】:

      问题在于列表视图布局被回收以提高性能,这意味着屏幕上的第一个可见单元格的位置为 0 并递增到最后一个可见单元格。因此,如果设备显示 10 个视图,那么每 10 个视图本质上是相同的视图。保留要修改的视图位置列表是一种快速简单的方法,可确保在正确的视图上绘制正确的方案。

      解决方案

      List<Integer> presentPositions = new ArrayList<>();
      List<Integer> absentPositions = new ArrayList<>();
      
      public View getView(int position, View convertView, ViewGroup parent) {
      
          if (convertView == null) {
              LayoutInflater inflater = (LayoutInflater) context
                  .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
              convertView = inflater.inflate(R.layout.list, parent, false);
          }
      
          //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) {
                  presentPosition.add(position);
                  Toast.makeText(context,"hello",Toast.LENGTH_SHORT).show();
                  deleteBtn.setBackgroundColor(Color.GREEN);
                  addBtn.setEnabled(true);
              }
          });
      
          addBtn.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
                  absentPositions.add(position);
                  addBtn.setBackgroundColor(Color.GREEN);
                  deleteBtn.setEnabled(false);
               //   notifyDataSetChanged();
              }
          });
      
          if (presentPositions.contains(position)) {
              deleteBtn.setBackgroundColor(Color.GREEN);
              addBtn.setEnabled(false);
          } else {
              deleteBtn.setBackgroundColor(Color.RED);
              addBtn.setEnabled(true);
          }
      
          if (absentPositions.contains(position)) {
              addBtn.setBackgroundColor(Color.GREEN);
              deleteBtn.setEnabled(false);
          } else {
              addBtn.setBackgroundColor(Color.RED);
              deleteBtn.setEnabled(true);
          }
      
          return convertView;
      }
      

      【讨论】:

      • 它仍然禁用两个不存在的按钮?? 1 日 11 日
      • @unniraja 更新答案,启用的代码也需要在 if 语句中
      猜你喜欢
      • 1970-01-01
      • 2012-10-16
      • 1970-01-01
      • 2011-06-22
      • 2011-09-01
      • 1970-01-01
      • 2012-07-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多