Carlos
Carlos

Reputation: 355

How should I display values from few arrays in my custom Adapter for ListActivity?

For example I have such code:

    String[] values = new String[] { "Text1", "Text2", "Text3",
            "Text4", "Text5", "Text6", "Text7", "Text8",
            "Text9", "Text10" };
    String[] subvalues = new String[] {"subtext1", "subtext2", "subtext3", "subtext4", 
            "subtext5", "subtext6", "subtext7", "subtext8", "subtext9", "subtext10"};

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(
            this,
            R.layout.rawlayout,
            R.id.phone_number,
            values);
    setListAdapter(adapter);

This sample fills TextView R.id.phone_number with the values from the values array. However I have an other textView R.id.date_time and I want to fill it with the values from the subvalues array.

How should I modify my code to achieve my objective?

Upvotes: 1

Views: 100

Answers (2)

Adam Fręśko
Adam Fręśko

Reputation: 1064

How should I modificate my code to achieve my objective?

You can always use Cursor Joiner Witch allows to join multiple lists/cursors in to a a single cursor. When you have that create cursor adapter class(allows for custom layouts) witch you assign as adapter to your listView.

CursorJoiner joiner = new CursorJoiner(cursor1, projection1, cursor2, projection2);


for (CursorJoiner.Result joinerResult : joiner) {
    switch (joinerResult) {

    case LEFT: // handle case where a row in cursorA is unique


        Log.d(tag, "left");

        break;
    case RIGHT: // handle case where a row in cursorB is unique

        Log.d(tag, "right");
        break;
    case BOTH: // handle case where a row with the same key is in
           // both // cursors
        Log.d(tag, "both");
        break;
    }

You can also use a MatrixCursor

Upvotes: 1

Sam
Sam

Reputation: 86948

You can either combine your two Array into a List<Map<String, String>> and use a SimpleAdapter to display both values. Or you can create a custom Adapter that takes both of your Arrays and displays them appropriately.

I recommend using a custom Adapter, because SimpleAdapters are slower than customized code. If you have never created a custom Adapter there are a few Google I/O presentations by lead Android developers dedicated to this subject, like Turbo-Charge Your UI.

Upvotes: 1

Related Questions