或如何将图像与引号一起插入?我的想法是创建一个填充R.drawable.image
的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>
答案 0 :(得分:0)
这超出了内置ArrayAdapter的功能,我担心。您需要创建一个自定义适配器来扩展ArrayAdapter并同时使用字符串和整数。它不必扩展ArrayAdapter,但这会使事情变得更容易。
在自定义适配器中,getView()方法应为每个列表项扩展自定义布局XML文件,此布局文件将包含显示图像和引用的视图。在这里,您将使用传递给适配器的数据填充ImageView和TextView。
Vogella有一个令人难以置信的资源来编写自定义列表适配器,在这里:http://www.vogella.com/tutorials/AndroidListView/article.html
答案 1 :(得分:0)
看起来您可能想要创建自己的ArrayAdapter
,其类型为包含图像资源(int
)和引用(String
)的简单类。
public class QuoteListAdapter extends ArrayAdapater<QuotedImage> {
// check link for examples
}
static class QuotedImage {
int imageResource;
String quote;
}
查看here示例。