gpt4 book ai didi

java - ListView 不会填充

转载 作者:行者123 更新时间:2023-12-01 14:18:57 25 4
gpt4 key购买 nike

好吧,我一直致力于在 Android 中填充 ListView。这对你们来说一定是一个很累的问题,但我找不到问题所在。基本上,它会在 ListView 中生成放置文本的位置,但不会生成文本。我检查了我的数据库类,它似乎正确存储了数据,我检查了语法,但我找不到问题。

持有 ListView 的主要 Activity

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>

最佳答案

好吧,我想我找到了问题所在,就是简单适配器中的 String 数组与 HashMap 键值不对应,所以当它查找不在 HashMap 中的键值时。改变它,它就填充了。把答案记下来,以防将来有人看到这篇文章。

关于java - ListView 不会填充,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17816498/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com