2015-10-09 45 views
1

爲什麼我無法從SQLite檢索數據?我錯過了什麼嗎?我已經閱讀了許多文檔並試圖解決它,但仍然失敗。從SQLite中檢索數據並在另一個類中按行顯示

WorkDetailsTable.java

Button btn1=(Button)findViewById(R.id.button2); 
     btn1.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View arg0) { 
     AlertDialog.Builder builder=new AlertDialog.Builder(WorkDetailsTable.this); 
     builder.setTitle("Data Saved"); 
     builder.setMessage("Are you sure you want to save?"); 
     builder.setIcon(android.R.drawable.ic_dialog_alert); 
     builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, int ii) { 
      long ab = ts.insertTimeSheet(name, weather, date, status); 
      Toast.makeText(context, "Data Saved", Toast.LENGTH_SHORT).show(); 
      Intent intent=new Intent(WorkDetailsTable.this,DisplayData.class); 
      intent.putExtra("name",name); //name will be used in another class 
      startActivity(intent); 

DisplayDate.java

public class DisplayData extends AppCompatActivity { 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.displaydata); 
     setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); 
     final String name1 = getIntent().getExtras().getString("name"); 
     Toast.makeText(getApplicationContext(),name1, Toast.LENGTH_SHORT).show(); 

     if(name1.equals("John")) 
     { 
      SQLiteDatabase db=(new MyDatabaseHelper(this)).getReadableDatabase(); 
      Cursor cursor=db.rawQuery("SELECT Weather,Date,Status FROM Information WHERE Name = ?",new String[]{""+name1}); 
      if(cursor.getCount()==1) 
      { 
       String weather=cursor.getString(cursor.getColumnIndex("Weather")); 
       String date=cursor.getString(cursor.getColumnIndex("Date")); 
       String status=cursor.getString(cursor.getColumnIndex("Status")); 


      } 
      db.close(); 

     } 

displaydata.xml

<?xml version="1.0" encoding="utf-8"?> 
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"> 

    <LinearLayout 
     android:orientation="vertical" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content"> 

     <HorizontalScrollView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginTop="15dp" > 

      <TableLayout 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:stretchColumns="|" 
       android:layout_marginBottom="25dp"> 


       <TableRow 
        android:layout_width="match_parent" 
        android:layout_height="match_parent" 
        android:id="@+id/tableRow1"> 

        <TextView 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:id="@+id/textView10" 
         android:background="@drawable/cell_shape" 
         android:textSize="17sp" 
         android:text="weather"/> 

        <TextView android:id="@+id/textView111" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:textSize="17sp" 
         android:background="@drawable/cell_shape" 
         android:text="date"/> 

     <TextView android:id="@+id/textView11" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:textSize="17sp" 
        android:background="@drawable/cell_shape" 
        android:text="status"/> 
       </TableRow> 
     // <TableRow> 

      //  <TextView android:id="@+id/textView1111" 
      //  android:layout_width="wrap_content" 
       //  android:layout_height="wrap_content" 
       // android:textSize="17sp" 
        // android:background="@drawable/cell_shape"/> 


    </TableRow> 
    </TableLayout> 
    </HorizontalScrollView> 
    </LinearLayout> 
    </ScrollView> 

我還噸在另一類由行,而不是EDITTEXT與此,但沒有運氣

TextView tvTemp=(TextView)findViewById(R.id.textView1111); 
     tvTemp.setText(weather + date + status); 

的數據應被檢索和顯示發慌。我該如何做到這一點?

+0

你有什麼要求? –

+0

有什麼要求? – John

+0

我的意思是你只是想在下一個活動中顯示那個特定名字的細節?從數據庫中獲取? –

回答

0

一旦你點擊了是,那麼你正在向未來的活動和通過名字了:

public class DisplayData extends AppCompatActivity { 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.displaydata); 
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); 

    Bundle bundle=getIntent().getExtras(); 
    String name=""; 
     if(bundle!=null) 
     { 
     name=bundle.getString("name"); 
      } 

     //check whether you get the name value here or not..(use log) 


     SQLiteDatabase db=(new MyDatabaseHelper(this)).getReadableDatabase(); 
     Cursor cursor=db.rawQuery("SELECT Weather,Date,Status FROM Information WHERE Name = ?",new String[]{""+name1}); 
     if(cursor.getCount()==1) 
     { 
      String weather=cursor.getString(cursor.getColumnIndex("Weather"));//give the proper index of weather and same for rest. 
      String date=cursor.getString(cursor.getColumnIndex("Date")); 
      String status=cursor.getString(cursor.getColumnIndex("Status")); 


     } 
     db.close(); 

}

+0

我使用Toast.makeText(getApplicationContext(),name1,Toast.LENGTH_SHORT).show();我可以得到值 – John

+0

檢查你是否得到天氣的價值! –

+0

我需要使用set和get方法嗎? – John

相關問題