Android的 - 片段containg的ListView,CursorAdapter的使​​用SQLite [英] Android - Fragment containg ListView, CursorAdapter using SQLite

查看:167
本文介绍了Android的 - 片段containg的ListView,CursorAdapter的使​​用SQLite的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含ListView和按钮片段。当单击该按钮DialogFragment将打开,用户可以将数据输入的EditText字段。一旦用户presses setPositiveButton 它DBHelper调用的方法将数据插入到SQLite的话驳斥dialogFragment

我的问题,一旦解聘ListView控件应与新的输入列出了所有未来的投入更新。不过,我似乎无法得到它的工作。我已经做了的CursorAdapter。有谁知道为什么它不是工作

 公共类NotesAdapter扩展的CursorAdapter {DatabaseHelper帮助;
私人光标C;
公共NotesAdapter(上下文的背景下,光标光标){
    超级(上下文,光标,0);
    C =光标;
}
@覆盖
公共查看NewView的(上下文的背景下,光标光标的ViewGroup父){
    返回LayoutInflater.from(上下文).inflate(R.layout.list_layout,父母,假);
}@覆盖
公共无效bindView(查看视图,上下文的背景下,光标光标){    //视图
    TextView的标题=(TextView中)view.findViewById(R.id.item_title);
    TextView的身体=(TextView中)view.findViewById(R.id.item_body);
    TextView的日期=(TextView中)view.findViewById(R.id.item_date);
    //列开往进入视野
    字符串的getTitle = cursor.getString(cursor.getColumnIndexOrThrow(标题));
    字符串getBody = cursor.getString(cursor.getColumnIndexOrThrow(身体));
    字符串GETDATE = cursor.getString(cursor.getColumnIndexOrThrow(DATE));    title.setText(的getTitle);
    body.setText(getBody);
    date.setText(GETDATE);
}

}

查询数据库表的注意事项,如果共享preF键值名等于column_creator然后返回数据,是我的查询吧?

 公共光标listNotes(){
    SQLiteDatabase分贝= help.getReadableDatabase();
    字符串username = session.getUser();
   // Log.v(光标对象,DatabaseUtils.dumpCursorToString(listNotes))
    返回db.rawQuery(SELECT * FROM+ help.NOTE_TABLE +WHERE+ help.CREATOR +='+用户名+'COLLATE NOCASE,NULL); }公共课堂笔记扩展片段{NotesControl控制;
NotesAdapter适配器;
ListView控件的ListView;
按钮添加;
公共静态布尔isDataUpdated = FALSE;公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,捆绑savedInstanceState){
    查看查看= inflater.inflate(R.layout.fragment_notes,集装箱,FALSE);
    添加=(按钮)view.findViewById(R.id.addbtn);
    控制=新NotesControl(getActivity());
    control.open();
    add.setOnClickListener(新View.OnClickListener(){
        公共无效的onClick(视图v){
            NotesDialog对话框=新NotesDialog();
            dialog.show(getChildFragmentManager(),对话片段);
        }
    });    返回视图。
}公共无效onActivityCreate(捆绑savedInstanceState){
    super.onActivityCreated(savedInstanceState);
    populateList();
}

最后ListView控件连接到我的片段,我做了一个方法我可以打电话popluate列表

 公共无效populateList(){
      光标光标= control.listNotes();
      ListView控件=(ListView控件)getView()findViewById(android.R.id.list)。
      NotesAdapter适配器=新NotesAdapter(getActivity(),光标);      listView.setAdapter(适配器);    control.close();
}


解决方案

由pressing确定当您关闭该对话框说,那么你必须通知光标数据已经改变了你必须更新自己..

EDIT1:结果声明游标作为类的数据成员太:)结果
我要你做类似下面的
执行:

 公共课堂笔记扩展片段{NotesControl控制;
NotesAdapter适配器;
ListView控件的ListView;
按钮添加;
光标光标;
公共静态布尔isDataUpdated = FALSE;公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,捆绑savedInstanceState){
    查看查看= inflater.inflate(R.layout.fragment_notes,集装箱,FALSE);
    添加=(按钮)view.findViewById(R.id.addbtn);    add.setOnClickListener(新View.OnClickListener(){
        公共无效的onClick(视图v){
            NotesDialog对话框=新NotesDialog();
            dialog.show(getChildFragmentManager(),对话片段);
        }
    });    返回视图。
}公共无效onActivityCreate(捆绑savedInstanceState){
    super.onActivityCreated(savedInstanceState);    控制=新NotesControl(getActivity());
    control.open();
    populateList();
}公共无效populateList(){
      光标= control.listNotes();
      ListView控件=(ListView控件)getView()findViewById(android.R.id.list)。
      适配器=新NotesAdapter(getActivity(),光标);      listView.setAdapter(适配器);    control.close();
}
@覆盖
公共无效onResume(){
    如果(isDataUpdated)
   {
     cursor.requery();
     adapter.notifyDataSetChanged();
     Notes.isDataUpdated = FALSE;
    }}

重要提示结果
在DialogFragment确定按钮单击事件中添加下面的代码片段

  Notes.isDataUpdated = TRUE;

PS:结果的方法重新查询()被depricated。得到这个下一个逻辑步骤的窍门后,将用户CursorLoaders

I have a Fragment that contains a listView and button. When the button is clicked a DialogFragment will open where the user can input data into EditText fields. Once the user presses setPositiveButton it calls method in DBHelper to insert data into SQLite then dismisses dialogFragment

My problem, once it dismisses the ListView should update with the new input listing all future inputs. However I cant seem to get it to work. I have made a CursorAdapter. Does anyone know why it isnt working

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);
}

}

Query the database table notes, if sharedpref keyvalue username equals column_creator then return data, is my query right?

    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();
}

Finally attaching the ListView to my Fragment, I made a method I can call to popluate the 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();
}

解决方案

When you dismiss the dialog say by pressing OK then you have to notify the cursor that data has been changed you have to update yourself..

Edit1:
Declare the cursor as data member of the class too :)
i want you to do something like the following Implementation:

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;
    }

}

Important
in your DialogFragment Ok button click event add the following snippet

Notes.isDataUpdated = true;

PS:
the method requery() is depricated. after getting the hang of this next logical step would be to user CursorLoaders

这篇关于Android的 - 片段containg的ListView,CursorAdapter的使​​用SQLite的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆