gnarlyhogie
gnarlyhogie

Reputation: 63

Listview will not populate

Alright I'm stuck on populating a ListView in Android. This must be a tiring question for you guys but I can't find the problem. Basically it will produce placements in the ListView to hold texts, but it wont produce text. I checked my database class and it seems to be storing the data correctly, and I checked the syntax, but I cant find the problem.

Main activity that holds the list view

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);
        }
    }

Database code that returns the array list

 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;
    }

contact entry 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>

Main screen 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>

Upvotes: 0

Views: 154

Answers (1)

gnarlyhogie
gnarlyhogie

Reputation: 63

Alright, I think I found the problem, it was that the String array in the simple adapter wasn't corresponding to the hash map key values, so when it was looking for key values that were not in the Hash Map. Change that and it populated. Putting the answer down incase anyone in the future sees this post.

Upvotes: 1

Related Questions