简体   繁体   中英

I can't see the first item added to my ListView

I have a database made with OpenSQLiteHelper which where I am getting a String of names from and adding it to a ListView. I can never see the first item in the database and the ListView only starts from the second item in the Database.

I am getting Strings from COL_2 (names) in my database.

ListView Class

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 Code

<?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>

Database Helper Class

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

Thanks for the help!

In my research of using Cursor your initial setup is correct

while(data.moveToNext())...

Cursor starts before the rows and the initial call moveToNext() moves it to the first row.

One glaring issue is that you are not closing the Cursor.

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

The second thing I see being a potential issue is that you are not accessing the correct value in the 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;
            }

不确定,但问题似乎出在这里:while(data.moveToNext())可能是在离开上一个项目之后移动到下一个项目时触发moveToNext(),换句话说,它将在第一个项目上触发将项目#1留在后面的时间)尝试使用索引循环代替.ie for(int i = 0; ....

In this code:

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

It will trigger the moveToNext() function and then do the while. So you could solve it using a do while instead

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM