0

我制作了应用程序,但我又遇到了数据库问题。我做了两个按钮——上一个按钮和下一个按钮。下一个按钮可以工作,但是单击上一个按钮后,程序不会在 textview 中显示记录。为什么?首先我单击嵌套按钮所有工作然后我单击上一个按钮和“光标”转到上一个记录但不在文本视图中显示记录。

我解决了我的问题。我删除了方法:getNextdata() 和 getPrevData。

现在我的代码是:

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);

                }
        });
    }
}

我在 DB Helper 类中的方法:

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;


}

我的所有按钮(下一个和前一个)都可以工作。谢谢你的帮助。

4

1 回答 1

0

如果不运行您的代码,您确定您没有忘记输入:

info.open();

info.close();

在上一个按钮的 onclick 监听器中?

如:

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);

        }
    });

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

如果这不能解决问题,您的 logcat 是否指示任何错误?如果不是,您是否绝对确定您正在从您的getPrevData(record)方法中检索字符串?您可以通过执行以下操作来检查:

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

如果dataLogCat 中没有显示,那么您就知道您没有正确地从数据库中获取它。

如果data出现在 LogCat 中,那么您缩小了问题范围,我们可以轻松地帮助您解决这部分问题 :)

于 2012-11-13T19:01:40.470 回答