2012-03-24 63 views
0

如何使用图片和摘要(项目列表文本下的文本)制作一个列表,对项目的描述很少。使用摘要制作ListActivity

我的代码与图片列表。我如何添加摘要?

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" > 

<ImageView 
    android:id="@+id/icon" 
    android:layout_width="22px" 
    android:layout_height="22px" 
    android:layout_marginLeft="4px" 
    android:layout_marginRight="10px" 
    android:layout_marginTop="4px" 
    android:src="@drawable/ic_launcher" > 
</ImageView> 

<TextView 
    android:id="@+id/label" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="@+id/label" 
    android:textSize="20px" > 
</TextView> 

</LinearLayout> 

活动类:

public class MyListActivity extends ListActivity { 
    public void onCreate(Bundle icicle) { 
     super.onCreate(icicle); 
     String[] values = new String[] { "Android", "iPhone", "WindowsMobile", 
      "Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X", 
      "Linux", "OS/2" }; 
     // Use your own layout 
     ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, 
     R.layout.rowlayout, R.id.label, values); 
     setListAdapter(adapter); 
    } 

    @Override 
    protected void onListItemClick(ListView l, View v, int position, long id) { 
     String item = (String) getListAdapter().getItem(position); 
     Toast.makeText(this, item + " selected", Toast.LENGTH_LONG).show(); 
    } 
} 

回答

1

您可以创建一个costum布局的costum列表视图。

在互联网上,你找到了很多很好的教程来教你。

下面是一个例子: http://www.softwarepassion.com/android-series-custom-listview-items-and-adapters/

你的情况下,唯一不同的是,图像是在文本的左边,你想让它在顶部:)。

如果您的列表始终具有相同的内容,您可以创建一个已知该内容的适配器。否则,您需要创建一个类来保存您将在列表中拥有的内容类型。看起来你的内容是静态的,所以,我会建议你遵循一些行。

主要是你必须创建你的适配器:

private class MyAdapter extends ArrayAdapter { 

    private String[] values = new String[] { "Android", "iPhone", "WindowsMobile", 
     "Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X", 
     "Linux", "OS/2" }; 

    private int[] myImagesSaved = new Int[] { R.drawable.image1, 
               R.drawable.image2, ... } 

    public MyAdapter (Context context, int textViewResourceId) { 
     super(context, textViewResourceId, values); 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     View v = convertView; 

     if (v == null) { 
      LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      v = vi.inflate(R.layout.row, null); 
     }    

     TextView text = (TextView) v.findViewById(R.id.icon); 
     ImageView image = (TextView) v.findViewById(R.id.label); 

     if (text != null) 
      text.setText(values[position]);       } 

     if(image != null) 
      image.setBackgroundResource(myImagesSaved[position]); 

     return v; 
    } 
} 
+0

你能告诉我什么是摘要的方法,因为我更喜欢留在我的代码中。谢谢 – user1257040 2012-03-24 19:55:43

+0

方法摘要?你是什​​么意思?如果你想适应你的问题,你可以在你的MyListActivity上使用你的xml和几乎相同的代码。你只需要创建一个适配器和getView(int position,View convertView,ViewGroup parent)。我会尽力完成我的答案。 – 2012-03-24 20:09:40

+0

摘要=该项目下的小说明,它就像另一个文本视图。 – user1257040 2012-03-24 20:47:56

1

创建列表视图 定义适配器如果要自定义列表视图,那么你必须创建一个自定义适配器。 有一些适配器,如ArrayAdapter,BaseAdapter,CusrsorAdapter等。 要创建自定义适配器,请在类中的适配器类上继承并覆盖方法。

这里是一个链接,自定义适配器 http://www.softwarepassion.com/android-series-custom-listview-items-and-adapters/

+2

不要让我downvote你。你知道这不是质量足够好的答案。 – 2012-03-24 18:33:19

+0

你可以给我教程或给我的方法摘要? – user1257040 2012-03-24 18:35:53

+0

@BorisStrandjev雅更新我的答案 – Dharmendra 2012-03-24 18:37:48