Android SQlite数据检索错误

时间:2013-12-05 07:41:01

标签: java android sqlite

我正在开发一个Android应用..其中我使用SQLite数据库存储值,我有两个Button - &gt; 保存用于保存数据和< strong>已保存数据以显示存储数据。

当用户点击任何已保存数据行时,必须显示特定数据但我的问题是当用户按任意一行时,只显示最后一行而不是按下的行。

请检查并帮助我。

我的Activity是:

public void saveddata(View V)
{
    Cursor c = db.rawQuery("SELECT * from Data", null);
    int count = c.getCount();
    c.moveToFirst();
    TableLayout tableLayout = new TableLayout(getApplicationContext());
    tableLayout.setVerticalScrollBarEnabled(true);
    TableRow tableRow;
    TextView textView1, textView2,textView3,textView4,textView5;
    tableRow = new TableRow(getApplicationContext());         
    textView1 = new TextView(getApplicationContext());
    textView1.setText("FirstName");
    textView1.setTextColor(Color.RED);
    textView1.setTypeface(null, Typeface.BOLD);
    textView1.setPadding(10, 10, 10, 10);
    tableRow.addView(textView1);

    textView2 = new TextView(getApplicationContext());
    textView2.setText("LastName");
    textView2.setTextColor(Color.RED);
    textView2.setTypeface(null, Typeface.BOLD);
    textView2.setPadding(10, 10, 10, 10);
    tableRow.addView(textView2);

    tableLayout.addView(tableRow);

    for(Integer j=0; j<count; j++)
    {
        tableRow = new TableRow(getApplicationContext());
        textView3= new TextView(getApplicationContext());
        textView3.setText(c.getString(c.getColumnIndex("fname")));
        textView3.setClickable(true);

        ett1=textView3.getText().toString();
        //datas11=textView3.getText().toString();

        textView4 = new TextView(getApplicationContext());
        textView4.setText(c.getString(c.getColumnIndex("mname")));
        textView4.setClickable(true);
        ett2=textView4.getText().toString();
        //datas12=textView4.getText().toString();

        textView5 = new TextView(getApplicationContext());
        textView5.setText(c.getString(c.getColumnIndex("lname")));
        textView5.setClickable(true);
        ett3=textView5.getText().toString();
        //datas13=textView7.getText().toString();

        textView3.setPadding(10, 10, 10,10);
        textView5.setPadding(10, 10, 10, 10);
        tableRow.addView(textView3);
        tableRow.addView(textView5);
        tableLayout.addView(tableRow);
        c.moveToNext();

        textView3.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                Intent intent = new Intent(MainActivity.this ,SavedDate.class);
                intent.putExtra("ett1",ett1 + "");
                intent.putExtra("ett2",ett2 + "");
                intent.putExtra("ett3",ett3 + "");

                startActivity(intent);
            }
        });
    }

    setContentView(tableLayout);

    db.close();
}

2 个答案:

答案 0 :(得分:2)

嘿,这是您的新更新代码,这将对您有所帮助。

 Cursor c = db.rawQuery("SELECT * from Data", null);
int count = c.getCount();
c.moveToFirst();
 TableLayout table=new TableLayout;
                  table.setGravity(Gravity.CENTER);

      table.setStretchAllColumns(true);
      table.setShrinkAllColumns(true);

                TableRow rowLabels = new TableRow(this);
                rowLabels.setLayoutParams(new LayoutParams(
                        LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

                rowLabels.setBackgroundDrawable(getResources().getDrawable(
                        R.xml.tablerow));

                TextView srnoLabel = new TextView(this);
                srnoLabel.setText("FirstName");
                srnoLabel.setTypeface(Typeface.SERIF, Typeface.BOLD);
                srnoLabel.setTextColor(Color.RED);
                srnoLabel.setPadding(40, 5, 0, 5);
                srnoLabel.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 17);
                rowLabels.addView(srnoLabel);

                TextView pLabel = new TextView(this);
                pLabel.setText("Middle Name");
                pLabel.setTypeface(Typeface.SERIF, Typeface.BOLD);
                pLabel.setTextColor(Color.RED);
                pLabel.setPadding(20, 5, 0, 5);
                pLabel.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 17);
                rowLabels.addView(pLabel);

                TextView valLabel = new TextView(this);
                valLabel.setText("Last Name");
                valLabel.setTypeface(Typeface.SERIF, Typeface.BOLD);
                valLabel.setTextColor(Color.RED);
                valLabel.setPadding(40, 5, 0, 5);
                valLabel.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 17);
                rowLabels.addView(valLabel);
                rowLabels.setPadding(0, 0, 10, 0);


if (c.moveToFirst()) {
                    do {
                    String f_name = c.getString(c
                                .getColumnIndex("fname"));
                    String l_name = c.getString(c
                                .getColumnIndex("lname"));
                    String m_name = c.getString(c
                                .getColumnIndex("mname"));      

                    TableRow rowdata = new TableRow(this);
                        rowdata.setLayoutParams(new LayoutParams(
                                LayoutParams.WRAP_CONTENT,
                                LayoutParams.WRAP_CONTENT));
                        // 1 column
                        TextView f_txt = new TextView(this);

                        f_txt.setText(Integer.toString(count));
                        f_txt.setPadding(50, 3, 10, 3);
                        f_txt.setText(f_name);
                        f_txt.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 17);
                        f_txt.setTextColor(Color.BLACK);
                        f_txt.setClickable(true);
                        rowdata.addView(f_txt);


                    // 2 column
                        final TextView m_txt = new TextView(this);
                        m_txt.setText(m_name);
                        m_txt.setPadding(15, 3, 10, 3);
                        m_txt
                                .setTextSize(TypedValue.COMPLEX_UNIT_DIP, 17);
                        m_txt.setTextColor(Color.BLACK);
                        rowdata.addView(m_txt);

                            // 3 column
                        final TextView l_txt = new TextView(this);
                        l_txt.setText(l_name);
                                //Annual_crop_loss.toString());
                        l_txt.setPadding(45, 3, 10, 3);
                        l_txt.setTextSize(
                                TypedValue.COMPLEX_UNIT_DIP, 17);
                        l_txt.setTextColor(Color.BLACK);
                        rowdata.addView(m_txt);
                        rowdata.setPadding(1, 1, 1, 1);

                    table.addView(rowLabels);
                        table.addView(rowdata);
                    }

                    } while (c.moveToNext());

                    table.setPadding(10, 10, 10, 10);
                    setContentView(table);
                }   
finally {
        if (dbase != null)
            // newDB.execSQL("DELETE FROM " + tableName);
            c.close();
            dbase.close();
    }

答案 1 :(得分:0)

原因是textview的click事件被动态创建的textview覆盖。因此,在for循环结束时,最终点击事件将指向最后一条记录。您可以使用自定义列表视图而不是此动态生成的表格行,并在单击时显示相应的数据。如果您想要一个示例listview和Sqlite数据库,请告诉我。