自定义列表视图未显示字符串

时间:2015-01-05 04:51:20

标签: android

Listview没有显示字符串列表,而只显示自定义列表视图文本

主要

的字符串声明
String[] Example = new String[]
        { "Android Introduction","Android Setup/Installation","Android Hello World",
                "Android Layouts/Viewgroups","Android Activity & Lifecycle","Intents in Android",
        "Cz"};
ArrayAdapter<String> ExampleArrayAdapter = new CustomAdapter(this, Example);

    ListView ExampleListView = (ListView)findViewById(R.id.listView);
    ExampleListView.setAdapter(ExampleArrayAdapter);

这是我的customAdapter.java

public class CustomAdapter extends ArrayAdapter {
private LayoutInflater inflater;
public CustomAdapter (Activity activity, String[] items){
    super(activity, R.layout.custom_layout, items);
    inflater = activity.getWindow().getLayoutInflater();
}

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

   int color = Color.argb(255, rnd.nextInt(256), rnd.nextInt(257), rnd.nextInt(258));

  View rowView = inflater.inflate(R.layout.custom_layout, parent, false);
    rowView.setBackgroundColor(color);

    return rowView;


}
}

这是我的应用程序的自定义布局,我更新了它........... custom_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_marginTop="70dp"
android:layout_height="70dp"
android:background="@drawable/blue_bg">

<LinearLayout
    android:layout_marginLeft="20dp"
    android:layout_width="345dp"
    android:layout_height="70dp"
    android:background="@drawable/white_bg">
    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="70dp"
        android:layout_height="60dp"
        android:src="@drawable/ic_launcher"
        android:layout_marginLeft="5dp"
        android:layout_marginTop="5dp" />


    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_marginLeft="10dp"
        android:gravity="left" >


        <TextView
            android:id="@+id/textView1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Large Text"
            android:layout_marginTop="10dp"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:textSize="19sp"
            android:textColor="#000000"
            android:textStyle="bold" />


        <TextView
            android:id="@+id/textView2"
            android:layout_width="match_parent"
            android:layout_height="15dp"
            android:text="Tweet body text here"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:layout_marginTop="5dp"
            android:ellipsize="end"
            android:lines="3"
            android:textColor="#000000"
            android:textSize="14sp" />


    </LinearLayout>

</LinearLayout>

</LinearLayout>

2 个答案:

答案 0 :(得分:3)

  

Listview没有显示字符串列表而只显示自定义   列表视图文本

因为你没有在ListView的每一行中为TextView设置文本,只需设置行的颜色。

所以自定义getView方法并从items String Array设置TextView文本。请参阅以下示例获取更多帮助: Using an ArrayAdapter with ListView

答案 1 :(得分:1)

您必须在Activity As:

中声明CustomAdapter对象
CustomAdapter ExampleArrayAdapter = new CustomAdapter(this, Example);

    ListView ExampleListView = (ListView)findViewById(R.id.listView);
    ExampleListView.setAdapter(ExampleArrayAdapter);

您的适配器代码应为:

public class CustomAdapter extends ArrayAdapter{
private LayoutInflater inflater;
private String[] items;
public CustomAdapter (Activity activity, String[] items){
          super(activity, 0, items);
    inflater = activity.getWindow().getLayoutInflater();
}

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

   int color = Color.argb(255, rnd.nextInt(256), rnd.nextInt(257), rnd.nextInt(258));

  View rowView = inflater.inflate(R.layout.custom_layout, parent, false);
    rowView.setBackgroundColor(color);
TextView tv = (TextView) rowView .findViewById(R.id.textView1);
tv.setText(items[position]);
    return rowView;


}
}