自定义列表视图未显示字符串
Custom Listview is not showing up the string
列表视图不显示字符串列表,而只显示自定义列表视图文本
我在主目录中的字符串声明
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>
您必须在 Activity 中将您的 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;
}
}
Listview is not showing the string list rather showing only the custom
list view text
因为您没有在 ListView 的每一行中为 TextView
设置文本,只是设置行的颜色。
因此自定义 getView
方法并从 items
字符串数组设置 TextView 文本。请参阅以下示例以获得更多帮助:
Using an ArrayAdapter with 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>
您必须在 Activity 中将您的 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;
}
}
Listview is not showing the string list rather showing only the custom list view text
因为您没有在 ListView 的每一行中为 TextView
设置文本,只是设置行的颜色。
因此自定义 getView
方法并从 items
字符串数组设置 TextView 文本。请参阅以下示例以获得更多帮助:
Using an ArrayAdapter with ListView