【发布时间】:2013-07-23 17:01:05
【问题描述】:
好吧,我一直坚持在 Android 中填充 ListView。这对你们来说一定是一个很累的问题,但我找不到问题。基本上它会在 ListView 中生成放置文本的位置,但不会生成文本。我检查了我的数据库类,它似乎正确地存储了数据,我检查了语法,但我找不到问题。
保存列表视图的主要活动
public class MainScreen extends ListActivity {
private TextView roommateId;
dbHelper db = new dbHelper(this);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_screen);
Log.i("Created", "main created");
ArrayList<HashMap<String, String>> roommates = db.getAllRoommates();
if(roommates.size()!=0){
ListView listView = getListView();
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
roommateId = (TextView) view.findViewById(R.id.roommateId);
String roommateIdValue = roommateId.getText().toString();
Intent intent = new Intent(getApplication(), RoommateView.class);
intent.putExtra("roommateId", roommateIdValue);
startActivity(intent);
}
});
ListAdapter adapter = new SimpleAdapter(MainScreen.this, roommates, R.layout.contact_entry,
new String[] {"roommateId", "firstName", "lastName"},
new int[]{R.id.roommateId, R.id.lastName, R.id.firstName});
setListAdapter(adapter);
}
}
返回数组列表的数据库代码
public ArrayList<HashMap<String,String>> getAllRoommates(){
ArrayList<HashMap<String,String>> list = new ArrayList<HashMap<String,String>>();
String query = "SELECT * FROM " + DB_TABLE;
SQLiteDatabase data = this.getWritableDatabase();
Cursor cursor = data.rawQuery(query, null);
if(cursor.moveToFirst()){
do{
HashMap<String,String> roommateMap = new HashMap<String, String>();
roommateMap.put(ID, cursor.getString(0));
Log.d("Roommate ID", cursor.getString(0));
roommateMap.put(FIRST_NAME, cursor.getString(1));
Log.d("First Name", cursor.getString(1));
roommateMap.put(LAST_NAME, cursor.getString(2));
list.add(roommateMap);
}while(cursor.moveToNext());
}
return list;
}
联系人条目 xml
<?xml version="1.0" encoding="utf-8"?>
<TableRow xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="@string/last_name"
android:id="@+id/lastName"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="@string/first_name"
android:id="@+id/firstName"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="@string/id"
android:id="@+id/roommateId"/>
</TableRow>
主屏幕xml
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<TableRow
android:id="@+id/tableRow1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#000000" >
<TextView
android:id="@+id/contactsTitleTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/app_name"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#FFFFFF"
android:layout_weight="1"/>
<Button
android:id="@+id/button1"
android:background="#444444"
android:onClick="showAddRoommate"
android:text="@string/add_roommate"
android:textColor="#FFFFFF"
android:textSize="20sp" />
</TableRow>
<TableRow
android:id="@+id/tableRow2"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1">
</ListView>
</TableRow>
</TableLayout>
【问题讨论】:
-
logcat 中没有错误?
-
我看不到任何关于我的程序的具体内容,只是一些关于电子邮件应用程序无法正常工作的事情。但我只是一个新手,还在学习,所以我可能读错了。
-
您的代码中是否有
catch(Exception)或类似名称?如果是这样,捕获的主体是什么? -
没有问题,我想我应该在某个地方放一个?我似乎在运行它时没有任何问题,当我尝试添加某人时没有崩溃,数据库正在记录它们,正如我在执行 log.d 并打印出结果时所看到的那样。应用程序失败的唯一一次是当我尝试单击列表视图项目时,但这不是我关注的问题。至于捕获(异常)我应该把它放在哪里的任何建议?
-
您在哪里将数据库数据分配给列表适配器?我没有看到这段代码。还有你在修改列表适配器后调用
notifyDataSetChanged()吗?
标签: java android sqlite android-listview