Search code examples
javaandroidarraysandroid-arrayadapter

how to put int type array in string type arrayadapter?


or how do I insert images alongside with quotes? my idea is create an int array filled with R.drawable.image it doesnt work since the arrayadapter type is string. here's the code:

TitleListActivity.java

package course.examples.quoteviewer;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class TitlesListActivity extends ListActivity {

public static String[] mTitleArray;
public static String[] mQuoteArray;

public static final String INDEX = "index";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    mTitleArray = getResources().getStringArray(R.array.Titles);
    mQuoteArray = getResources().getStringArray(R.array.Quotes);
    setListAdapter(new ArrayAdapter<String>(TitlesListActivity.this,
            R.layout.list_text_item_layout, TitlesListActivity.mTitleArray));
}

@Override
public void onListItemClick(ListView l, View v, int pos, long id) {
    Intent showItemIntent = new Intent(TitlesListActivity.this,
            QuoteListActivity.class);
    showItemIntent.putExtra(INDEX, mQuoteArray[pos]);
    startActivity(showItemIntent);
}

}

QutoeListActivity.java

package course.examples.quoteviewer;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.ArrayAdapter;

public class QuoteListActivity extends ListActivity {

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Intent intent = getIntent();
    String quote = intent.getStringExtra(TitlesListActivity.INDEX);

    if (null != quote) {
        setListAdapter(new ArrayAdapter<String>(QuoteListActivity.this,
                R.layout.list_text_item_layout, new String[] { quote }));
    }
}
} 

list_text_item_layout.xml

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/empty"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:textSize="32sp" >
</TextView>

strings.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">FragmentQuoteViewerWithActivity</string>
<string-array name="Titles">
    <item>The Tragedy of Hamlet, Prince of Denmark</item>
    <item>King Lear</item>
    <item>Julius Caesar</item>
</string-array>
<string-array name="Quotes">
    <item>Now cracks a noble heart. Good-night, sweet prince; And flights of angels sing thee   to thy rest.</item>
    <item>As flies to wanton boys, are we to the gods; they kill us for their sport.</item>
    <item>There is a tide in the affairs of men, which taken at the flood, leads on to fortune.  Omitted, all the voyage of their life is bound in shallows and in miseries.</item>
</string-array>
</resources>

Solution

  • It looks like your probably want to create your own ArrayAdapter that is typed to a simple class that holds an image resource (int) and your quote (String).

    public class QuoteListAdapter extends ArrayAdapater<QuotedImage> {
        // check link for examples
    }
    
    static class QuotedImage {
        int imageResource;
        String quote;
    }
    

    Check here for examples.