当我尝试从模拟器中添加数据时,我的数据库出现问题,它把它放在错误的列中,我没把握它从0开始,例如(1collumnn = 0,2collumn = 1 ....)
我认为我将其设置为1以将数据放入2列中,但是将其置于3列中,但是当将其设置为“ 0”时,它会像推测的那样向第一个列中添加数据。任何人都有任何想法为什么会令人开心?
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);
}
}
}
}
不要像这样使用列的索引:
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:
Replace
columnName
with the name of the column that you want.