或者我如何在引號旁插入圖片?我的想法是創建一個int數組填充R.drawable.image
它不工作,因爲arrayadapter類型是字符串。 下面的代碼:如何把int類型數組放入字符串類型arrayadapter?
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>