在片段中调用数据库(this)

时间:2017-10-17 01:13:24

标签: android database sqlite fragment

我正在尝试制作一个"目录"通过从文件夹资产中的数据库获取数据并将其显示在recycleviewer中。 这个活动已经在它自己的工作上我的意思不是片段,但我想让它在底栏中工作,我按下一个项目,然后这个屏幕出现。 当我试着打电话时我认为这个问题 - > ____(这个) 我是新手,我认为它不允许片段,我不确定.. 这是代码:

public class CatalogoFragment extends Fragment {
private static RecyclerView.Adapter adapter;
private RecyclerView.LayoutManager layoutManager;
private static RecyclerView recyclerView;
public static ArrayList<DictObjectModel> data;
DatabaseHelper db ;
ArrayList<String> nombrelist;
ArrayList<String> descripcionlist;
LinkedHashMap<String,String> namelist;
SearchView searchView;
private Context mContexto=null;
private View rootView = null;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //setContentView(R.layout.activity_main);
    recyclerView = (RecyclerView) rootView.findViewById(R.id.my_recycler_view);
    recyclerView.setHasFixedSize(true);
    db= new DatabaseHelper(this);
    searchView = (SearchView) rootView.findViewById(R.id.searchView);
    searchView.setQueryHint("Buscar");
    searchView.setQueryRefinementEnabled(true);
    layoutManager = new LinearLayoutManager(this);
    recyclerView.setLayoutManager(layoutManager);
    recyclerView.setItemAnimator(new DefaultItemAnimator());
    data = new ArrayList<DictObjectModel>();
    fetchData();

    searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextSubmit(String query) {
            return  false;
        }

        @Override
        public boolean onQueryTextChange(String newText) {
            newText = newText.toLowerCase();

            final ArrayList<DictObjectModel> filteredList = new ArrayList<DictObjectModel>();

            for (int i = 0; i < nombrelist.size(); i++) {

                final String text = nombrelist.get(i).toLowerCase();
                if (text.contains(newText)) {

                    filteredList.add(new DictObjectModel(nombrelist.get(i),descripcionlist.get(i)));
                }
            }
            adapter = new CustomAdapter(filteredList);
            recyclerView.setAdapter(adapter);


            return true;
        }
    });
    mContexto = getActivity();
}

private void fragment_catalogo(){

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    rootView = inflater.inflate(R.layout.fragment_catalogo, container, false);
    fragment_catalogo();

    return rootView;
}

public void fetchData()
{
    db = new DatabaseHelper(mContexto);
    try {

        db.createDataBase();
        db.openDataBase();

    }
    catch (Exception e)
    {
        e.printStackTrace();
    }


    namelist=new LinkedHashMap<>();
    int ii;
    SQLiteDatabase sd = db.getReadableDatabase();
    Cursor cursor = sd.query("Productos" ,null, null, null, null, null, null);
    ii=cursor.getColumnIndex("nombre");
    nombrelist=new ArrayList<String>();
    descripcionlist= new ArrayList<String>();
    while (cursor.moveToNext()){
        namelist.put(cursor.getString(ii), cursor.getString(cursor.getColumnIndex("descripcion")));
    }
    Iterator entries = namelist.entrySet().iterator();
    while (entries.hasNext()) {
        Map.Entry thisEntry = (Map.Entry) entries.next();
        nombrelist.add(String.valueOf(thisEntry.getKey()));
        //QUITAR el -
        descripcionlist.add(""+String.valueOf(thisEntry.getValue()));
    }

    for (int i = 0; i < nombrelist.size(); i++) {
        data.add(new DictObjectModel(nombrelist.get(i), descripcionlist.get(i)));
    }

    adapter = new CustomAdapter(data);
    recyclerView.setAdapter(adapter);
}

}

问题在于公共空白onCreate(接下来的两行)

db= new DatabaseHelper(this);

layoutManager = new LinearLayoutManager(this);

1 个答案:

答案 0 :(得分:0)

首先按照片段生命周期在oncreateView()方法中移动findViewById。

this替换为getActivity()