我想制作一个 recyclerview,将数据显示到 SQLITE 数据库中的新活动

我正在做一个项目,我有一个包含 id、标题、内容的 sqlite 数据库能够从数据库中读取内容。希望我的问题很清楚!谢谢。


这是我的 DatabaseHelper 类


DatabaseHelper.java


public class DatabaseHelper extends SQLiteOpenHelper {


public static final String DBNAME = "alQais.db";


private Context context;

private SQLiteDatabase sqLiteDatabase;


public DatabaseHelper(Context mContext){

    super(mContext, DBNAME, null, 1);

    this.context = mContext;

}


@Override

public void onCreate(SQLiteDatabase db) {

}


@Override

public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {


}


public void openDatabase(){

    String dbPath = context.getDatabasePath(DBNAME).getPath();

    if(sqLiteDatabase != null && sqLiteDatabase.isOpen()){

        return;

    }

    sqLiteDatabase = SQLiteDatabase.openDatabase(dbPath, null,SQLiteDatabase.OPEN_READWRITE);

}


public void closeDatabase(){

    if(sqLiteDatabase != null){

        sqLiteDatabase.close();

    }

}


public ArrayList getAllTitles(){

    ArrayList arrayList = new ArrayList();

    openDatabase();

    Cursor cursor = sqLiteDatabase.rawQuery("select * from muallaqat", null);

    cursor.moveToFirst();

    while (!cursor.isAfterLast()){

        arrayList.add(cursor.getString(cursor.getColumnIndex("title")));

        cursor.moveToNext();

    }

    cursor.close();

    closeDatabase();

    return arrayList;

}


public String getMuallaqa(String title){

    String muallaqat;

    openDatabase();

    Cursor cursor = sqLiteDatabase.rawQuery("select * from muallaqat where title like '" + title + "'", null);

    cursor.moveToFirst();

    muallaqat = cursor.getString(cursor.getColumnIndex("muallaqa"));

    cursor.close();

    closeDatabase();

    return muallaqat;

}}





斯蒂芬大帝
浏览 128回答 3
3回答

慕森卡

供参考遵循步骤1)例如为id、title、content创建setter和getter方法public class settter&nbsp;&nbsp;{int id;String name;String content;public int getId() {&nbsp; &nbsp; return id;}public void setId(int id) {&nbsp; &nbsp; this.id = id;}public String getName() {&nbsp; &nbsp; return name;}public void setName(String name) {&nbsp; &nbsp; this.name = name;}public String getContent() {&nbsp; &nbsp; return content;}public void setContent(String content) {&nbsp; &nbsp; this.content = content;&nbsp; &nbsp;}&nbsp;}2)在xml mainactivity中添加recyclerview<androidx.recyclerview.widget.RecyclerView&nbsp; &nbsp; android:id="@+id/recycler_view"&nbsp; &nbsp; android:layout_width="match_parent"&nbsp; &nbsp; android:layout_height="wrap_content"&nbsp; &nbsp; &nbsp;/>3)在 MainActivity 中删除所有 listview 相关代码并添加下面给出的代码&nbsp;RecyclerView recyclerView=(RecyclerView)findViewById(R.id.recycler_view2);&nbsp; &nbsp; recyclerView.setLayoutManager(new LinearLayoutManager(this));&nbsp; &nbsp; &nbsp; &nbsp; //make array list object with setter class&nbsp; &nbsp; &nbsp; &nbsp; ArrayList<setter> name =db.getAllTitles();&nbsp; &nbsp; &nbsp; &nbsp;//new adapter(ArrayList<setter>, context) ,so initiate adapter&nbsp; &nbsp; &nbsp; &nbsp; recyclerView.setAdapter(new adapter(name,getApplicationContext()));&nbsp; &nbsp; &nbsp; &nbsp; //draw line&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; recyclerView.addItemDecoration(new&nbsp;&nbsp; &nbsp; &nbsp;DividerItemDecoration(recyclerView.getContext(), DividerItemDecoration.VERTICAL));4)创建布局文件来保存你的数据并在recyclerview中膨胀名称:recycler_content_holder.xml<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"android:layout_height="50dp"android:orientation="vertical"><LinearLayout&nbsp; &nbsp; android:layout_width="match_parent"&nbsp; &nbsp; android:layout_height="50dp"&nbsp; &nbsp; android:id="@+id/recycler_content_holder"&nbsp; &nbsp; >&nbsp; &nbsp; <TextView&nbsp; &nbsp; &nbsp; &nbsp; android:layout_width="50dp"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_height="match_parent"&nbsp; &nbsp; &nbsp; &nbsp; android:gravity="center"&nbsp; &nbsp; &nbsp; &nbsp; android:id="@+id/tv_id"/>&nbsp; &nbsp; <TextView&nbsp; &nbsp; &nbsp; &nbsp; android:layout_width="100dp"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_height="match_parent"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_marginLeft="30dp"&nbsp; &nbsp; &nbsp; &nbsp; android:id="@+id/tv_title"&nbsp; &nbsp; &nbsp; &nbsp; android:gravity="center"/>&nbsp; &nbsp; <TextView&nbsp; &nbsp; &nbsp; &nbsp; android:layout_width="match_parent"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_height="match_parent"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_marginLeft="30dp"&nbsp; &nbsp; &nbsp; &nbsp; android:gravity="center|right"&nbsp; &nbsp; &nbsp; &nbsp; android:id="@+id/tv_content"/></LinearLayout>5)创建适配器类public class adapter extends RecyclerView.Adapter<adapter.AdapterViewHolder>{ArrayList<settter> data;Context context;public adapter(ArrayList<settter> data,Context context){&nbsp; &nbsp; this.data=data;&nbsp; &nbsp; this.context=context;}@NonNull@Overridepublic AdapterViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i){&nbsp; &nbsp; LayoutInflater layoutInflater=LayoutInflater.from(viewGroup.getContext());&nbsp; &nbsp; View view=layoutInflater.inflate(R.recycler_content_holder,viewGroup,false);&nbsp; &nbsp; return new ProgramingViewHolder(view);}@Overridepublic void onBindViewHolder(@NonNull ViewHolder viewHolder, final int i){&nbsp; &nbsp; viewHolder.id.setText(""+data.get(i).getId());&nbsp; &nbsp; viewHolder.title.setText(data.get(i).getTitle());&nbsp; &nbsp; viewHolder.content.setText(data.get(i).getContent());&nbsp; &nbsp; viewHolder.linearLayout.setOnClickListener(new View.OnClickListener()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public void onClick(View v)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String title = String.valueOf(data.get(i).getTitle());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Intent intent = new Intent(context, Main2Activity.class);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; intent.putExtra("title", title);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; startActivity(intent);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; });}@Overridepublic int getItemCount() {&nbsp; &nbsp; return data.size();}//class programingViewHolderpublic class AdapterViewHolder extends RecyclerView.ViewHolder{&nbsp; &nbsp; TextView id,title,content;&nbsp; &nbsp; LinearLayout linearLayout;&nbsp; &nbsp; public AdapterViewHolder(@NonNull View itemView) {&nbsp; &nbsp; &nbsp; &nbsp; super(itemView);&nbsp; &nbsp; &nbsp; &nbsp; id=(TextView)itemView.findViewById(R.id.tv_id);&nbsp; &nbsp; &nbsp; &nbsp; title=(TextView)itemView.findViewById(R.id.tv_mtrNo);&nbsp; &nbsp; &nbsp; &nbsp; content=(TextView)itemView.findViewById(R.id.tv_nm);&nbsp; &nbsp; &nbsp; &nbsp; linearLayout=(LinearLayout)itemView.findViewById(R.id.recycler_content_holder);&nbsp; &nbsp; }&nbsp; }&nbsp;}6) DatabaseHelper 中的更改方法public ArrayList<setter> getAllTitles(){&nbsp; &nbsp; ArrayList<setter> arrayList = new ArrayList();&nbsp; &nbsp; openDatabase();&nbsp; &nbsp; setter set=new setter();&nbsp; &nbsp; Cursor cursor = sqLiteDatabase.rawQuery("select * from muallaqat", null);&nbsp; &nbsp; cursor.moveToFirst();&nbsp; &nbsp; while (!cursor.isAfterLast()){&nbsp; &nbsp; &nbsp; &nbsp; set.setTitle(cursor.getString(cursor.getColumnIndex("title")));&nbsp; &nbsp; &nbsp; &nbsp; //add object to arralist&nbsp; &nbsp; &nbsp; &nbsp; arrayList.add(set);&nbsp; &nbsp; &nbsp; &nbsp; cursor.moveToNext();&nbsp; &nbsp; }&nbsp; &nbsp; cursor.close();&nbsp; &nbsp; closeDatabase();&nbsp; &nbsp; return arrayList;}只需复制粘贴即可理解,希望对您有所帮助。

12345678_0001

如果我理解正确的话,您想对来自 SQLite 数据库的数据使用 recyclerview 吗?当您单击某个项目并使用该项目的数据填充它时,您想打开一个新活动吗?看看这篇文章RecyclerView 与 Cursor Adapter 的实现

素胚勾勒不出你

只需做一件事,从数据库中获取 id 和标题,并在 recyclerview 中显示标题。并且当 onItemClick onItemClick on recyclerview 将特定位置的 id 传递给下一个活动。并在下一个活动中获取从上一个活动传递的 id。并从带有 id 的数据中获取数据。您将获得所有数据。并将获取的数据显示给活动。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java