【问题标题】:ListView CheckBoxes not working as they shouldListView 复选框无法正常工作
【发布时间】:2012-08-22 14:01:18
【问题描述】:

在我的 ListItems 中选择 CheckBoxes 时遇到问题。每个 ListItem 在其布局中都有一个 CheckBox,当单击 ListItem 时,应激活 CheckBox。问题是复选框是随机激活的。例如:当我有 2 个 ListItems 并单击 ListItem1 时,它的 CheckBox 被激活。当我再次单击时,ListItem2 的 CheckBox 也被激活。当我再次单击时, ListItem1 的 CheckBox 被停用。当我再次单击时, ListItem2 的 CheckBox 被停用。当我再次单击时,一切都从头开始。我了解 ListView 重用了 Items 并且我必须使用 getView() 方法,但我无法让它工作。不得不提一下,我是一个血腥的初学者,所以如果有些代码根本没有意义,请原谅我。

【问题讨论】:

  • 检查this可能对你有帮助
  • 如何使它与自定义 ListItem XML 一起工作?该示例似乎仅适用于标准 ListItem 布局。尽管如此,有趣的是这个例子并没有使用 getView() 来使它工作。
  • 好的,所以你想要 ListView 与 checkBox 和自定义数组适配器??我正在尝试为你制作一个代码!

标签: android listview


【解决方案1】:

您需要创建一个自定义阵列适配器和所有设置。

这是我为 ChekBoxList 的 ListView 创建的 Activity 类

ActivityGamesList.java

package com.rdc.gameListApp;

import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.CheckBox;
import android.widget.ListView;
import android.widget.Toast;

public class ActivityGamesList extends Activity {

     private ListView listGames = null; 
     private DBAdapter database = null;
     private Context context = null;    

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.list_of_games);

        final CheckBox favBox = (CheckBox) findViewById(R.id.favbuttonInList);
        listGames = (ListView) findViewById(R.id.listViewGame); 

        context = getApplicationContext();
        database = new DBAdapter(context);

        //here I am getting the data from database and  putting in to List
        List<String> gamelistArrayR = database.selectAllName();     

        // custom adapter for adding fav box
        //make sure here "gamelistArrayR" is List and type is String
        listGames.setAdapter(new CustomAdapter(this, gamelistArrayR));

        //listener for list view
        listGames.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {

                Toast.makeText(getApplicationContext(), "You have selected:  " +
                          position+" no row", Toast.LENGTH_SHORT).show();

            }
        });
    }

}

下面是自定义数组适配器

CustomAdapter.java


package com.rdc.gameListApp;

import java.util.List;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;
import android.widget.Toast;

public class CustomAdapter extends ArrayAdapter<String> {
    private final Context context;
    private final List<String> values;

    public CustomAdapter(Context context, List<String> gamelistArrayR) {
        super(context, R.layout.list_row_custom, gamelistArrayR);
        this.context = context;
        this.values = gamelistArrayR;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        View rowView = inflater.inflate(R.layout.list_row_custom, parent, false);

        //Game Name Text in list
        TextView textView = (TextView) rowView.findViewById(R.id.txtGameNameInList);
        // Check Box in list
        CheckBox favBox = (CheckBox) rowView.findViewById(R.id.favbuttonInList);

        textView.setText(values.get(position));         

        //listener for check box
        favBox
        .setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
                Boolean flag = isChecked;               
                if(flag){                   

                        Log.v("Debug", "step no 1");
                        Toast.makeText(context, "Marked as Favorite Game!", 1).show();                  
                }
            }
        }); 
        return rowView;
    }
}

我得到了这样的输出..

编辑:

当您滚动列表时,检查框会因为或重新创建视图而取消检查。

所以你需要存储复选框的状态,你应该检查下面的链接

this 所以线程或 this教程

【讨论】:

  • 该代码基本上可以工作,但复选框仅在我按下它们时才会突出显示。他们没有被选中。编辑:我的错。我现在删除了 中的“android:choiceMode="multipleChoice"。现在代码完美运行。:) 竖起大拇指,感谢您的帮助。
  • 欢迎您..真的很高兴看到这个.. :) 很快我将为此创建一个教程并发布在我的博客上
  • 就像我说的,它现在可以工作了,但是还有另一个问题。当激活的 CheckBox 滚动出屏幕时,它会自行停用。我怎样才能防止这种情况? ConvertView 跟这个有关系吗?
  • 为此您需要存储选定复选框的状态..请参阅更新的答案
  • 好的,这似乎比我想象的要复杂一些。明天我会继续努力,如果有效,我会在这里发布。
【解决方案2】:

尝试使用 setOnItemSelectedListener 而不是 setOnItemClickListener 。我相信点击事件会得到你需要的更频繁的调用。

【讨论】:

  • 它不工作。 CheckBoxes 仅在我按下 ListItem 时突出显示,但无法选择。
【解决方案3】:

您需要将项目状态存储在您的适配器中,而不是在视图本身中,因为它可能是重复使用的,就像您说的那样。您对super.getView() 的调用将为您填写正确的文本,但不会更改复选框状态。保留一个反映每个复选框状态的 bool 数组,并在 getView() 中应用状态,然后再返回您的视图:

CheckBox cb = (CheckBox) view.findViewById(R.id.cb);
cb.setChecked(cbStates[position]);

【讨论】:

  • 好的,这是有道理的。我试图在我的代码中实现它,但我总是在这一行得到一个 NULLPOINTEREXCEPTION:“for(int i = 0; i
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-04-04
  • 1970-01-01
  • 1970-01-01
  • 2020-07-31
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多