2015-10-09 39 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

相关问题