我无法看到添加到ListView的第一个项目

时间:2017-12-28 14:40:18

标签: java mysql sql sqlite listview

我有一个使用OpenSQLiteHelper创建的数据库,我从中获取一个名称字符串并将其添加到ListView。我永远看不到数据库中的第一项,ListView只能从数据库中的第二项开始。

我从数据库中的COL_2(名字)获取字符串。

ListView类

public class Castawaylist extends AppCompatActivity {

private static final String TAG = "CastList";
myDbAdapter mydb;
private ListView castview;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_castawaylist);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();

        }
    });

    castview = (ListView) findViewById(R.id.listView);
    mydb = new myDbAdapter(this);

    populateCastList();

}

String score;

private void populateCastList()
{
    //get the data and append to the list
    Log.d(TAG, "populateCastList: Displaying List of Castaways.");
    Cursor data = mydb.getData();
    //int save = data.getInt(2);

    ArrayList<String> listData = new ArrayList<>();


    while(data.moveToNext())
    {
        listData.add(data.getString(1) +
                "                                                                                                  " + data.getInt(2)); //get value from database at column 1, then add it to array list
    }



    ListAdapter adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, listData); //Creates the list adapter and set it
    castview.setAdapter(adapter);




    castview.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {


            String nametest = "";
            int u = 0;


                while(!adapterView.getItemAtPosition(i).toString().substring(u,u+1).equals(" "))
                {
                    nametest = nametest + adapterView.getItemAtPosition(i).toString().substring(u, u+1);
                    u = u + 1;
                }



            Log.d(TAG, "onItemClick: You Clicked on " + nametest);

            Cursor data = mydb.getItemID(nametest); //Get the ID associated with that name
            int itemID = -1;
            while(data.moveToNext())
            {
                itemID = data.getInt(0);
            }
            if(itemID > -1)
            {
                Log.d(TAG, "onItemClick: The ID is: " + itemID);
                Intent editCastaway = new Intent(Castawaylist.this, EditCastaway.class);
                editCastaway.putExtra("id", itemID);
                editCastaway.putExtra("name", nametest);
                //editCastaway.putExtra("score", data.getInt(2));
                startActivity(editCastaway);
            }else
            {
                toastText("No ID associated with that name");
            }
        }


    });
}



private void toastText(String text)
{
    Toast.makeText(this,text, Toast.LENGTH_SHORT).show();
}}

ListView XML代码

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">

<ListView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/listView"/>
</LinearLayout>

数据库助手类

public class myDbAdapter extends SQLiteOpenHelper{


public static final String DATABASE_NAME = "Castaways.db";
public static final String TABLE_NAME = "Survivor_Table";
public static final String COL_1 = "ID";
public static final String COL_2 = "NAME";
public static final String COL_3 = "MARK";

public myDbAdapter(Context context) {
    super(context, DATABASE_NAME, null, 2);
}

@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL("create table " + TABLE_NAME +" (ID INTEGER PRIMARY KEY AUTOINCREMENT,NAME TEXT,MARK INTEGER)");
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("DROP TABLE IF EXISTS "+TABLE_NAME);
    onCreate(db);
}

public boolean addData(String name,int score) {
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues contentValues = new ContentValues();
    contentValues.put(COL_2,name);
    contentValues.put(COL_3,score);
    long result = db.insert(TABLE_NAME,null ,contentValues);
    if(result == -1)
        return false;
    else
        return true;
}

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

public Cursor getItemID(String name){
    SQLiteDatabase db = this.getWritableDatabase();
    String query = "SELECT " + COL_1 + " FROM " + TABLE_NAME +
            " WHERE " + COL_2 + " = '" + name + "'";
    Cursor data = db.rawQuery(query, null);
    return data;
}


public boolean updateData(String id,String name,String surname,int score) {
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues contentValues = new ContentValues();
    contentValues.put(COL_1,id);
    contentValues.put(COL_2,name);
    contentValues.put(COL_3,score);
    db.update(TABLE_NAME, contentValues, "ID = ?",new String[] { id });
    return true;
}

public void updateName(String newName, int id, String oldName){
    SQLiteDatabase db = this.getWritableDatabase();
    String query = "UPDATE " + TABLE_NAME + " SET " + COL_2 +
            " = '" + newName + "' WHERE " + COL_1 + " = '" + id + "'" +
            " AND " + COL_2 + " = '" + oldName + "'";
    Log.d(TAG, "updateName: query: " + query);
    Log.d(TAG, "updateName: Setting name to " + newName);
    db.execSQL(query);
}


public Integer deleteData(String id) {
    SQLiteDatabase db = this.getWritableDatabase();
    return db.delete(TABLE_NAME, "ID = ?",new String[] {id});
}

public void deleteName(int id, String name){
    SQLiteDatabase db = this.getWritableDatabase();
    String query = "DELETE FROM " + TABLE_NAME + " WHERE "
            + COL_1 + " = '" + id + "'" +
            " AND " + COL_2 + " = '" + name + "'";
    Log.d(TAG, "deleteName: query: " + query);
    Log.d(TAG, "deleteName: Deleting " + name + " from database.");
    db.execSQL(query);
}

public void updateScore(int newScore, int id, int oldScore){
    SQLiteDatabase db = this.getWritableDatabase();
    String query = "UPDATE " + TABLE_NAME + " SET " + COL_3 +
            " = '" + newScore + "' WHERE " + COL_1 + " = '" + id + "'" +
            " AND " + COL_3 + " = '" + oldScore + "'";
    Log.d(TAG, "updateName: query: " + query);
    Log.d(TAG, "updateName: Setting name to " + newScore);
    db.execSQL(query);
}

public void clearData()
{
    SQLiteDatabase db = this.getWritableDatabase();
    db.delete(TABLE_NAME,null,null);
    db.close();
}}

感谢您的帮助!

3 个答案:

答案 0 :(得分:0)

在我使用光标的研究中,您的初始设置是正确的

while(data.moveToNext())...

光标在行之前开始,初始调用moveToNext()将它移动到第一行。

一个明显的问题是你没有关闭光标。

while(data.moveToNext())
{
    listData.add(data.getString(1) +
            "                                                                                                  " + data.getInt(2)); //get value from database at column 1, then add it to array list
}
// Close the Cursor here and again after the second one you create
data.close();

我认为存在潜在问题的第二件事是您没有在onClick中访问正确的值

// int i might not be 0 at some point
@Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
i--;// maybe try subtracting 1 from i. Alternative is to do that when i is being used
        String nametest = "";
        int u = 0;


      // or subtract here         
while(!adapterView.getItemAtPosition(i-1).toString().substring(u,u+1).equals(" "))
            {
                nametest = nametest + adapterView.getItemAtPosition(i-1).toString().substring(u, u+1);
                u = u + 1;
            }

答案 1 :(得分:0)

不确定,但问题似乎在这里: 而(data.moveToNext()) 可能是moveToNext()在离开前一个项目后移动到下一个项目时被触发,换句话说,它会在离开项目#1后第一次被触发) 尝试使用索引循环代替.i.e。 for(int i = 0; ....

答案 2 :(得分:0)

在此代码中:

while(data.moveToNext())
{
   itemID = data.getInt(0);
}

它将触发moveToNext()函数,然后执行while。所以你可以使用do while来解决它

do{
   itemID = data.getInt(0);
}while(data.moveToNext())