2014-07-22 49 views
0

或者我如何在引号旁插入图片?我的想法是创建一个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> 

回答

0

它看起来像你可能想创建自己的ArrayAdapter,它的类型保存的图像资源(int)一个简单的类和你的报价(String)。

public class QuoteListAdapter extends ArrayAdapater<QuotedImage> { 
    // check link for examples 
} 

static class QuotedImage { 
    int imageResource; 
    String quote; 
} 

查询here的例子。

0

这超出了内置ArrayAdapter的功能,恐怕。您需要创建一个扩展ArrayAdapter的自定义适配器,并同时采用字符​​串和整数。它不必扩展ArrayAdapter,但可以让事情变得更简单。

在您的自定义适配器中,getView()方法应该为每个列表项添加自定义布局XML文件,并且此布局文件将包含视图以显示图像和报价。这是您将通过传递给适配器的数据填充ImageView和TextView的位置。

Vogella有一个令人难以置信的资源用于编写自定义列表适配器,在这里:http://www.vogella.com/tutorials/AndroidListView/article.html