我已将此代码实施到我的应用中,以便保存名称,然后在ListView
中显示该名称。我想我已经将名称添加到数据库中,但它显示空白ListView
。
SQLDbHelper
public class SqlDbHelper extends SQLiteOpenHelper
{
public static final String DATABASE_TABLE = "PHONE_CONTACTS";
public static final String COLUMN1 = "name";
private static final String SCRIPT_CREATE_DATABASE = "create table "
+ DATABASE_TABLE + " (" + COLUMN1
+ " text not null);";
public SqlDbHelper(Context context, String name, SQLiteDatabase.CursorFactory factory,
int version) {
super(context, name, factory, version);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL(SCRIPT_CREATE_DATABASE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
onCreate(db);
}
}
SqlHandler
public class SqlHandler
{
public static final String DATABASE_NAME = "MY_DATABASE";
public static final int DATABASE_VERSION = 1;
Context context;
SQLiteDatabase sqlDatabase;
SqlDbHelper dbHelper;
public SqlHandler(Context context) {
dbHelper = new SqlDbHelper(context, DATABASE_NAME, null,
DATABASE_VERSION);
sqlDatabase = dbHelper.getWritableDatabase();
}
public void executeQuery(String query) {
try {
if (sqlDatabase.isOpen()) {
sqlDatabase.close();
}
sqlDatabase = dbHelper.getWritableDatabase();
sqlDatabase.execSQL(query);
} catch (Exception e) {
System.out.println("DATABASE ERROR " + e);
}
}
public Cursor selectQuery(String query) {
Cursor c1 = null;
try {
if (sqlDatabase.isOpen()) {
sqlDatabase.close();
}
sqlDatabase = dbHelper.getWritableDatabase();
c1 = sqlDatabase.rawQuery(query, null);
} catch (Exception e) {
System.out.println("DATABASE ERROR " + e);
}
return c1;
}
}
List
适配器:
public class PlantListAdapter extends BaseAdapter
{
Context context;
ArrayList<PlantListitems> contactList;
public PlantListAdapter(Context context, ArrayList<PlantListitems> list) {
this.context = context;
contactList = list;
}
@Override
public int getCount() {
return contactList.size();
}
@Override
public Object getItem(int position) {
return contactList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup arg2) {
PlantListitems contactListItems = contactList.get(position);
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.plant_list_row, null);
}
TextView tvName = (TextView) convertView.findViewById(R.id.tv_name);
tvName.setText(contactListItems.getName());
return convertView;
}
}
用于显示数据库的 ListView
活动:
SqlHandler sqlHandler;
//private ArrayList<String> addArray;
//public static final String PREFS = "examplePrefs";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mygarden_list);
listView = (ListView) findViewById(R.id.mygardenlist);
sqlHandler = new SqlHandler(this);
//addArray = new ArrayList<>();
ArrayList<PlantListitems> contactList = new ArrayList<>();
//contactList.clear();
String query = "SELECT * FROM PHONE_CONTACTS ";
Cursor c1 = sqlHandler.selectQuery(query);
if (c1 != null && c1.getCount() != 0) {
if (c1.moveToFirst()) {
do {
PlantListitems plantListItems = new PlantListitems();
plantListItems.setName(c1.getString(c1
.getColumnIndex("name")));
} while (c1.moveToNext());
}
}
c1.close();
PlantListAdapter plantListAdapter = new PlantListAdapter(mygardenMain.this, contactList);
listView.setAdapter(plantListAdapter);
}
答案 0 :(得分:0)
contactList为空。
do {
PlantListitems plantListItems = new PlantListitems();
plantListItems.setName(c1.getString(c1
.getColumnIndex("name")));
//Add the plantListItem object to the ContactList arrayList.
contactList.add(plantList);
} while (c1.moveToNext());