【问题标题】:Unable to check/uncheck CheckedTextView inside getView无法在 getView 中选中/取消选中 CheckedTextView
【发布时间】:2012-09-20 10:16:46
【问题描述】:

我正在自定义 ListView 中加载电话联系人。每行都是一个可检查的 LinearLayout,其中包含一个 CheckedTextView 和另一个 TextView。

我正在使用自定义 ArrayAdapter 提供列表视图。我的问题是我无法在 getView() 中控制 CheckedTextViews。例如,当我尝试以下操作时

public View getView(int position, View convertView, ViewGroup parent) {
        View row = convertView;
        if(row == null){            
            row = inflater.inflate(layout, parent, false);
        }

        CheckedTextView checkedTextView =  (CheckedTextView) row.findViewById(R.id.checkedTextView);
        checkedTextView.setText("A");
        checkedTextView.setChecked(true);
        return row;
    }

每当我滚动列表视图时,它应该检查每个文本视图,但这并没有发生。谁能告诉我怎么做?

编辑:在 getView() 中检查很重要,我不能在 setListAdapter() 之后检查所有内容

EDIT2:这是显示每一行视图的 xml 文件

<?xml version="1.0" encoding="utf-8"?>
<com.example.multiplecontacts.CheckableLinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <CheckedTextView
        android:id="@+id/checkedTextView"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:checkMark="?android:attr/listChoiceIndicatorMultiple"
        android:paddingBottom="0dp"
        android:text="CheckedTextView"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <TextView
        android:id="@+id/subTextView"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Small Text"
        android:paddingTop="0dp"
        android:textAppearance="?android:attr/textAppearanceSmall" />

</com.example.multiplecontacts.CheckableLinearLayout>

CheckableLinearLayout 是一个自定义布局,它扩展了 LinearLayout 并实现了我之前所说的 Checkable。我已经从here 拿走了它

【问题讨论】:

  • 我不需要默认勾选。实际上上面的代码不是我的实际代码,它只是一个演示我的问题的示例。我所需要的只是能够在 getView() 中切换 CheckedTextView
  • 你有checkbox的布局但是当你按下checkbox时没有检查?
  • 在 Alex Orlov 的良好反应之后,我这样做了:public View getView(int position, View convertView, ViewGroup parent) {..... ((ListView) parent).setItemChecked(position, true);....}

标签: android android-layout android-spinner android-checkbox


【解决方案1】:

您是否在布局 xml 中为 CheckedTextView 设置了 checkMark 属性?

例如:android:checkMark="?android:attr/listChoiceIndicatorMultiple

CheckedTextView 不仅仅是一个带有文本的复选框。您还必须注意,CheckedTextView 是不可聚焦的,或者在没有一些操作的情况下可点击(因为它是为 ListView 设计的,因此它的状态必须由 ListView 的setOnItemClickListener 控制)

setChoiceMode 应该为 ListView 设置。 检查适配器的 getView 中的行应该通过:listView.setItemChecked(position, value)

【讨论】:

  • 我做到了。当我自己按下一个项目时,它通常会被选中/取消选中。它只是在 getView() 内部的行为。
  • 可能 CheckedTextView 是由 ListView 控制的,您是否将 ListView 的ChoiceMode 设置为多个?
  • 如果您可以从适配器访问您的 ListView 实例,您可以(并且可能应该)使用 listView.setItemChecked(position, value)
  • 亚历克斯·奥尔洛夫,级长。非常感谢。我已经将一个实例传递给适配器并完成了你所说的。它现在就像魅力一样。请编辑您的答案并包含您的评论:)
  • 您可以使用适配器中的((ListView) parent).setItemChecked(position, true); 而不是传递引用。
【解决方案2】:

@Pier-Luc Gendreau 和 @fox 在 cmets 中提到了解决方案,但没有在答案中提及,因此代表他们发布答案。

正如@AlexOrlov 所提到的,CheckedTextView 在没有一些操作的情况下是不可聚焦或可点击的。所以你需要做的是使用 ListView 设置项目检查,你可以从适配器本身或从你有 ListView 的活动/片段中进行。

要从适配器执行此操作,

public View getView(int position, View convertView, ViewGroup parent) 
{ 
if (//logic) {
((ListView) parent).setItemChecked(position, true);
}
}

来自活动/片段

if (//logic) {
listView.setItemChecked(position, true);
}

【讨论】:

    【解决方案3】:

    当你滚动列表时,每次它调用它的 getview() 方法。因此,如果您在 listcell 中有任何复选框或任何编辑框,它将重新初始化它。

    我的想法是存储复选框的状态(选中或未选中)。所以在这里我首先使用 ArrayList 我用 false 值填充它,然后在它的点击事件上我用来存储它的实际状态。

     class MyAdapter extends BaseAdapter  implements OnClickListener   {
    
        private ArrayList<Boolean> checks=new ArrayList<Boolean>();//Boolean type array to   manage check box
    
      private static LayoutInflater inflater=null;
    
        public MyAdapter(Context context, ArrayList<HashMap<String, String>> d)
        {
                  inflater =     (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    
          //fill with false values
        for (int i = 0; i < d.size(); i++) 
        {
                checks.add(i, false);
        }
        }
    
        public int getCount()
        {
            return data.size();
        }
    
        public Object getItem(int position)
        {
            return position;
        }
    
        public long getItemId(int position) 
        {
            return position;
        }
    
    
    
    
            public View getView(int position, View convertView, ViewGroup parent)
           {
    
            View vi=convertView;
    
            if(convertView==null)
    
              vi = inflater.inflate(R.layout.<your layout>, null);
    
              //Checkbox is of button type----android:button="@drawable/btn_check"
              //make a selector xml for checkbox       
    
              checkBox=(CheckBox)vi.findViewById(R.id.check_box);
    
    
              checkBox.setTag(Integer.valueOf(position));
              checkBox.setOnClickListener(this);
              checkBox.setChecked(checks.get(position));
    
    
            return vi;
        }
    
            @Override
            public void onClick(View v) 
            {
              int viewId=v.getId();
              if(viewId== R.id.check_box)
              {
                Integer index = (Integer)v.getTag();
                boolean state = checks.get(index.intValue());
                checks.set(index.intValue(), !state);
    
              }
            }
         }
    

    更新:解决方案 2:您可以在 ViewHolder 类中放置一个布尔变量。这个布尔变量将用于定义是否选择了项目。

    希望这对您有所帮助。

    【讨论】:

    • checkBox.setChecked(checks.get(position));这对你有用吗?是 CheckedTextView 类型的复选框还是只是一个复选框?
    • @IslamHassan 是的,它在我身边工作。这里我的复选框是 Button 类型的。
    猜你喜欢
    • 1970-01-01
    • 2020-09-11
    • 2013-01-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-12
    • 1970-01-01
    • 2012-06-01
    相关资源
    最近更新 更多