如何只从数据库中显示一次特定数据?

时间:2012-06-21 09:23:26

标签: java android sql database

如何让变量text1text2time在开头只显示一次,例如 title ,因为它们具有相同的特定值数据组,然后在下面显示变量text3,因为该变量具有该特定数据组的值。

text3完成写入数据时,对于该组数据,查询必须开始为text1text2time以及新值编写另一组数据下面的text3

这是我的代码:

 public class NapredakActivity extends Activity {  
 @Override
 public void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.first);
 String var;
 final TextView t = (TextView) findViewById(R.id.textView2);
 Intent intent = getIntent();
 var=intent.getStringExtra("date");
       SQLiteDatabase db = openOrCreateDatabase("Database", MODE_PRIVATE, null);
       String q = "SELECT text1 ,text2, text3, time FROM table WHERE time LIKE '"+var+"'";

       Cursor c = db.rawQuery(q, null);

       if( c.getCount() < 1 ){

           c.close();
           db.close();
           Toast.makeText(NapredakActivity.this, "wrong choice",Toast.LENGTH_LONG).show();

           startActivity(new Intent(this,calendar.class));
       }
       else{
                 String j="";
                                           c.moveToFirst();
                         do {
                          String cm = c.getString(c.getColumnIndex("text1" )); 
                          String cm2 = c.getString(c.getColumnIndex("text2"));            
                          String cm4 = c.getString(c.getColumnIndex("text3"));
                          String cm3 = c.getString(c.getColumnIndex("time" ));

                        j = j+"\n"+cm+cm4+cm2+cm3;  

                          } while (c.moveToNext());

                         t.setText(j);   
                   c.close();
              db.close();        
       }        }    }

1 个答案:

答案 0 :(得分:0)

如果我理解你正确假设你喜欢保持显示db内容的方式,即在一个TextView中,只需使用这样的标志:

else{
                         String cmOld ="a text that never will be in the BD";
                         boolean flag_newText1Found;
                         String j="";
                         c.moveToFirst();
                         do {
                          String cm = c.getString(c.getColumnIndex("text1" )); 
                          if (cm.equals(cmOld)) 
                             cm = "";
                          else
                             cmOld = cm;
                          String cm2 = c.getString(c.getColumnIndex("text2"));            
                          String cm4 = c.getString(c.getColumnIndex("text3"));
                          String cm3 = c.getString(c.getColumnIndex("time" ));

                        j = j+"\n"+cm+cm4+cm2+cm3;  

                          } while (c.moveToNext());


                         t.setText(j);