【问题标题】:Android - Fragment containg ListView, CursorAdapter using SQLiteAndroid - 使用 SQLite 的包含 ListView、CursorAdapter 的片段
【发布时间】:2015-05-15 01:00:03
【问题描述】:

我有一个包含 listView 和按钮的片段。单击按钮时,将打开一个 DialogFragment,用户可以在其中将数据输入到 EditText 字段中。一旦用户按下setPositiveButton,它就会调用 DBHelper 中的方法将数据插入 SQLite,然后关闭 dialogFragment

我的问题,一旦它关闭 ListView 应该使用列出所有未来输入的新输入进行更新。但是我似乎无法让它工作。我制作了一个 CursorAdapter。有谁知道它为什么不起作用

public class NotesAdapter extends CursorAdapter {

DatabaseHelper help;
private Cursor c;
public NotesAdapter(Context context, Cursor cursor) {
    super(context, cursor, 0);
    c = cursor;
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
    return LayoutInflater.from(context).inflate(R.layout.list_layout, parent, false);
}

@Override
public void bindView(View view, Context context, Cursor cursor) {

    //View
    TextView title = (TextView) view.findViewById(R.id.item_title);
    TextView body = (TextView) view.findViewById(R.id.item_body);
    TextView date = (TextView) view.findViewById(R.id.item_date);
    //Columns to bound into view
    String getTitle = cursor.getString(cursor.getColumnIndexOrThrow("title"));
    String getBody = cursor.getString(cursor.getColumnIndexOrThrow("body"));
    String getDate = cursor.getString(cursor.getColumnIndexOrThrow("date"));

    title.setText(getTitle);
    body.setText(getBody);
    date.setText(getDate);
}

}

查询数据库表注释,如果sharedpref keyvalue username等于column_creator则返回数据,我的查询对吗?

    public Cursor listNotes() {
    SQLiteDatabase db = help.getReadableDatabase();
    String username = session.getUser();
   // Log.v("Cursor Object", DatabaseUtils.dumpCursorToString(listNotes))
    return db.rawQuery("SELECT * FROM " +help.NOTE_TABLE+ "WHERE " +help.CREATOR+ "= '" + username + "' COLLATE NOCASE", null);

 }

public class Notes extends Fragment {

NotesControl control;
NotesAdapter adapter;
ListView listView;
Button add;
public static boolean isDataUpdated=false;

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_notes, container, false);
    add = (Button) view.findViewById(R.id.addbtn);
    control = new NotesControl(getActivity());
    control.open();
    add.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            NotesDialog dialog = new NotesDialog();
            dialog.show(getChildFragmentManager(), "Dialog Fragment");
        }
    });

    return view;
}

public void onActivityCreate(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    populateList();
}

最后将 ListView 附加到我的 Fragment,我创建了一个可以调用来填充 List 的方法

    public void populateList(){
      Cursor cursor = control.listNotes();
      listView = (ListView) getView().findViewById(android.R.id.list);
      NotesAdapter adapter = new NotesAdapter(getActivity(), cursor);

      listView.setAdapter(adapter);

    control.close();
}

【问题讨论】:

    标签: java android android-fragments android-listview android-sqlite


    【解决方案1】:

    当您按下确定关闭对话框时,您必须通知光标数据已更改,您必须自行更新..

    Edit1:
    也将光标声明为该类的数据成员:)
    我想让你做如下的事情 实施:

    public class Notes extends Fragment {
    
    NotesControl control;
    NotesAdapter adapter;
    ListView listView;
    Button add;
    Cursor cursor;
    public static boolean isDataUpdated=false;
    
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_notes, container, false);
        add = (Button) view.findViewById(R.id.addbtn);
    
        add.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                NotesDialog dialog = new NotesDialog();
                dialog.show(getChildFragmentManager(), "Dialog Fragment");
            }
        });
    
        return view;
    }
    
    public void onActivityCreate(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    
        control = new NotesControl(getActivity());
        control.open();
        populateList();
    }
    
    public void populateList(){
          cursor = control.listNotes();
          listView = (ListView) getView().findViewById(android.R.id.list);
          adapter = new NotesAdapter(getActivity(), cursor);
    
          listView.setAdapter(adapter);
    
        control.close();
    }
    @Override
    public void onResume(){
        if(isDataUpdated)
       {
         cursor.requery();
         adapter.notifyDataSetChanged();
         Notes.isDataUpdated = false;
        }
    
    }
    

    重要
    在您的 DialogFragment Ok 按钮单击事件中添加以下 sn-p

    Notes.isDataUpdated = true;
    

    PS:
    requery() 方法已被弃用。在掌握下一个逻辑步骤的窍门后,将是用户 CursorLoaders

    【讨论】:

    • 我将把那个方法放在哪里,我应该在哪里调用它
    • 像使用 populateList() 方法一样定义它...并在您创建的对话框的 click 方法中调用它以在数据库中进行更改....对于按 OK 的示例..
    • 还有一件事你必须先通过插入查询在数据库中添加新数据..
    • 你迷路了,我有一个对话框片段,用户在其中输入 EditText,然后当他按 OK 时,它调用 dbhelper insert 并将数据插入 SQLite 数据库。从那里 dialogFragment 将被解除,它应该在列表视图中显示用户输入,但它没有显示
    • 请更新您有问题的代码..然后我很容易帮助您完成 90% 的工作
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-04-01
    • 2012-07-06
    • 1970-01-01
    • 2015-05-08
    • 2016-08-20
    • 2011-09-24
    • 1970-01-01
    相关资源
    最近更新 更多