0

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.

1 Answer 1

0

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:

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.getPrevData(record);
            info.close();
            tv.setText(data);

        }
    });

==================================

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:

String data = info.getPrevData(record);
Log.d("bPoprzedni", data);

and if data is not showing in LogCat, then you know you're not getting it from the database correctly.

if data is showing up in LogCat, then you narrowed the problem down, and we can help you with that part easily :)

9
  • I added message from Logcat.
    – Tomeksss
    Nov 13, 2012 at 20:13
  • Above message from Logcat id If I run app in Android SDK 8. If I run app on SDK API Level 16 LogCat doesn't show error but previous button doesn't work.
    – Tomeksss
    Nov 13, 2012 at 20:54
  • @Tomeksss Sorry, i didn't understand. The fix (info.open() and info.close()) still does not work for SDK API level 16? It still crash? even after you put in info.close()?
    – David T.
    Nov 13, 2012 at 23:31
  • No, the fix work, My button "bNastępny" (next) work. I don't know why after click previous button program doesn't display previous record from database.
    – Tomeksss
    Nov 14, 2012 at 10:45
  • @Tomeksss Proszę przeczytać ten stackoverflow.com/questions/4940308/…
    – David T.
    Nov 14, 2012 at 16:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.