从SQLite获取数据到新类

时间:2015-10-09 04:27:11

标签: java android sqlite fetch

我仍然是Android开发的新手,现在面临使用SQLiteclass向另一个intent提取数据的问题。我已阅读了大量文档,但仍未达到预期的效果。 DisplayData.java上没有数据显示。我错过了什么吗???下面是我的编码片段。

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);
                    startActivity(intent);
 }

                });
                builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int ii) {
                        dialog.dismiss();
                    }
                });
                builder.show();


            }
        });
    }

DisplayData.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");
        if(name1=="Lim X Y")
        {
            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)
            {
                cursor.moveToFirst();
                cursor.getString(cursor.getColumnIndex("weather"));
                cursor.getString(cursor.getColumnIndex("date"));
                cursor.getString(cursor.getColumnIndex("status"));

            }

        }

    }
}

InfoAPI.java

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import com.example.project.project.database.MyDatabaseHelper;
import android.content.Context;
import android.database.SQLException;
import android.content.ContentValues;

public class InfoAPI {
    private SQLiteDatabase database;
    private MyDatabaseHelper dbHelper;
    public String[] allColumns={MyDatabaseHelper.ID,MyDatabaseHelper.Name,MyDatabaseHelper.Weather,MyDatabaseHelper.Date,MyDatabaseHelper.Status};

    public InfoAPI(Context context)
    {
        dbHelper=new MyDatabaseHelper(context);
    }

    public void open() throws SQLException {
        database = dbHelper.getWritableDatabase();

    }

    public void close() {
        dbHelper.close();
    }
    public long insertTimeSheet(String name,String weather,String date,String status){
        database=dbHelper.getWritableDatabase();
        ContentValues values=new ContentValues();
        values.put(MyDatabaseHelper.Name,name);
        values.put(MyDatabaseHelper.Weather,weather);
        values.put(MyDatabaseHelper.Date,date);
        values.put(MyDatabaseHelper.Status, status);
        database.insert(MyDatabaseHelper.TABLE_INFO, null, values);
        Cursor cursor = database.rawQuery("SELECT MAX(ID) FROM "+ MyDatabaseHelper.TABLE_INFO, null);
        if(cursor.getCount()>0){
            cursor.moveToFirst();
            return cursor.getLong(0);
        }

        return 0;
    }
}

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>
    </TableLayout>
    </HorizontalScrollView>
    </LinearLayout>
    </ScrollView>

2 个答案:

答案 0 :(得分:1)

在调用ts.insertTimeSheet(name, weather, date, status)并且变量name, weather, datestatus包含某些值时,您是否调试并检查数据是否实际存储在数据库中?

您还可以使用name1.equalsname1.equalsIgnoreCase进行String比较。

答案 1 :(得分:0)

将字符串if(name1 ==&#34; Lim X Y&#34;)的比较更改为if(name1.equalsIgnoreCase(&#34; Lim X Y&#34;))