I'm having a problem with my database when I try to add data from the simulator it puts it in the wrong column. I understand that it begins from 0 like (1 column = 0, 2 column = 1....)
I think I set it to 1 to put the data in the 2 columns but instead it puts it in the 3 columns but when set it to "0" it adds data the first column like it supposed to. Anybody has any ideas why it might be happening?
package com.example.laivumusis;
import android.database.Cursor;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.Toast;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import java.util.ArrayList;
public class ViewListData extends AppCompatActivity {
KlausimynoDatabaseHelper myDB;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.editlistview_layout);
ListView listView = (ListView) findViewById(R.id.listView);
myDB = new KlausimynoDatabaseHelper(this);
ArrayList<String> theList = new ArrayList<>();
Cursor data = myDB.getListContents();
if (data.getCount() == 0) {
Toast.makeText(ViewListData.this,"Database tuščias!",Toast.LENGTH_LONG).show();
}else{
while (data.moveToNext()){
theList.add(data.getString(1));
ListAdapter listAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1,theList);
listView.setAdapter(listAdapter);
}
}
}
}
Don't use the index of the column like this:
theList.add(data.getString(1));
Use the method getColumnIndex()
by passing the name of the column, because the order of the columns may not be what you think it is.
So change to this:
theList.add(data.getString(data.getColumnIndex("columnName")));
Replace columnName
with the name of the column that you want.