为什么我的循环仅输出最后一个结果?

时间:2019-05-09 07:01:31

标签: java android sqlite textview android-sqlite

我正在构建一个Android Studio应用,其中可以将数据添加到SQLite数据库。当我想将数据输出到页面时,它仅显示数据库中的最后结果(它被覆盖)。如何防止这种情况发生并输出所有数据?

这是我的代码:

public void viewAll() {
    Cursor res = myDb.getAllData();
    if (res.getCount() == 0) {
        // show error
        System.out.println("No Data Found");
        return;
    }

    while (res.moveToNext()) {
        System.out.println("Id: " + res.getString(0) + "\n");
        System.out.println("Title: " + res.getString(1) + "\n");
        System.out.println("Description: " + res.getString(2) + "\n");
        System.out.println("Location: " + res.getString(4) + "\n\n");
        TextView textViewID = (TextView) findViewById(R.id.textViewID);
        textViewID.setText("Id: " + res.getString(0) + "\n");
        TextView textViewName = (TextView) findViewById(R.id.textViewName);
        textViewName.setText("Name: " + res.getString(1) + "\n");
        TextView textViewDescription = (TextView) findViewById(R.id.textViewDescription);
        textViewDescription.setText("Description: " + res.getString(2) + "\n");
        TextView textViewLocation = (TextView) findViewById(R.id.textViewLocation);
        textViewLocation.setText("Location: " + res.getString(4) + "\n");
    }

    // show all the data

}

预期结果是来自数据库的所有数据,但仅输出最后一个条目。

https://i.imgur.com/ByCfqUA.jpg

编辑

MyActivity.java

package com.example.triptracker;
import android.content.Intent;
import android.database.Cursor;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;

public class MomentsActivity extends AppCompatActivity {

private TextView mTextMessage;

private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
        = new BottomNavigationView.OnNavigationItemSelectedListener() {

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()) {
            case R.id.navigation_moments:
                Intent intent = new Intent(MomentsActivity.this, MomentsActivity.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
                startActivity(intent);
                break;

            case R.id.navigation_addmoment:
                Intent intent2 = new Intent(MomentsActivity.this, AddMomentActivity.class);
                intent2.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
                startActivity(intent2);
                break;
        }
        return false;
    }
};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    myDb = new DatabaseHelper(this);
    setContentView(R.layout.activity_moments);
    BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
    navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
    mList = this.findViewById(R.id.my_listview);
    manageListView();
}

DatabaseHelper myDb;
EditText editTitle, editDescription, editLocation;
Button btnAddData;
Button btnViewAll;
SimpleCursorAdapter mSCA; //Adapts/Handles the data for the listview
ListView mList;
Cursor mCsr;

int[] item_layout_ids_for_list = new int[]{
        R.id.textview_id,
        R.id.textview_name,
        R.id.textview_description,
        R.id.textview_location
};

String[] columns_to_list = new String[]{
        DatabaseHelper.COL_1,
        DatabaseHelper.COL_2,
        DatabaseHelper.COL_3,
        DatabaseHelper.COL_4
};

private void manageListView() {
    mCsr = myDb.getAllData();
    if (mSCA == null) {
        // Builds the Adapter for the List
        mSCA = new SimpleCursorAdapter(
                this,
                R.layout.mylistview_item, mCsr,
                columns_to_list,
                item_layout_ids_for_list,
                0
        );
        mList.setAdapter(mSCA); // Ties the Adapter to the ListView
    } else {
        mSCA.swapCursor(mCsr); // Refresh the List
    }
}

@Override
protected void onDestroy() {
    super.onDestroy();
    mCsr.close();
}

@Override
protected void onResume() {
    super.onResume();
    manageListView();
}

}

DatabaseHelper.java

package com.example.triptracker;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DatabaseHelper extends SQLiteOpenHelper {

public static final String DATABASE_NAME = "triptracker.db";
public static final String TABLE_NAME = "trip_table";
public static final String COL_1 = "trip_id";
public static final String COL_2 = "trip_title";
public static final String COL_3 = "trip_description";
public static final String COL_4 = "trip_image";
public static final String COL_5 = "trip_location";


public DatabaseHelper(Context context) {
    super(context, DATABASE_NAME, null, 1);
}

@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL("create table " + TABLE_NAME + " (" + COL_1 + " INTEGER PRIMARY KEY AUTOINCREMENT, " + COL_2 + " TEXT, " + COL_3 + " TEXT, " + COL_4 + " TEXT, " + COL_5 + " TEXT)");
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("drop table if exists " + TABLE_NAME);
    onCreate(db);
}

// TODO: Add image functionality.

public boolean insertData(String title, String description, String location) {
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues contentValues = new ContentValues();
    contentValues.put(COL_2, title);
    contentValues.put(COL_3, description);
    // contentValues.put(COL_4, image);
    contentValues.put(COL_5, location);
    long result = db.insert(TABLE_NAME, null, contentValues);
    if (result == -1)
        return false;
    else
        return true;

}

public Cursor getAllData() {
    SQLiteDatabase db = this.getWritableDatabase();
    Cursor res = db.rawQuery("select * from " + TABLE_NAME, null);
    return res;
}

}

正在输出以下错误:

2019-05-09 14:23:47.959 21253-21253/com.example.triptracker E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.triptracker, PID: 21253
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.triptracker/com.example.triptracker.MomentsActivity}: java.lang.IllegalArgumentException: column '_id' does not exist. Available columns: [trip_id, trip_title, trip_description, trip_image, trip_location]
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2913)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
    at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
    at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loop(Looper.java:193)
    at android.app.ActivityThread.main(ActivityThread.java:6669)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
 Caused by: java.lang.IllegalArgumentException: column '_id' does not exist. Available columns: [trip_id, trip_title, trip_description, trip_image, trip_location]
    at android.database.AbstractCursor.getColumnIndexOrThrow(AbstractCursor.java:340)
    at android.widget.CursorAdapter.init(CursorAdapter.java:180)
    at android.widget.CursorAdapter.<init>(CursorAdapter.java:157)
    at android.widget.ResourceCursorAdapter.<init>(ResourceCursorAdapter.java:96)
    at android.widget.SimpleCursorAdapter.<init>(SimpleCursorAdapter.java:104)
    at com.example.triptracker.MomentsActivity.manageListView(MomentsActivity.java:81)
    at com.example.triptracker.MomentsActivity.onCreate(MomentsActivity.java:52)
    at android.app.Activity.performCreate(Activity.java:7136)
    at android.app.Activity.performCreate(Activity.java:7127)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2893)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048) 
    at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78) 
    at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108) 
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808) 
    at android.os.Handler.dispatchMessage(Handler.java:106) 
    at android.os.Looper.loop(Looper.java:193) 
    at android.app.ActivityThread.main(ActivityThread.java:6669) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858) 
2019-05-09 14:23:47.983 21253-21253/com.example.triptracker I/Process: Sending signal. PID: 21253 SIG: 9

1 个答案:

答案 0 :(得分:1)

原因是,对于循环的每次迭代,您都将TextViews的值设置为该迭代的值,因此,您很可能只会看到最后一行。

您可以使用:-

public void viewAll() {
    Cursor res = myDb.getAllData();
    if (res.getCount() == 0) {
        // show error
        System.out.println("No Data Found");
        return;
    }

   TextView textViewID = (TextView) findViewById(R.id.textViewID);
   textViewID.setText("");
   TextView textViewName = (TextView) findViewById(R.id.textViewName);
   textViewName.setText("");
   TextView textViewDescription = (TextView) findViewById(R.id.textViewDescription);
   textViewDescription.setText("");
   TextView textViewLocation = (TextView) findViewById(R.id.textViewLocation);
   textViewLocation.setText("");


    while (res.moveToNext()) {
        System.out.println("Id: " + res.getString(0) + "\n");
        System.out.println("Title: " + res.getString(1) + "\n");
        System.out.println("Description: " + res.getString(2) + "\n");
        System.out.println("Location: " + res.getString(4) + "\n\n");
        textViewId.setText(textViewID.getText().toString()+res.getString(0) + "\n");
        textViewName.setText(textViewName.getText().toString()+"Name: " + res.getString(1) + "\n");
        textViewDescription.setText(textViewDescription.getText().toString() + "Description: " + res.getString(2) + "\n");
        textViewLocation.setText(textViewLocation.getText().toString() + "Location: " + res.getString(4) + "\n");
    }
}

甚至更好 -循环中的字符串连接效率低下 -这也只会在第一个字符之后附加一个换行符。

:-

public void viewAll() {
    Cursor res = myDb.getAllData();
    if (res.getCount() == 0) {
        // show error
        System.out.println("No Data Found");
        return;
    }
    StringBuilder idsb = new StringBuilder();
    StringBuilder namesb = new StringBuilder();
    StringBuilder descsb = new StringBuilder();
    StringBuilder locsb = new StringBuilder();
    String endofline = "";

   TextView textViewID = (TextView) findViewById(R.id.textViewID);
   TextView textViewName = (TextView) findViewById(R.id.textViewName);
   TextView textViewDescription = (TextView) findViewById(R.id.textViewDescription);
   TextView textViewLocation = (TextView) findViewById(R.id.textViewLocation);


    while (res.moveToNext()) {
        System.out.println("Id: " + res.getString(0) + "\n");
        System.out.println("Title: " + res.getString(1) + "\n");
        System.out.println("Description: " + res.getString(2) + "\n");
        System.out.println("Location: " + res.getString(4) + "\n\n");
        idsb.append("Id: ").append(res.getString(0)).append(endofline);
        namesb.append("Name: ").append(res.getString(1)).append(endofline);
        descsb.append("Description: ").append(res.getString(2)).append(endofline);
        locsb.append("Location: ").append(res.getString(4)).append(endofline);
        if (endofline.length() == 0) {
            endofline = "\n";
        }
    }
    textViewId.setText(idsb.toString());
    textViewName.setText(namesb.toString());
    textViewDescription.setText(descsb.toString());
    textViewLocation.setText(locsb.toString());
}

但是,您可能想要使用ListView或RecyclerView。

其他-使用ListView

重要提示

此示例使用了一个CursorAdapter,使生活变得非常轻松,但它们要求存在一个专门命名为 _id 的列(id不会存在)。您可能需要更改表,以便将该列命名为 _id (可以使用下面的DBHelper中使用的常量BaseColumns._ID)。还有其他方法,例如在SELECT(查询)中创建_id列,例如SELECT rowid AS _id,* FROM the_table

1 在活动的布局中,添加一个ListView(替换id / name / description / location的文本视图),确保您为其指定ID,例如:-

<ListView
    android:id="@+id/my_listview"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
</ListView>

2 创建一个新的布局 mylistview_item.xml ,它将有4个文本视图,例如

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
    <TextView
        android:id="@+id/textview_id"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <TextView
        android:id="@+id/textview_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <TextView
        android:id="@+id/textview_description"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <TextView
        android:id="@+id/textview_location"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>
  • 这将是列表视图中每行(项目)使用的布局

3 添加以下类变量:-

SimpleCursorAdapter mSCA; //Adapts/Handles the data for the listview
ListView mList;
Cursor mCsr;

4 将以下int数组添加为类变量:-

int[] item_layout_ids_for_list = new int[]{
        R.id.textview_id,
        R.id.textview_name,
        R.id.textview_description,
        R.id.textview_location
};
  • 请注意,这些是布局 mylistview_item.xml 中TextView的ID,适配器将使用它们将数据从列映射到TextView。

5 类似地,为要从中检索数据的列名添加一个String数组;但是请注意,您将/可能不得不更改列名(以下使用数据库帮助器中定义的常量(建议定义此类常量,并且始终使用常量而不是硬编码名称)),例如:-

String[] columns_to_list = new String[]{
            DBHelper.COL_MYTABLE_ID,
            DBHelper.COl_MYTABLE_NAME,
            DBHelper.COL_MYTABLE_DESCRIPTION,
            DBHelper.COl_MYTABLE_LOCATION
    };

6 将以下方法添加到活动中:-

private void manageListView() {
    mCsr = myDb.getAllData();
    if (mSCA == null) {
        // Builds the Adapter for the List
        mSCA = new SimpleCursorAdapter(
                this,
                R.layout.mylistview_item, mCsr,
                columns_to_list,
                item_layout_ids_for_list,
                0
        );
        mList.setAdapter(mSCA); // Ties the Adapter to the ListView
    } else {
        mSCA.swapCursor(mCsr); // Refresh the List
    }
}
  • 这就像方法所说的那样管理ListView。它将数据从db获取到Cursor中,构造(实例化)SimpleCursorAdapter(如果尚未实例化),然后将适配器绑定到ListView。如果适配器已实例化,它会告诉适配器有一个新的(更改的)游标(因此刷新了列表)。

7

将以下行添加到活动的onCreate方法中:-

    mList = this.findViewById(R.id.my_listview);
    myDb = new DBHelper(this); //<<<<<<<<<< SHOULD ALREADY HAVE SOMETHING LIKE THIS LEAVE AS IT IS
    manageListView(); 

8 使用以下方法覆盖活动的onResume和onDestroy方法:-

@Override
protected void onDestroy() {
    super.onDestroy();
    mCsr.close();
}

@Override
protected void onResume() {
    super.onResume();
    manageListView();
}
  • 这还不是必需的,但是是很好的实践。
  • onDestroy关闭活动被破坏时的游标(完成后应关闭游标)
    • 对主要活动不太重要
  • onResume被忽略,以调用manageListView()方法(尚未添加),并将导致列表视图显示从另一个活动返回时的最新数据。

我用来测试的活动是:-

public class MainActivity extends AppCompatActivity {

    DBHelper myDb;
    SimpleCursorAdapter mSCA; //Adapts/Handles the data for the listview
    ListView mList;
    Cursor mCsr;
    String[] columns_to_list = new String[]{
            DBHelper.COL_MYTABLE_ID,
            DBHelper.COl_MYTABLE_NAME,
            DBHelper.COL_MYTABLE_DESCRIPTION,
            DBHelper.COl_MYTABLE_LOCATION
    };
    int[] item_layout_ids_for_list = new int[]{
            R.id.textview_id,
            R.id.textview_name,
            R.id.textview_description,
            R.id.textview_location
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mList = this.findViewById(R.id.my_listview);
        myDb = new DBHelper(this);
        forTestingAddSomeData();
        manageListView();
    }

    private void manageListView() {
        mCsr = myDb.getAllData();
        if (mSCA == null) {
            // Builds the Adapter for the List
            mSCA = new SimpleCursorAdapter(
                    this,
                    R.layout.mylistview_item, mCsr,
                    columns_to_list,
                    item_layout_ids_for_list,
                    0
            );
            mList.setAdapter(mSCA); // Ties the Adapter to the ListView
        } else {
            mSCA.swapCursor(mCsr); // Refresh the List
        }
    }

    private void forTestingAddSomeData() {
        if(DatabaseUtils.queryNumEntries(myDb.getWritableDatabase(),DBHelper.TABLE_MYTABLE) < 1) {
            myDb.add("Test001","This is a test","Home");
            myDb.add("Test002","For Testing","Garage");
            myDb.add("Test003","Test using this","Loft");
            myDb.add("Test004","Yet again for testing","Cupboard");
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mCsr.close();
    }

    @Override
    protected void onResume() {
        super.onResume();
        manageListView();
    }
}
  • 注意 forTestingAddSomeData 和对该方法的调用只是为了添加一些测试数据。逐步指南中省略了添加和使用此方法(以便显示一些数据)的操作。

如果遇到问题或想要使用完整的代码,则DBHelper类为:-

public class DBHelper extends SQLiteOpenHelper {

    public static final String DBNAME = "mydb";
    public static final int DBVERSION = 1;
    public static final String TABLE_MYTABLE = "mytable";
    public static final String COL_MYTABLE_ID = BaseColumns._ID;
    public static final String COl_MYTABLE_NAME = "name";
    public static final String COL_MYTABLE_DESCRIPTION = "description";
    public static final String COl_MYTABLE_LOCATION = "location";

    SQLiteDatabase mDB;

    public DBHelper(Context context) {
        super(context, DBNAME, null, DBVERSION);
        mDB = this.getWritableDatabase();
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("CREATE TABLE IF NOT EXISTS " + TABLE_MYTABLE +
                "(" +
                COL_MYTABLE_ID + " INTEGER PRIMARY KEY," +
                COl_MYTABLE_NAME + " TEXT, " +
                COL_MYTABLE_DESCRIPTION + " TEXT, " +
                COl_MYTABLE_LOCATION + " TEXT" +
                ")"
        );
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }

    public long add(String name, String description, String location) {
        ContentValues cv = new ContentValues();
        cv.put(COl_MYTABLE_NAME,name);
        cv.put(COL_MYTABLE_DESCRIPTION,description);
        cv.put(COl_MYTABLE_LOCATION,location);
        return mDB.insert(TABLE_MYTABLE,null,cv);
    }

    public Cursor getAllData() {
        return mDB.query(TABLE_MYTABLE,null,null,null,null,null,null);
    }
}

结果

运行上面的结果是:-

enter image description here