虽然这里已经多次询问过这个问题,但我找不到合适的答案来适应我的代码。我正在尝试在我的应用程序中实现<ListView>
以便在我的活动中显示数据库中的数据,但每当我在模拟器上测试它时,都不会显示任何内容。我的代码没有任何错误,我一直试图找到问题几个小时没有成功。我意识到它可能很小但我无法确定问题。
非常感谢任何帮助。
我的数据库活动:
public class LysandrosDatabaseAdapter {
LysandrosHelper helper;
public LysandrosDatabaseAdapter (Context context) {
helper = new LysandrosHelper(context);
}
public long insertData (String name,
String surname,
String tnumber,
String bnumber,
String email,
String address,
String dob,
String department,
String workplace,
String stardate,
String notes)
{
SQLiteDatabase db = helper.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(LysandrosHelper.NAME, name);
contentValues.put(LysandrosHelper.SURNAME, surname);
contentValues.put(LysandrosHelper.NUMBER, tnumber);
contentValues.put(LysandrosHelper.BNUMBER, bnumber);
contentValues.put(LysandrosHelper.EMAIL, email);
contentValues.put(LysandrosHelper.ADDRESS, address);
contentValues.put(LysandrosHelper.DOB, dob);
contentValues.put(LysandrosHelper.DEPARTMENT, department);
contentValues.put(LysandrosHelper.WORKPLACE, workplace);
contentValues.put(LysandrosHelper.STARTDATE, stardate);
contentValues.put(LysandrosHelper.NOTES, notes);
long id = db.insert(LysandrosHelper.TABLE_NAME, null, contentValues);
db.close();
return id;
}
static class LysandrosHelper extends SQLiteOpenHelper {
static final String DATABASE_NAME = "LysandrosDatabase";
static final String TABLE_NAME = "LysandrosTable";
static final int DATABASE_VERSION = 4;
static final String UID = "_id";
static final String NAME = "Name";
static final String SURNAME = "Surname";
static final String NUMBER = "TelephoneNumber";
static final String BNUMBER = "BusinessNumber";
static final String EMAIL = "Email";
static final String ADDRESS = "Address";
static final String DOB = "DateofBirth";
static final String DEPARTMENT = "Department";
static final String WORKPLACE = "Workplace";
static final String STARTDATE = "StartDate";
static final String NOTES = "Notes";
private static final String CREATE_TABLE = "CREATE TABLE "+TABLE_NAME+"("+UID+" INTEGER PRIMARY KEY AUTOINCREMENT, " +
""+NAME+" VARCHAR(255), " +
""+SURNAME+" VARCHAR(255), "+
""+NUMBER+" INTEGER," +
""+BNUMBER+" INTEGER," +
""+EMAIL+" VARCHAR(255)," +
""+ADDRESS+" VARCHAR(255)," +
""+DOB+" DATE," +
""+DEPARTMENT+" VARCHAR(255)," +
""+WORKPLACE+" VARCHAR(255)," +
""+STARTDATE+" DATE," +
""+NOTES+" TEXT);";
private static final String DROP_TABLE = "DROP TABLE IF EXISTS " +TABLE_NAME;
private Context context;
public LysandrosHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
this.context = context;
Message.message(context, "Constructor Called");
}
public void onCreate (SQLiteDatabase db) {
try {
db.execSQL(CREATE_TABLE);
Message.message(context, "onCreate Called");
} catch (SQLException e) {
Message.message(context, ""+e);
}
}
public void onUpgrade (SQLiteDatabase db, int oldVersion, int newVersion) {
try {
Message.message(context, "onUpgrade Called");
db.execSQL(DROP_TABLE);
onCreate(db);
} catch (SQLException e) {
Message.message(context, ""+e);
}
}
}
我希望显示信息的我的EmployeeList活动:
import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.widget.EditText;
import android.view.View;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
/**
* Created by User on 7/25/2015.
*/
public class EmployeeList extends Activity {
EditText searchBar;
SQLiteDatabase db;
Cursor cursor;
ListAdapter adapter;
ListView employeeList;
/** Called when the activity is first created */
@Override
public void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.employeelist);
db = (new LysandrosDatabaseAdapter.LysandrosHelper(this)).getWritableDatabase();
searchBar = (EditText) findViewById (R.id.searchBar);
employeeList = (ListView) findViewById(R.id.list);
}
public void Search(View view) {
// || is the concatenation operation in SQLite
cursor = db.rawQuery("SELECT _id, Name, Surname, Department FROM LysandrosTable WHERE Name || ' ' || Surname LIKE ?",
new String[]{"%" + searchBar.getText().toString() + "%"});
adapter = new SimpleCursorAdapter(
this,
R.layout.employee_list_item,
cursor,
new String[] {"Name", "Surname", "Department"},
new int[] {R.id.EmployeeListName, R.id.EmployeeListSurname, R.id.EmployeeListDepartment});
employeeList.setAdapter(adapter);
}
}
我的employee_list XML文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<EditText
android:id="@+id/searchBar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="@string/searchhere"
android:layout_weight="1"/>
<Button
android:id="@+id/searchButton"
android:text="@string/search"
android:onClick="Search"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<ListView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/list">
</ListView>
</LinearLayout>
我的employee_list_item XML文件负责显示活动中的信息:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="8dp">
<TextView
android:id="@+id/EmployeeListName"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/EmployeeListSurname"
android:layout_marginStart="6dp"
android:layout_toEndOf="@+id/EmployeeListName"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/EmployeeListDepartment"
android:layout_below="@+id/EmployeeListName"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
答案 0 :(得分:0)
您没有在onCreate内的活动中调用Search(),因此您的列表视图无法设置适配器,这就是您的列表未填充的原因。