ListView的图像不起作用

时间:2015-04-02 10:54:12

标签: android android-layout android-listview

我尝试自定义ListView以显示图像列表,因此我编写了以下代码,但遗憾的是它无效。

布局/ menu.xml文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent" android:background="@drawable/menu_bg">
    <ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/name"
    android:layout_gravity="center"/>

   <ListView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/menu_list"
    android:layout_gravity="center"
    android:layout_marginTop="60sp"></ListView>
 </LinearLayout>

布局/ menu_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
   <ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/item"/>
</LinearLayout>

Menu.java

public class Menu extends Activity{


private int[] items = {R.drawable.res_opt, R.drawable.new_game_opt, R.drawable.score_opt, R.drawable.exit_opt};
ListView list;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.menu);
    list = (ListView) findViewById(R.id.menu_list);
    list.setAdapter(new MenuAdapter(Menu.this, items));
 }
}

MenuAdapter.java

 public class MenuAdapter extends ArrayAdapter{

private int[] items;
private Context context;

public MenuAdapter(Context context, int[] items) {
    super(context, R.layout.menu_item);
    this.context = context;
    this.items = items;
}



@Override
public View getView(int position, View convertView, ViewGroup parent) {

    //make sure we have a view to work with
    View itemView = convertView;
    if(itemView == null)
    {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService( Context.LAYOUT_INFLATER_SERVICE );
        itemView = inflater.inflate(R.layout.menu_item,null, true);
    }
        //Find the object to work with
        int id = items[position];

        //Fill the view
        ImageView item = (ImageView) itemView.findViewById(R.id.item);
        item.setImageResource(id);

    return itemView;

 }

}

2 个答案:

答案 0 :(得分:0)

将视图持有者类添加到您的代码中,因为它不完整。

    public class ViewHolder{
        public ImageView iv;
    }

现在,您的代码将如下所示:

ViewHolder mViewHolder = new ViewHolder();
if(itemView == null){
    LayoutInflater inflater = (LayoutInflater) context.getSystemService( Context.LAYOUT_INFLATER_SERVICE );
    itemView = inflater.inflate(R.layout.menu_item,null, null);
    mViewHolder.iv = (ImageView) itemView.findViewById(R.id.item);
    itemView.setTag(mViewHolder);
}else{
    mViewHolder = (ViewHolder) itemView.getTag();
}
//Find the object to work with
int id = items[position];
//Fill the view
mViewHolder.iv.setImageResource(id);

此外,您错过了为ListView适配器添加大小。

希望,这可以帮助你。

答案 1 :(得分:0)

您需要使用ArrayAdapter的另一个超级构造函数。

public class MenuAdapter extends ArrayAdapter<Integer>{

private Integer[] items;
private Context context;

public MenuAdapter(Context context, Integer[] items) {
    super(context, R.layout.menu_item, items);
    this.context = context;
    this.items = items;
}

<强>被修改

同时修复此行

itemView = inflater.inflate(R.layout.menu_item,parent, false);