我面临一个问题,就是当我滚动列表并且我不知道该怎么做时,它会消失在listview中检查的项目。
以下是我的自定义适配器代码:
public class SPCKontrola extends Activity {
ArrayList<Integer> yourSelectedItems= new ArrayList<Integer>();
@Override
public void onResume() {
super.onResume();
yourSelectedItems.clear();
}
}
public class SPCMjereAdapter extends BaseAdapter
{
private Context context;
public SPCMjereAdapter(Context c)
{
context = c;
}
public int getCount() {
return MyArrList.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(final int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = inflater.inflate(R.layout.activity_list_row, null);
}
TextView txtOpis = (TextView) convertView.findViewById(R.id.ColOpis);
txtOpis.setText(MyArrList.get(position).get("OpisMjere") );
TextView txtRbMjere = (TextView) convertView.findViewById(R.id.ColCode);
txtRbMjere.setText(MyArrList.get(position).get("RbMjere"));
CheckBox Chk = (CheckBox) convertView.findViewById(R.id.ColChk);
Chk.setTag(MyArrList.get(position).get("RbMjere"));
Chk.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
yourSelectedItems.add(position);
}else{
if(yourSelectedItems.contains(position)){
yourSelectedItems.remove((position));
}
}
}
});
return convertView;
}
}
我应该在代码中更改或添加什么内容?我找到了一些例子,但我不能重复使用它们并在我的代码中实现:/
答案 0 :(得分:5)
为此你必须维护一个数组变量yourSelectedItems[]
来存储你检查过的视图位置..并在你的getView()
方法中添加这样的条件..
if(yourSelectedItems.contains((position))){
check.setChecked(true);
}else{
check.setChecked(false);
}
您可以通过在getView()
..
check.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
yourSelectedItems.add(position);
}else{
if(yourSelectedItems.contains(position)){
yourSelectedItems.remove((position));
}
}
});
答案 1 :(得分:1)
答案 2 :(得分:1)
避免CheckBoxes出现问题 在ListView中,您可以采用布尔值 数组在开头初始化为false 然后使真实的 数组中的相应位置 在ListView中选中复选框。当您在应用程序中向前和向后移动或滚动ListView时,这不会导致复选框的选中状态出现问题。您可以在此处查看示例代码:
答案 3 :(得分:1)
这发生在我身上,我解决了这个问题,
1.创建一个全局布尔arraylist并在构造函数上用MyArrList.size()填充它。
2.当您更改复选框值时,使用视图索引设置arratlist。
3.改变这个:
CheckBox Chk = (CheckBox) convertView.findViewById(R.id.ColChk);
Chk.setTag(MyArrList.get(position).get("RbMjere"));
用这个:
CheckBox Chk = (CheckBox) convertView.findViewById(R.id.ColChk);
Chk.setTag(MyArrList.get(position).get("RbMjere"));
Chk.setChecked(commentTexts.get(position));
答案 4 :(得分:1)
用这个改变你的getview()...
public View getView(final int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = inflater.inflate(R.layout.activity_list_row, null);
}
TextView txtOpis = (TextView) convertView.findViewById(R.id.ColOpis);
txtOpis.setText(MyArrList.get(position).get("OpisMjere") );
TextView txtRbMjere = (TextView) convertView.findViewById(R.id.ColCode);
txtRbMjere.setText(MyArrList.get(position).get("RbMjere"));
CheckBox Chk = (CheckBox) convertView.findViewById(R.id.ColChk);
if(yourSelectedItems.contains((position))){
Chk.setChecked(true);
}else{
Chk.setChecked(false);
}
Chk.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
yourSelectedItems.add(position);
}else{
if(yourSelectedItems.contains(position)){
yourSelectedItems.remove((position));
}
}
}
});
return convertView;
}
答案 5 :(得分:0)
如果您在listView.onItemSelectedListner中使用了getView,那么您将遇到此问题 listView不会将每个对象放在不同的视图中,这就是为什么当你调用getView(2)时它会返回屏幕上第二个列表对象的视图而不是列表第二个对象的视图 ..你必须自己使用类中的一些布尔值或复选框来实现它
答案 6 :(得分:0)
我想我得到的东西......而不是Chk.setOnCheckedChangeListener()
我改为Chk.setOnClickListener(new OnClickListener()
public class SPCMjereAdapter extends BaseAdapter
{
private Context context;
public SPCMjereAdapter(Context c)
{
context = c;
}
public int getCount() {
return MyArrList.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(final int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = inflater.inflate(R.layout.activity_list_row, null);
}
TextView txtOpis = (TextView) convertView.findViewById(R.id.ColOpis);
txtOpis.setText(MyArrList.get(position).get("OpisMjere") );
TextView txtRbMjere = (TextView) convertView.findViewById(R.id.ColCode);
txtRbMjere.setText(MyArrList.get(position).get("RbMjere"));
final CheckBox Chk = (CheckBox) convertView.findViewById(R.id.ColChk);
if(yourSelectedItems.contains((position))){
Chk.setChecked(true);
}else{
Chk.setChecked(false);
}
Chk.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (Chk.isChecked())
{
yourSelectedItems.add(position);
} else
{
if(yourSelectedItems.contains(position)){
yourSelectedItems.remove((position));
}
}
}
});
return convertView;
}
}
现在滚动向上/向下时工作正常。但还有另一个问题,我有点困惑。当滚动并随机选中/取消选中项目程序崩溃和这些错误时,我得到:
FATAL EXCEPTION: main
java.lang.IndexOutOfBoundsException: Invalid index 2, size is 2
at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:251)
at java.util.ArrayList.remove(ArrayList.java:399)
at com.dem.spckontrola.SPCKontrola$SPCMjereAdapter$1.onClick(SPCKontrola.java:830)
at android.view.View.performClick(View.java:4084)
at android.widget.CompoundButton.performClick(CompoundButton.java:100)
at android.view.View$PerformClick.run(View.java:16966)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method
)
确定。我通过添加 yourSelectedItems.remove(yourSelectedItems.indexOf(position));
所以现在正确的解决方案是:
public class SPCMjereAdapter extends BaseAdapter
{
private Context context;
public SPCMjereAdapter(Context c)
{
context = c;
}
public int getCount() {
return MyArrList.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(final int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = inflater.inflate(R.layout.activity_list_row, null);
}
TextView txtOpis = (TextView) convertView.findViewById(R.id.ColOpis);
txtOpis.setText(MyArrList.get(position).get("OpisMjere") );
TextView txtRbMjere = (TextView) convertView.findViewById(R.id.ColCode);
txtRbMjere.setText(MyArrList.get(position).get("RbMjere"));
final CheckBox Chk = (CheckBox) convertView.findViewById(R.id.ColChk);
if(yourSelectedItems.contains((position))){
Chk.setChecked(true);
}else{
Chk.setChecked(false);
}
Chk.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (Chk.isChecked())
{
yourSelectedItems.add(position);
} else
{
if(yourSelectedItems.contains(position)){
yourSelectedItems.remove(yourSelectedItems.indexOf(position));
}
}
}
});
return convertView;
}
}
但是现在我遇到了一个新问题:如何获取所有已检查和未选中项目的列表,其中由于列表视图中的许多项目仅在向下滚动时可见,因此某些复选框(项目)不可见?