TestITBabe
TestITBabe

Reputation: 23

How to get Data by Id Room DataBase

I'm trying Hard but can't get any result I just want to get my String offlineData by id from the database i'm getting the id number by getId and sending it into mainActivty but there I can't benefit from it please check my codes

Dao
public interface FavNewsDao {



@Query("SELECT * FROM news_table")
LiveData<List<FavNews>> getAllNotes();

@Query("SELECT * FROM news_table WHERE id=:id")
int loadSingle(int id);
}

I'm setting the data in recyclerView adapters here is my MainActivity

public class Main2Activity extends AppCompatActivity {

private WebView web_View;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main2);
    Intent getUrl = getIntent();

    String url2 = getUrl.getStringExtra("url");
    web_View = findViewById(R.id.webView);

    web_View.setWebViewClient(new WebViewClient());

    if (CheckInternet.isNetwork(getApplicationContext())) {
        if(url2!=null) {
            Log.e("url2", url2);
            web_View.loadUrl(url2);
        }
    } else {

        int data = getUrl.getIntExtra("data",0);


       int a = FavNewsDatabase.getInstance(getApplicationContext()).noteDao().loadSingle(1);
            Log.e("data2",a+"");
           String dds = Integer.toString(a);
       String encodedHtml = Base64.encodeToString(dds.getBytes(), Base64.NO_PADDING);

    web_View.loadData(encodedHtml, "text/html", "base64");

    }



}
}

what i'm doing in the adapter ` intent.putExtra("data", favNews.get(position).getId()); and here is my problem

       int a = FavNewsDatabase.getInstance(getApplicationContext()).noteDao().loadSingle(1);

so how is it possible to get the offline data by id

@Entity(tableName = "news_table")
 public class FavNews {

@PrimaryKey(autoGenerate = true)
private int id;

private String title;

private String description;

private String url;

private String offlineData;

of course i have setters and getters here

Upvotes: 1

Views: 7736

Answers (1)

Md. Asaduzzaman
Md. Asaduzzaman

Reputation: 15423

Change your query like below to get only offlineData by id

@Query("SELECT offlineData FROM news_table WHERE id =:id")
String loadSingle(int id);

And then fetch it like below:

String a = FavNewsDatabase.getInstance(
           getApplicationContext()).noteDao().loadSingle(1);

N.B: You should use data instead of hardcoded 1 as you get data through intent pass from RecyclerView item click.

Upvotes: 2

Related Questions