0

好吧,我坚持在 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>
4

1 回答 1

1

好吧,我想我找到了问题,它是简单适配器中的字符串数组与哈希映射键值不对应,所以当它在寻找哈希映射中没有的键值时。改变它并填充它。把答案写下来,以防将来有人看到这篇文章。

于 2013-07-30T15:02:12.950 回答