0

I'm trying to get single data cell from my DB into text view

this is the function i wrote it in my sqliteopenhelper class

public Cursor getMainData(){
SQLiteDatabase db = this.getWritableDatabase();
Cursor result = db.rawQuery("select NAMEPPL from "+Table_name2,null);
return result;}

and this what i did it in the main activity class ,

its crashing once i lunch the app and giving me something related to buffer while I'm not using any buffers

is this the write way?

Thank you in advance.

public class MainActivity extends AppCompatActivity {

DB db;
Button addmed,addpl;
TextView PPLNAMERES;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    PPLNAMERES = (TextView)findViewById(R.id.PPLAGETXTVIEW);

    db = new DB(this);
    listView();
}

public void addmedView(View view){
    Intent ADDMEDVIEW = new Intent(this,ADDMEDCINEVIEW.class);
    startActivity(ADDMEDVIEW);

}
public void addpplview(View view){
    Intent ADDPPLVIEWS = new Intent(this,ADDPPLVIEW.class);
    startActivity(ADDPPLVIEWS);
}
 public void listView(){
Cursor res= db.getMainData();
if(res.getCount()==0){
    Toast.makeText(MainActivity.this,"No Data",Toast.LENGTH_LONG).show();
    return;
}
else{
    PPLNAMERES.setText(res.getString(0));

}}
Colonel Thirty Two
  • 23,953
  • 8
  • 45
  • 85
OWA
  • 45
  • 6

1 Answers1

0

Use res.moveToFirst() before getting date from cursor.

if(res.moveToFirst()){
     PPLNAMERES.setText(res.getString(0));
} else{
     Toast.makeText(MainActivity.this,"No Data",Toast.LENGTH_LONG).show();
     return;
}

moveToFirst() method will return false if the cursor is empty.

Dhaval Patel
  • 10,119
  • 5
  • 43
  • 46
  • still crashing and giving me this message in logcat java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference – OWA Oct 31 '15 at 21:21
  • @OWA Then your `TextView` is null. Test it with `if (PPLNAMERES == null) Toast.makeText (etc...)` - http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it – PPartisan Oct 31 '15 at 21:26
  • still crashing dhaval public void listView(){ Cursor res= db.getMainData(); if(res.moveToFirst()){ PPLNAMERES.setText(res.getString(0)); } else{ Toast.makeText(MainActivity.this,"No Data",Toast.LENGTH_LONG).show(); return; } – OWA Oct 31 '15 at 21:30
  • @OWA Does "activity_main" layout has TextView with id "PPLAGETXTVIEW"? – Dhaval Patel Oct 31 '15 at 21:33
  • Dhaval i test it its showing null , what it the problem . is it something related cursor ? – OWA Oct 31 '15 at 21:34
  • @OWA It's related to PPLNAMERES TextView. It's null so NullPointerException. – Dhaval Patel Oct 31 '15 at 21:35
  • HOLLLY S*** i made a mistake thank you soon much it been solve the ID of the text view was the problem :S – OWA Oct 31 '15 at 21:42