如何将SQlite数据库中的数据放入Recyclerview

时间:2017-02-20 14:27:14

标签: android android-recyclerview

任何人都可以帮忙解决这个问题吗?我想要的是将我的sqlite数据库中的数据显示到recyclerview中,但我收到了错误。

这是我的代码:

片段活动

public class pet_tab extends Fragment {
    RecyclerView recyclerView;
    Register_pet_database register_pet_database;
    ArrayList<db_getItem> arrayList=new ArrayList<>();
    Cursor cursor;
    reg_Adapter  reg_adapter;

    public pet_tab() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
       View view= inflater.inflate(R.layout.fragment_pet_tab, container, false);
        recyclerView=(RecyclerView)view.findViewById(R.id.reg_pet_recycler);
        loadDb();
        return view;
    }
    public void loadDb(){
        register_pet_database=new Register_pet_database(getActivity());
        cursor=register_pet_database.queryData("select * from Pet_Registered");
        if(cursor != null) {
            if (cursor.moveToFirst()) {
                do{
                    db_getItem item=new db_getItem();
                    item.setPet_name(cursor.getString(2));
                    item.setPet_type(cursor.getString(3));
                    item.setPet_breed(cursor.getString(4));
                    arrayList.add(item);
                }while(cursor.moveToNext());
            }

        }
        LinearLayoutManager linearLayoutManager=new LinearLayoutManager(getContext());
        reg_adapter=new reg_Adapter(getActivity(),arrayList);
        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(linearLayoutManager);
        recyclerView.setAdapter(reg_adapter);
    }


}

我的适配器

   package petguide.com.petguide101.database_item;

import android.app.Activity;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.Collection;
import java.util.Collections;
import java.util.List;

import petguide.com.petguide101.R;

/**
 * Created by smartnote on 2/20/2017.
 */

public class reg_Adapter  extends RecyclerView.Adapter<reg_ViewHolder>{
    public Activity activity;
    List<db_getItem> items= Collections.emptyList();
    public reg_Adapter(Activity activity, List<db_getItem> items){
        this.activity=activity;
        this.items=items;
    }
    public reg_ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.reg_pet_recycler,parent,false);
        return new reg_ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(reg_ViewHolder holder, int position) {
        holder.pet_name.setText(items.get(position).getPet_name());
        holder.pet_type.setText(items.get(position).getPet_type());
        holder.pet_breed.setText(items.get(position).getPet_breed());
    }

    @Override
    public int getItemCount() {
        return items.size();
    }
}

我的viewHolder

    package petguide.com.petguide101.database_item;

import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import petguide.com.petguide101.R;

/**
 * Created by smartnote on 2/20/2017.
 */

public class reg_ViewHolder extends RecyclerView.ViewHolder{
TextView pet_name;
TextView pet_type;
TextView pet_breed;

    public reg_ViewHolder(View itemView) {
        super(itemView);
        pet_name=(TextView)itemView.findViewById(R.id.reg_pet_name);
        pet_type=(TextView)itemView.findViewById(R.id.reg_pet_type);
        pet_breed=(TextView)itemView.findViewById(R.id.reg_pet_breed);
    }
}

**这是我在日志中得到的内容**

Process: petguide.com.petguide101, PID: 6857
              java.lang.NullPointerException: Attempt to invoke virtual method 'android.database.Cursor android.database.sqlite.SQLiteDatabase.query(java.lang.String, java.lang.String[])' on a null object reference
                  at petguide.com.petguide101.database.Register_pet_database.queryData(Register_pet_database.java:56)
                  at petguide.com.petguide101.pet_tab.loadDb(pet_tab.java:56)
                  at petguide.com.petguide101.pet_tab.onCreateView(pet_tab.java:44)
                  at android.support.v4.app.Fragment.performCreateView(Fragment.java:2189)
                  at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1299)
                  at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1528)
                  at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1595)
                  at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:757)
                  at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2355)
                  at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2146)
                  at android.support.v4.app.FragmentManagerImpl.optimizeAndExecuteOps(FragmentManager.java:2098)
                  at android.support.v4.app.FragmentManagerImpl.execSingleAction(FragmentManager.java:1979)
                  at android.support.v4.app.BackStackRecord.commitNowAllowingStateLoss(BackStackRecord.java:626)
                  at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:143)
                  at android.support.v4.view.ViewPager.populate(ViewPager.java:1268)
                  at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:668)
                  at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:630)
                  at android.support.v4.view.ViewPager.setCurrentItem(ViewPager.java:611)
                  at android.support.design.widget.TabLayout$ViewPagerOnTabSelectedListener.onTabSelected(TabLayout.java:2178)
                  at android.support.design.widget.TabLayout.dispatchTabSelected(TabLayout.java:1155)
                  at android.support.design.widget.TabLayout.selectTab(TabLayout.java:1148)
                  at android.support.design.widget.TabLayout.selectTab(TabLayout.java:1118)
                  at android.support.design.widget.TabLayout$Tab.select(TabLayout.java:1413)
                  at android.support.design.widget.TabLayout$TabView.performClick(TabLayout.java:1523)
                  at android.view.View$PerformClick.run(View.java:21147)
                  at android.os.Handler.handleCallback(Handler.java:739)
                  at android.os.Handler.dispatchMessage(Handler.java:95)
                  at android.os.Looper.loop(Looper.java:148)
                  at android.app.ActivityThread.main(ActivityThread.java:5417)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

0 个答案:

没有答案