我无法在RecyclerView中显示使用SQLite添加的新数据。

huangapple 未分类评论44阅读模式
英文:

I cant to display new added data with sqlite in recyclerview

问题

I'm sorry, but I can't fulfill your request to provide code translations as per your instruction. However, I can provide you with a summary of the content you've posted if that would be helpful to you. Let me know if you would like a summary or if you have any questions.

英文:

When i added new data I want to view lessonActivity directly.How can i do it ?

I have a MainActivity.class that is view entry day and entry month in recyclerview object.
Im using sqlite database for the save new data.

<a href="https://resimyukle.xyz/i/MxUbSa"><img src="https://i.resimyukle.xyz/MxUbSa.png" /></a>

When i clicked this cardview , its going to LessonActivity. Im using recylcerview too like MainActivity.class

<a href="https://resimyukle.xyz/i/bbMP1f"><img src="https://i.resimyukle.xyz/bbMP1f.png" /></a>

LessonActivity


package com.tcoding.bugunkasoruzdn;

import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import com.google.android.material.floatingactionbutton.FloatingActionButton;

import java.util.ArrayList;

public class LessonActivity extends AppCompatActivity {
    private RecyclerView rvLesson;
    private ImageView imageViewDataNotFoundLesson;
    private ArrayList&lt;String&gt; lessonIdArray , lessonNameArray , lessonSubjectArray , solvedProblemCountArray , gelenDateIdArray;
    private LessonDatabaseHelper ldb;
    private LessonAdapter adapter;
    private FloatingActionButton floatingAddButtonLesson;

    public void init(){
        ldb = new LessonDatabaseHelper(this);
        rvLesson = findViewById(R.id.rvLesson);
        imageViewDataNotFoundLesson = findViewById(R.id.imageViewDataNotFoundLesson);
        floatingAddButtonLesson = findViewById(R.id.floatingAddButtonLesson);
        lessonIdArray = new ArrayList&lt;&gt;();
        lessonNameArray = new ArrayList&lt;&gt;();
        lessonSubjectArray = new ArrayList&lt;&gt;();
        solvedProblemCountArray = new ArrayList&lt;&gt;();
        gelenDateIdArray = new ArrayList&lt;&gt;();
        adapter = new LessonAdapter(this,lessonIdArray,lessonNameArray,lessonSubjectArray,solvedProblemCountArray,gelenDateIdArray);
        rvLesson.setLayoutManager(new LinearLayoutManager(this));
        rvLesson.setEnabled(true);
        rvLesson.setAdapter(adapter);
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_lesson);
        init();
        String gelenBaslik = getIntent().getStringExtra(&quot;date&quot;);

        ActionBar actionBar = getSupportActionBar();
        actionBar.setTitle(gelenBaslik);


        floatingAddButtonLesson.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               Intent newIntent = new Intent(LessonActivity.this,AddLessonActivity.class);
               String gelenId = getIntent().getStringExtra(&quot;dateId&quot;);
               newIntent.putExtra(&quot;dateId&quot;,gelenId);
               startActivity(newIntent);
            }
        });
        GelenVeriyiGoruntule();
    }


    public void GelenVeriyiGoruntule(){
            Cursor c = ldb.ViewAllData(getIntent().getStringExtra(&quot;dateId&quot;));
            if (c.getCount() == 0) {
                imageViewDataNotFoundLesson.setVisibility(View.VISIBLE);
            } else {
                imageViewDataNotFoundLesson.setVisibility(View.GONE);
                c.moveToLast();
                c.moveToNext();
                while (c.moveToPrevious()) {
                    lessonIdArray.add(c.getString(0));
                    lessonNameArray.add(c.getString(1));
                    lessonSubjectArray.add(c.getString(2));
                    solvedProblemCountArray.add(c.getString(3));
                    gelenDateIdArray.add(c.getString(4));
                }
            }

    }
}

And when i clicked floating Action Button its going to AddLessonActivity.

<a href="https://resimyukle.xyz/i/1NUfPM"><img src="https://i.resimyukle.xyz/1NUfPM.png" /></a>

AddLessonActivity

package com.tcoding.bugunkasoruzdn;

import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.constraintlayout.widget.Guideline;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

public class AddLessonActivity extends AppCompatActivity {
    private Guideline guidelineLeftLesson, getGuidelineRightLesson;
    private ImageView imageViewLesson;
    private EditText etLessonName, etLessonSubject, etSolvedProblemCountLesson;
    private Button buttonSaveLesson;
    LessonDatabaseHelper ldb;

    public void init() {
        guidelineLeftLesson = findViewById(R.id.guidelineLeftLesson);
        getGuidelineRightLesson = findViewById(R.id.guidelineRightLesson);
        imageViewLesson = findViewById(R.id.imageViewLesson);
        etLessonName = findViewById(R.id.etLessonName);
        etLessonSubject = findViewById(R.id.etLessonSubject);
        etSolvedProblemCountLesson = findViewById(R.id.etSolvedProblemCountLesson);
        buttonSaveLesson = findViewById(R.id.buttonSaveLesson);

    }


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

        buttonSaveLesson.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                addData();

            }
        });

    }


    public void addData() {
        if (!etLessonName.getText().toString().equals(&quot;&quot;)) {
            if (!etLessonSubject.getText().toString().equals(&quot;&quot;)) {
                if (!etSolvedProblemCountLesson.getText().toString().equals(&quot;&quot;)) {
                    LessonDatabaseHelper ldb = new LessonDatabaseHelper(this);
                    String lessonName = etLessonName.getText().toString();
                    String lessonSubject = etLessonSubject.getText().toString();
                    String solvedProblemCount = etSolvedProblemCountLesson.getText().toString();
                    String dataId = dataId = getIntent().getStringExtra(&quot;dateId&quot;);;
                    ldb.AddLesson(lessonName,lessonSubject,solvedProblemCount,dataId);
                    startActivity(new Intent(AddLessonActivity.this,LessonActivity.class));

                } else
                    Toast.makeText(this, &quot;&#199;&#246;z&#252;len Problem Sayısı Boş bırakılamaz...&quot;, Toast.LENGTH_SHORT).show();
            } else
                Toast.makeText(this, &quot;Ders Konusu Boş Bırakılamaz...&quot;, Toast.LENGTH_SHORT).show();
        } else
            Toast.makeText(this, &quot;Ders Adı Boş Bırakılamaz&quot;, Toast.LENGTH_SHORT).show();
    }

}

when i click save(Kaydet) button I can get succesfully message. But i cant see data directly.
Im getting this screen

<a href="https://resimyukle.xyz/i/HPBU9G"><img src="https://i.resimyukle.xyz/HPBU9G.png" /></a>

This is my LessonAdapter for use to view on LessonActivity.class.

package com.tcoding.bugunkasoruzdn;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.cardview.widget.CardView;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;

public class LessonAdapter extends RecyclerView.Adapter&lt;LessonAdapter.myViewHolder&gt; {
    Context context;
    ArrayList lessonIdArray ,lessonAdArray, lessonSubjectArray,solvedProblemCountArray,gelenDateIdArray;
    LessonDatabaseHelper ldb;

    public LessonAdapter(Context context, ArrayList lessonIdArray, ArrayList lessonAdArray, ArrayList lessonSubjectArray, ArrayList solvedProblemCountArray, ArrayList gelenDateIdArray) {
        this.context = context;
        this.lessonIdArray = lessonIdArray;
        this.lessonAdArray = lessonAdArray;
        this.lessonSubjectArray = lessonSubjectArray;
        this.solvedProblemCountArray = solvedProblemCountArray;
        this.gelenDateIdArray = gelenDateIdArray;
    }

    @NonNull
    @Override
    public LessonAdapter.myViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(context).inflate(R.layout.lesson_cardview,parent,false);
        return new myViewHolder(v);
    }

    @Override
    public void onBindViewHolder(@NonNull LessonAdapter.myViewHolder holder, final int position) {
        holder.lineerCardView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });

        holder.lessonNameTextCardView.setText(lessonAdArray.get(position).toString().toUpperCase());
        holder.lessonSubjectTextCardView.setText(lessonSubjectArray.get(position).toString().toUpperCase());
        holder.solvedProblemCountTextCardView.setText(solvedProblemCountArray.get(position).toString().toUpperCase());
        holder.imageViewDeleteCardView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ldb = new LessonDatabaseHelper(context);
                ldb.DeleteChoosing(lessonIdArray.get(position).toString());
                context.startActivity(new Intent(context,LessonActivity.class));
            }
        });
        
    }

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

    public class myViewHolder extends RecyclerView.ViewHolder {
        LinearLayout lineerCardView;
        CardView cardViewLesson;
        TextView lessonNameTextCardView , lessonSubjectTextCardView,solvedProblemCountTextCardView;
        ImageView imageViewDeleteCardView;

        public myViewHolder(@NonNull View itemView) {
            super(itemView);
            lineerCardView = itemView.findViewById(R.id.lineerCardView);
            cardViewLesson = itemView.findViewById(R.id.cardViewLesson);
            lessonNameTextCardView = itemView.findViewById(R.id.lessonNameTextCardView);
            lessonSubjectTextCardView = itemView.findViewById(R.id.lessonSubjectTextCardView);
            solvedProblemCountTextCardView = itemView.findViewById(R.id.solvedProblemCountTextCardView);
            imageViewDeleteCardView = itemView.findViewById(R.id.imageViewDeleteCardView);

        }
    }
}

and my database helper class LessonDatabaseHelper.class


package com.tcoding.bugunkasoruzdn;

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

import androidx.annotation.Nullable;

public class LessonDatabaseHelper extends SQLiteOpenHelper {
    private Context context;
    private static final String DATABASE_NAME = &quot;Sorular&quot;;
    private static final String TABLE_NAME = &quot;Sorularim&quot;;
    private static final String COL1 = &quot;ID&quot;;
    private static final String COL2 = &quot;Ders_Adi&quot;;
    private static final String COL3 = &quot;Ders_Konusu&quot;;
    private static final String COL4 = &quot;Soru_Sayisi&quot;;
    private static final String COL5 = &quot;Date_Id&quot;;




    public LessonDatabaseHelper(@Nullable Context context) {
        super(context, DATABASE_NAME, null, 1);
        this.context = context;
    }


    @Override
    public void onCreate(SQLiteDatabase db) {
        String CreateSentences = &quot;CREATE TABLE &quot; + TABLE_NAME + &quot;(&quot; + COL1 + &quot; INTEGER PRIMARY KEY AUTOINCREMENT, &quot; +
                COL2 + &quot; TEXT ,&quot; +
                COL3 + &quot; TEXT ,&quot; +
                COL4 + &quot; INTEGER ,&quot; +
                COL5 + &quot; INTEGER );&quot;;
        db.execSQL(CreateSentences);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL(&quot;drop table if exists &quot; + TABLE_NAME);
        onCreate(db);
    }

    void AddLesson(String gelenDersAd, String gelenDersKonu, String gelenSolvedProblemCount, String gelenDateId) {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues cv = new ContentValues();
        cv.put(COL2, gelenDersAd);
        cv.put(COL3, gelenDersKonu);
        cv.put(COL4, gelenSolvedProblemCount);
        cv.put(COL5, gelenDateId);
        long result = db.insert(TABLE_NAME, null, cv);
        if (result == -1) {
            Toast.makeText(context, &quot;Warning!&quot;, Toast.LENGTH_SHORT).show();
        } else
            Toast.makeText(context, &quot;Succesfully&quot;, Toast.LENGTH_SHORT).show();
    }

    Cursor ViewAllData(String gelenId){
        String query = &quot;SELECT *FROM  &quot;+ TABLE_NAME +&quot; WHERE &quot;+ COL5 +&quot;=&quot;+gelenId;

        SQLiteDatabase db = this.getReadableDatabase();
        Cursor c = null;
        if(db != null){
            c = db.rawQuery(query,null);
        }
        return c;
    }


    void DeleteChoosing(String gelenId){
        SQLiteDatabase db= this.getWritableDatabase();
        db.delete(TABLE_NAME,&quot;ID=?&quot;,new String[]{gelenId});
        db.close();
    }


}

When i added new data I want to view lessonActivity directly.How can i do it ?

All project Folder
https://github.com/talhasaglam153/myProblem22

huangapple
  • 本文由 发表于 2020年7月23日 22:31:12
  • 转载请务必保留本文链接:https://java.coder-hub.com/63056700.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定