【问题标题】:CursorAdapter in ListviewListview 中的 CursorAdapter
【发布时间】:2014-05-17 21:53:49
【问题描述】:

我正在使用 CursorAdapter 在列表视图中读取数据库。 我在列表的每个项目中都有一个复选框,当用户选中该复选框时,我的数据库中的收藏列将更改为“是”并将该项目添加到收藏夹中。

一切正常,最喜欢的列发生了变化,但是当我向上和向下滚动列表时,复选框将被取消选中。 如果您重新启动应用程序,复选框已被选中

这个问题我该怎么办:

抱歉我的英语不好:

CursorAdapter 类:

public class MyAdapter extends CursorAdapter {

    Context b;   
    LayoutInflater inflater;
    @SuppressWarnings("deprecation")
    public MyAdapter(Context context, Cursor c) {
        super(context, c);
        inflater = LayoutInflater.from(context);
        b= (Context) context;
    }

    @SuppressWarnings("unused")
    @Override
    public void bindView(View view, Context context, final Cursor cursor) {
        // TODO Auto-generated method stub

        TextView tv1 = (TextView)view.findViewById(R.id.txt_name);
        TextView tv2 = (TextView)view.findViewById(R.id.txt_numer);

        tv1.setText(cursor.getString(2));
        tv2.setText(cursor.getString(3));

        final int pos = cursor.getPosition();

        final CheckBox repeatChkBx = (CheckBox)view.findViewById(R.id.favorite_check);

        String me = cursor.getString(cursor.getColumnIndex("like"));

        if (me.equals("yes")) {
            repeatChkBx.setChecked(true);
        } else {
            repeatChkBx.setChecked(false);
        }

        repeatChkBx.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View view) {
                MyDatabase MyDatabase = new MyDatabase(b);
                SQLiteDatabase mydb = MyDatabase.getWritableDatabase();
                cursor.moveToPosition(pos);

                if (repeatChkBx.isChecked()) {                   
                    mydb.execSQL("update list set like = 'yes' where id = " + cursor.getString(1));

                }else{
                    mydb.execSQL("update list set like = 'no' where id = " + cursor.getString(1));           

                }
            }
        });

        }

        protected Context getActivity() {
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public View newView(Context context, Cursor cursor, ViewGroup parent) {
            return inflater.inflate(R.layout.item, parent, false);
        }
    }

截图:

【问题讨论】:

标签: android listview checkbox android-cursoradapter


【解决方案1】:

选中的项目不会被回收。 您必须将选中的项目保存到某种数组 - 动态或静态。布尔数组非常适合此目的。

Boolean[] myCheckedItems = new Boolean[SIZE];

【讨论】:

    【解决方案2】:

    问题是当你更新你的数据库时只是数据库要更新而不是游标适应你的游标适配器,所以你必须使用

     changeCursor(newcursor);
    

    更新数据库后在适配器中。 希望对您有所帮助。

    【讨论】:

      【解决方案3】:

      我将使用 listAdapter 来实现在 ListView 中使用多个选择。如果当前选择了此元素,则创建以下模型,其中包含名称和信息。

      首先创建模型类:

      public class Model {
      
          private String name;
          private boolean selected;
      
          public Model(String name) {
              this.name = name;
              selected = false;
          }
      
          public String getName() {
              return name;
          }
      
          public void setName(String name) {
              this.name = name;
          }
      
          public boolean isSelected() {
              return selected;
          }
      
          public void setSelected(boolean selected) {
              this.selected = selected;
          }
      
      } 
      

      在 layouts 文件夹中创建以下 xml 文件:

      <?xml version="1.0" encoding="utf-8"?>
      <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="wrap_content" >
      
      <TextView
          android:id="@+id/label"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="@+id/label"
          android:textSize="30px" >
      </TextView>
      
      <CheckBox
          android:id="@+id/check"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_alignParentRight="true"
          android:layout_marginLeft="4px"
          android:layout_marginRight="10px" >
      </CheckBox>
      </RelativeLayout> 
      

      创建以下适配器。这个适配器在 Checkbox 视图上添加了一个监听器。如果选中该复选框,则模型的基础数据将更改。 Checkbox 通过 getTag() 方法获取对应的模型元素。

      import java.util.List;
      import android.app.Activity;
      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;
      
      public class InteractiveArrayAdapter extends ArrayAdapter<Model> {
      
          private final List<Model> list;
          private final Activity context;
      
          public InteractiveArrayAdapter(Activity context, List<Model> list) {
              super(context, R.layout.rowbuttonlayout, list);
              this.context = context;
              this.list = list;
          }
      
          static class ViewHolder {
              protected TextView text;
              protected CheckBox checkbox;
          }
      
          @Override
          public View getView(int position, View convertView, ViewGroup parent) {
              View view = null;
              if (convertView == null) {
                  LayoutInflater inflator = context.getLayoutInflater();
                  view = inflator.inflate(R.layout.rowbuttonlayout, null);
                  final ViewHolder viewHolder = new ViewHolder();
                  viewHolder.text = (TextView) view.findViewById(R.id.label);
                  viewHolder.checkbox = (CheckBox) view.findViewById(R.id.check);
                  viewHolder.checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
      
                  @Override
                  public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                Model element = (Model) viewHolder.checkbox.getTag();
                element.setSelected(buttonView.isChecked());
      
                }
              });
              view.setTag(viewHolder);
              viewHolder.checkbox.setTag(list.get(position));
              } else {
                  view = convertView;
                  ((ViewHolder) view.getTag()).checkbox.setTag(list.get(position));
              }
              ViewHolder holder = (ViewHolder) view.getTag();
              holder.text.setText(list.get(position).getName());
              holder.checkbox.setChecked(list.get(position).isSelected());
          return view;
          }
      }
      

      最后,您需要将您的活动更改为以下内容:

      import java.util.ArrayList;
      import java.util.List;
      import android.app.ListActivity;
      import android.os.Bundle;
      import android.widget.ArrayAdapter;
      
      public class MyList extends ListActivity {
      
      /** Called when the activity is first created. */
      public void onCreate(Bundle icicle) {
          super.onCreate(icicle);
      
          // create an array of Strings, that will be put to our ListActivity
          ArrayAdapter<Model> adapter = new InteractiveArrayAdapter(this, getModel());
          setListAdapter(adapter);
      }
      
      private List<Model> getModel() {
          List<Model> list = new ArrayList<Model>();
          //The following elements need to be changed by your elements.
          list.add(get("List number 1"));
          list.add(get("List number 2"));
          list.add(get("List number 3"));
          list.add(get("List number 4"));
          list.add(get("List number 5"));
          list.add(get("List number 6"));
      
          // Initially select one of the items
          list.get(1).setSelected(true);
          return list;
      }
      
      private Model get(String s) {
            return new Model(s);
      }
      

      }

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-05-27
        • 1970-01-01
        • 1970-01-01
        • 2011-12-12
        • 2018-10-07
        • 2016-11-27
        相关资源
        最近更新 更多