Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/181.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 从SQLite检索数据并在另一个类中按行显示_Java_Android_Sqlite - Fatal编程技术网

Java 从SQLite检索数据并在另一个类中按行显示

Java 从SQLite检索数据并在另一个类中按行显示,java,android,sqlite,Java,Android,Sqlite,为什么我不能从SQLite检索数据?我错过什么了吗?我已经阅读了许多文档,并试图解决它,但仍然失败 worketailstable.java Button btn1=(Button)findViewById(R.id.button2); btn1.setOnClickListener(new View.OnClickListener() { public void onClick(View arg0) { AlertDialog.Builder

为什么我不能从SQLite检索数据?我错过什么了吗?我已经阅读了许多文档,并试图解决它,但仍然失败

worketailstable.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);
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();

        }
DisplayDate.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);
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上。如何实现这一点?

单击“是”后,您将转到下一个活动并向其传递名称:

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();

}

单击“是”后,您将移动到下一个活动并向其传递名称:

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();


}

您的要求是什么?我的意思是您只想在下一个活动中显示该特定名称的详细信息???从数据库中获取?没错。我希望它显示为一行,而不是在EditText上。您是否尝试使用其他工具运行查询?例如,命令行sqlite3命令?这就是我在调试java部件之前验证数据库部件是否工作的方法。@KağanKayal我不太确定是否使用sqlite3命令,但我确信数据可以插入SQLite。您的要求是什么?我的意思是,您只想在下一个活动中显示该特定名称的详细信息???从数据库中获取?没错。我希望它显示为一行,而不是EditText。您是否尝试使用其他工具运行查询?例如,命令行sqlite3命令?这就是我在调试java部件之前验证数据库部件是否工作的方法。@KağanKayal我不确定是否使用sqlite3命令,但我确定数据可以插入SQliteI使用Toast.makeText(getApplicationContext(),name1,Toast.LENGTH_SHORT).show();我可以得到数值,检查你是否得到天气的数值!!我需要使用set和get方法吗?你得到weather的值了吗?是的,但我希望它以某种方式显示,在特定列下…我使用Toast.makeText(getApplicationContext(),name1,Toast.LENGTH_SHORT)。show();我可以得到数值,检查你是否得到天气的数值!!我需要使用set和get方法吗?你得到天气的值了吗?是的,但我希望它以某种方式显示,在特定列下。。。