【问题标题】:How to change other items in a Android ListView when one item is clicked单击一个项目时如何更改Android ListView中的其他项目
【发布时间】:2014-05-07 16:40:22
【问题描述】:

我有一个ListView,其中包含带有复选框的项目,这些复选框的行为有时应类似于CHOICE_MODE_MULTIPLE,有时类似于CHOICE_MODE_SINGLE。 What I mean is for certain items in the list, when selected certain other items needs to be deselected whilst other can remain selected.

所以当项目 A 被选中时,我可以在我的数据中找到需要取消选中的项目 B 但是我如何让 UI 刷新以将其显示为我(我相信)找不到代表 B 的实际 View,而只是它的数据?

【问题讨论】:

  • 我想通了,我可以使用getChildAtgetFirstVisiblePosition 找到我需要更新的实际View,方法是获取项目索引并减去getFirstVisiblePosition 返回的值。

标签: android android-listview listviewitem


【解决方案1】:

听起来您的开端不错。您是对的,您应该在单击 A 时操作 B 项的基础数据源。

两个提示可能对您有所帮助:

  1. 适配器中的getView() 方法应该查看您的数据源并根据它找到的内容更改convertView。您找不到代表 B 的实际视图,因为在 ListView 中,Views 被回收并在需要显示不同数据时被重复使用。基本上,当一个项目从列表中滚动出来时,所使用的View 将作为convertView 传递给getView() 函数,准备处理下一个元素的数据。因此,您可能永远不应该根据用户输入直接更改View 中的ListView,而是根据基础数据。
  2. 您可以在适配器内调用 notifyDataSetChanged() 来表示底层数据已更改,并且应该为当前显示在列表中的元素再次调用 getView()

如果您仍然遇到问题,请随时发布一些代码来说明您遇到的具体问题。当问题得到更好的定义时,提供具体的建议会容易得多。希望这会有所帮助!

【讨论】:

    【解决方案2】:

    你可以使用singleChoice alartDialog,我用过类似:

    private int i = 0; // this is global
    private final CharSequence[] items = {"Breakfast", "Lunch", "Dinner"}; // this is global
    
    Button settings = (Button)view.findViewById(R.id.settings);
    
    settings.setOnClickListener(new View.OnClickListener() {
    
        @Override
        public void onClick(final View v) {
            AlertDialog.Builder builder = new AlertDialog.Builder(v.getContext());
    
        //Title of Popup
        builder.setTitle("Settings");
    
        builder.setSingleChoiceItems(items, i,
              new DialogInterface.OnClickListener() {
           // When you click the radio button
           public void onClick(DialogInterface dialog, int item){
    
               i=item;
           }
        });
    
        builder.setPositiveButton("Confirm",
               new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int item) {
    
              if (i == 0) {
                      //it means 1st item is checked, so do your code
              }
              if (i == 1) {
                 //it means 2nd item is checked, so do your code
              } /// for more item do if statement    
            }
        });
          //When you click Cancel, Leaves PopUp.
          builder.setNegativeButton("Cancel", null); 
          builder.create().show();
    
        }
    });
    

    我已经初始化了i=0,所以当用户第一次点击settings按钮时,第一个项目被选中。然后当用户选择其他项目后,我保存了i 值,以便下次用户单击settings 按钮时,我可以向用户显示他/她之前选择的项目已被选中。

    【讨论】:

      【解决方案3】:

      我今天遇到并解决了这个问题。

      public class ItemChooceActivity extends Activity implements OnItemClickListener {
      
             private int chosenOne = -1;
      
            class Madapter extends BaseAdapter {
                       .....
                       .....
             @Override
              public View getView(final int position, View convertView,
                  ViewGroup parent) {
              // TODO Auto-generated method stub
      
                  if (chosenOne != position) {
                      set the view in A style
                  } else {
                      set the view in B style
                  }
      
              return convertView;
          }
      }
      
      
      
              @Override
              public void onItemClick(AdapterView<?> arg0, View view, int position,
              long arg3) {
          ,,,,
              chosenOne = position;
              adapter.notifyDataSetChanged();
               ,,,
      }
      

      }

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-08-24
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多