在片段内的RecyclerView中从SQLite检索数据

时间:2018-09-16 06:16:56

标签: android android-studio android-recyclerview android-fragmentactivity

我有一个包含自定义Tablayout的活动。

Tablayot具有pageViewer并链接到一个片段。

我想在SQLite的片段中显示来自RecyclerView数据库的数据。

这是我的代码:

DatabaseHelper.java:

package ir.shirazmetro;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DatabaseHelper extends SQLiteOpenHelper {
private static final String TAG = "DatabaseHelper";

public static final String DATABASE_NAME = "MetroDB.db";
public static final String TABLE_NAME = "estation";
public static final String COL_1 = "station";
public static final String COL_2 = "time";
public static final String COL_3 = "line";

public DatabaseHelper(Context context) {
    super(context, DATABASE_NAME, null, 1);
}

@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL("create table " + TABLE_NAME + " (COL_1,COL_2,COL_3)");
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
    onCreate(db);
}

public boolean insertData(String station, String time, String line) {
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues contentValues = new ContentValues();
    contentValues.put(COL_2, time);
    long result = db.insert(TABLE_NAME, null, contentValues);
    if (result == -1)
        return false;
    else
        return true;
}


public Cursor getData() {
    SQLiteDatabase db = this.getWritableDatabase();
    return db.rawQuery("select "+COL_2+" from " + TABLE_NAME, null);
}

}

我的活动(station.java):

package ir.shirazmetro.views.activities;
import android.content.Intent;
import android.support.v4.view.ViewPager;
import android.support.design.widget.TabLayout;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import java.util.ArrayList;
import java.util.List;
import ir.shirazmetro.DatabaseHelper;
import ir.shirazmetro.R;
import ir.shirazmetro.other.components.ButtonCell;
import ir.shirazmetro.other.components.TextViewCell;
import ir.shirazmetro.views.adapters.BasePagerAdapter;
import ir.shirazmetro.views.adapters.MoviesAdapter;

public class estation extends BaseActivity {
Toolbar mToolbar;
private TabLayout tbLayout;
private ViewPager vPager;

private List<DatabaseHelper> movies = new ArrayList<DatabaseHelper>();
private RecyclerView recyclerView;
private MoviesAdapter adapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_estation);

    mToolbar = findViewById(R.id.tlbr1);

    setSupportActionBar(mToolbar);

        initView();
        setupWithViewPager();
        tbLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });

        recyclerView = findViewById(R.id.myRecycler);
        adapter = new MoviesAdapter(movies);

        recyclerView.setLayoutManager(new LinearLayoutManager(estation.this));
        recyclerView.setAdapter(adapter);
        DatabaseHelper getData = (DatabaseHelper) new DatabaseHelper(this).getData();
        movies.add(getData);
        adapter.notifyDataSetChanged();

    }

}

private void setupWithViewPager() {
    BasePagerAdapter basePagerAdapter = new BasePagerAdapter(this, getSupportFragmentManager());
    vPager.setAdapter(basePagerAdapter);
    tbLayout.setupWithViewPager(vPager);

}

private void initView() {
    vPager = findViewById(R.id.view_pager);
    tbLayout = findViewById(R.id.tab_layout);
    backBtn = findViewById(R.id.backBtn);

    backBtn.setOnClickListener((View.OnClickListener) this);
}

}

这是我的适配器:

package ir.shira.views.adapters;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import java.util.List;
import ir.shira.DatabaseHelper;
import ir.shira.R;
import ir.shira.models.movie;


public class MoviesAdapter extends RecyclerView.Adapter<MoviesAdapter.MovieViewHolder> {

private List<movie> movies;
private Context context;

public MoviesAdapter(List<DatabaseHelper> movies) {
    movies = movies;
}
@NonNull
@Override
public MovieViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.fragment_to_dastgheyb,parent,false);
    return new MovieViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull MovieViewHolder holder, int position) {
    holder.txtTime.setText(movies.get(position).getTime());
}
@Override
public int getItemCount() {
    return movies.size();
}
public class MovieViewHolder extends RecyclerView.ViewHolder
{
    public TextView txtTime;

    public MovieViewHolder(View itemView)
    {
        super(itemView);
        txtTime = itemView.findViewById(R.id.textView);
    }
}

}

运行项目时我的应用程序崩溃了,有什么主意吗?

2 个答案:

答案 0 :(得分:0)

movies.get(position).getTime()检索DatabaseHelper,我认为这是已检查的异常。

答案 1 :(得分:0)

您听错了声音

  

创建列表对象时

private List<DatabaseHelper> movies = new ArrayList<DatabaseHelper>();

// ..列表类型DatabaseHelper但是您想要电影吗?所以把这行改成这样

private List<movie> movies = new ArrayList<movie>();

//电影是你的模特