I make application and I have a problem with database again.
I made two buttons – previous button and next button.
Next button is work, but after click previous button program doesn’t diplay record in textview. Why?
First I click nest button all work then I click previous button and “cursor” go to previous record but doesn’t display record in textview.
I solved my problem.
I deleted methods: getNextdata() and getPrevData.
Now my code is:
Galeria.java:
public class Galeria extends Activity {
public static long record = 1;
PrzyslowiaArabskie info = new PrzyslowiaArabskie(this);
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.galeria);
TextView tv = (TextView) findViewById(R.id.editText1);
info.open();
int irec = info.policz_rec();
Toast.makeText(getApplicationContext(), " ilość przysłów w bazie " + irec, Toast.LENGTH_SHORT).show();
String data = info.getData(record);
info.close();
tv.setText(data);
Button bNastepny = (Button) findViewById(R.id.next);
bNastepny.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
TextView tv = (TextView) findViewById(R.id.editText1);
info.open();
record++;
String data = info.getData(record);
info.close();
tv.setText(data);
}
});
Button bPoprzedni = (Button) findViewById(R.id.prev);
bPoprzedni.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
TextView tv = (TextView) findViewById(R.id.editText1);
info.open();
record--;
String data = info.getData(record);
info.close();
tv.setText(data);
}
});
}
}
My method in DB Helper class:
public String getData(long record) {
// TODO Auto-generated method stub
record = Galeria.record;
String[] columns = new String[] { KEY_ID, KEY_PRZYSLOWIE};
Cursor c = myDatabase.query(TABLE_NAME, columns, KEY_ID + "=" + record, null, null, null, null);
if (c != null) {
c.moveToFirst();
String data = c.getString(1);
return data;
}
return null;
}
And my all buttons (next and prefious) work. Thank you for help.
without running your code, are you sure that you didn’t forget to put:
info.open();and
info.close();in your previous button’s onclick listener?
such as:
==================================
if that doesn’t solve the problem, does your logcat indicate any errors? if not, are you absolutely sure that you are retrieving a string from your
getPrevData(record)method? you can check this by doing something like:and if
datais not showing in LogCat, then you know you’re not getting it from the database correctly.if
datais showing up in LogCat, then you narrowed the problem down, and we can help you with that part easily 🙂