自定义ListView,文本和图像未显示在“活动”中
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了自定义ListView,文本和图像未显示在“活动”中相关的知识,希望对你有一定的参考价值。
我是android的新手,我正在尝试使用基于自定义适配器的ListView
和ImageView
创建TextView
。
不知何故,当我尝试运行我的应用程序时,它似乎不会将ListView
放在屏幕上。不过没有例外。也许我错过了一些非常明显的东西。如果有人可以查看我的代码,那将是很棒的:
my activity.Java:
public class MyActivity extends Activity {
public static final String[] countries = new String[] {"Germany", "USA", "Great Britain", "Japan", "Korea"};
public static final int[] images = new int[]{R.drawable.de, R.drawable.us, R.drawable.kr, R.drawable.jp, R.drawable.kr};
ListView listView;
List<MyListObject> values;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
TextView textView = (TextView) findViewById(R.id.textView);
values = new ArrayList<MyListObject>();
for (int i = 0; i < countries.length; i++) {
MyListObject item = new MyListObject();
item.setCountry(countries[i]);
item.setImage(images[i]);
values.add(item);
}
listView = (ListView) findViewById(R.id.list);
MySimpleArrayAdapter adapter = new MySimpleArrayAdapter(this, R.layout.rowlayout, values);
listView.setAdapter(adapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.my, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
my simple array adapter.Java:
public class MySimpleArrayAdapter extends ArrayAdapter<MyListObject> {
private final Context context;
private final List<MyListObject> values;
public MySimpleArrayAdapter(Context context, int resourceID, List<MyListObject> values) {
super(context, resourceID, values);
this.context = context;
this.values = values;
}
private class ViewHolder {
ImageView imageView;
TextView textView;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
MyListObject rowItem = getItem(position);
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = inflater.inflate(R.layout.rowlayout, null);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder = new ViewHolder();
holder.textView = (TextView) convertView.findViewById(R.id.countryName);
holder.imageView = (ImageView) convertView.findViewById(R.id.countryImage);
holder.textView.setText(rowItem.getCountry());
holder.imageView.setImageResource(rowItem.getImage());
return convertView;
}
@Override
public MyListObject getItem(int position) {
return values.get(position);
}
}
activity_my.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MyActivity">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/textView"/>
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/list"
android:layout_weight="1" />
</LinearLayout>
rowlayout.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:gravity="center_vertical" >
<ImageView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingRight="10dp"
android:id="@+id/countryImage" />
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/countryName"
android:text="@+id/countryName" />
</LinearLayout>
my list object.Java:
public class MyListObject {
private int image;
private String country;
public int getImage() {
return image;
}
public void setImage(int image) {
this.image = image;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
}
在activity_my's
最热的linearlayout
应该有android:orientation="vertical"
属性
在你的rowlayout
父布局是Horizontal LinearLayout
和android:layout_width
为ImageView
和TextView
是fill_parent
.So,只有ImageView
是可见的LinearLayout
儿童地方线性逐一.Change rowlayout
像这样
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:padding="8dp"
android:orientation="horizontal" >
<ImageView
android:id="@+id/countryImage"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:paddingRight="10dp" />
<TextView
android:id="@+id/countryName"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="countryName" />
</LinearLayout>
你也需要改变你的Adapter
MySimpleArrayAdapter
public class MySimpleArrayAdapter extends ArrayAdapter<MyListObject> {
private final Context context;
private final List<MyListObject> values;
private int resourceID;
public MySimpleArrayAdapter(Context context, int resourceID, List<MyListObject> values) {
super(context, resourceID, values);
this.context = context;
this.resourceID = resourceID;
this.values = values;
}
private class ViewHolder {
ImageView imageView;
TextView textView;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(resourceID, null);
holder = new ViewHolder();
holder.textView = (TextView) convertView.findViewById(R.id.countryName);
holder.imageView = (ImageView) convertView.findViewById(R.id.countryImage);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
MyListObject rowItem = (MyListObject) values.get(position);
holder.textView.setText(rowItem.getCountry());
holder.imageView.setImageResource(rowItem.getImage());
return convertView;
}
}
截图:
试试这个:
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(layout, parent, false);
//...
return rowView;
}
在我的自定义适配器类下面,请参考它,并按照这种方式,它肯定会工作。
private class EfficientAdapter extends BaseAdapter {
private LayoutInflater mInflater;
private Context context;
JSONArray array;
private Bitmap imageBitmap = null;
public EfficientAdapter(Context context) {
mInflater = LayoutInflater.from(context);
this.context = context;
}
@Override
public int getCount() {
return data.size();
}
@Override
public Object getItem(int arg0) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.featured_item, null);
holder = new ViewHolder();
holder.txtTitle = (TextView) convertView.findViewById(R.id.txtTitle);
holder.txtAuthorName = (TextView) convertView.findViewById(R.id.txtAuthorName);
holder.txtDescription = (TextView) convertView.findViewById(R.id.txtDescription);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.txtTitle.setText(data.get(position).get("article_title"));
holder.txtAuthorName.setText(data.get(position).get("author_name"));
holder.txtDescription.setText(data.get(position).get("article_text"));
return convertView;
}
class ViewHolder {
TextView txtTitle;
TextView txtAuthorName;
TextView txtDescription;
}
}
你的LinearLayout
的方向应该是vertical
。默认是horizontal
。加
android:orientation="vertical"
到包含LinearLayout
的ListView
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MyActivity">
这可能会解决您的可视化问题。另一方面,您可以更改一些内容以提高性能。
- 在您的布局中,您应该提供
0dip
作为高度,因为您提供了重量。这将有助于“onLayout
”时间的框架。 ArrayAdapter
具有getItem()
的具体实现。您不需要再次覆盖它,因为您已经将数据集提供给超类- 您以错误的方式使用
ViewHolder
模式。您应该实例化ViewHolder
一次,当convertView为null时,查找视图(如您所做)并调用convertView.setTag(viewHolderInstance)
,
确保图像宽度和高度未设置为匹配约束。另外,尝试像这样更新主活动中的适配器
public void updateAdapter(MyListObject _newObject){
objectList.add(_newObject);
adapter.notifyDataSetChanged();
}
以上是关于自定义ListView,文本和图像未显示在“活动”中的主要内容,如果未能解决你的问题,请参考以下文章
我想在自定义列表视图中选择一项,该项目将显示在第二个活动的文本视图上
ListView CustomAdapter 未显示第一个位置结果