【发布时间】:2015-11-28 11:27:47
【问题描述】:
我正在尝试创建一个仅包含图像的 listView。请这不是一个愚蠢的问题,所以任何帮助表示赞赏。我认为我的代码是正确的,但是当我运行它时,它只显示 contentView 但为空。它没有显示我告诉它显示的图像。我创建了 java 文件,然后创建了两个 xml 文件,一个是 listView,它是 java 文件的 contentView,另一个是 listView 的单行。
我的 java 文件:
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
public class Craft extends Activity {
int[]images={R.drawable.one,R.drawable.two,R.drawable.three,R.drawable.four,R.drawable.five,R.drawable.six,R.drawable.seven;
ListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.craft);
listView=(ListView)findViewById(R.id.listView);
MyAdapter adapter=new MyAdapter(Craft.this,images);
listView.setAdapter(adapter);
}
class MyAdapter extends ArrayAdapter<String> {
Context c;
int[] images;
int count = 0;
public MyAdapter(Context c,int imgs[]) {
super(Craft.this, R.layout.single_row);
this.c = Craft.this;
this.images = imgs;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View row = inflater.inflate(R.layout.single_row, parent, false);
Log.d("Plates", "Count: " + count++);
ImageView myImage = (ImageView) row.findViewById(R.id.imageView23);
myImage.setImageResource(images[position]);
return row;
}
}
}
我的craft.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:background="@drawable/woodcrop">
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/listView"
android:layout_gravity="center_vertical"
android:layout_weight="1" />
我的 single_row.xml 文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:background="@drawable/woodcrop">
<ImageView
android:layout_margin="20dp"
android:layout_width="48dp"
android:layout_height="48dp"
android:src="@drawable/one"
android:id="@+id/imageView23" />
感谢任何帮助!
【问题讨论】:
-
您的 getView() 方法未在您的 MyAdapter 类中执行
标签: android xml listview android-listview android-image