Android 仅包含图像的listView
我正在尝试创建一个仅包含图像的列表视图。请注意,这不是一个愚蠢的问题,因此我们非常感谢您的帮助。我认为我的代码是正确的,但当我运行它时,它只显示contentView,但为空。它没有显示我告诉它要显示的图像。我创建了java文件,然后创建了两个xml文件,一个带有listView,它是java文件的contentView,另一个用于listView的一行 我的java文件:Android 仅包含图像的listView,android,xml,listview,android-listview,android-image,Android,Xml,Listview,Android Listview,Android Image,我正在尝试创建一个仅包含图像的列表视图。请注意,这不是一个愚蠢的问题,因此我们非常感谢您的帮助。我认为我的代码是正确的,但当我运行它时,它只显示contentView,但为空。它没有显示我告诉它要显示的图像。我创建了java文件,然后创建了两个xml文件,一个带有listView,它是java文件的contentView,另一个用于listView的一行 我的java文件: import android.app.Activity; import android.content.Context;
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;
}
}
}
导入android.app.Activity;
导入android.content.Context;
导入android.os.Bundle;
导入android.util.Log;
导入android.view.LayoutInflater;
导入android.view.view;
导入android.view.ViewGroup;
导入android.widget.ArrayAdapter;
导入android.widget.ImageView;
导入android.widget.ListView;
公共类工艺扩展活动{
int[]images={R.drawable.1,R.drawable.2,R.drawable.3,R.drawable.4,R.drawable.5,R.drawable.6,R.drawable.7;
列表视图列表视图;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.craft);
listView=(listView)findViewById(R.id.listView);
MyAdapter=newMyAdapter(Craft.this,images);
setAdapter(适配器);
}
类MyAdapter扩展了ArrayAdapter{
上下文c;
int[]图像;
整数计数=0;
公共MyAdapter(上下文c,int-imgs[]){
超级(工艺、本、右布局、单行);
this.c=Craft.this;
该图像=imgs;
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
LayoutInflater充气器=(LayoutInflater)c.getSystemService(Context.LAYOUT\u充气器\u服务);
视图行=充气机。充气(R.layout.single_row,parent,false);
Log.d(“板”,“计数:“+Count++”);
ImageView myImage=(ImageView)row.findViewById(R.id.imageView23);
setImageResource(图像[位置]);
返回行;
}
}
}
my 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" />
非常感谢您的帮助!您应该将ListView高度更改为:
<ListView
.
.
.
android:layout_height="match_parent"/>
Ty无法使用此超级语句
Super(c,R.layout.single_row,imgs);您可以发布您完成my craft.xml吗?仅此而已。它只有ListView。您的getView()方法没有在MyAdapter类中执行
@Override
public int getCount() {
return images.length;
}