ListView的用法

易百洋 2016-07-05 06:11:26
ListView的用法 我按着书中代码写的,但是在模拟器中打开的时候出错 只要一打开就显示 很抱歉,已停止运行!
代码如下:
<?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:orientation="vertical" >
<ListView
android:id="@+id/list_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
></ListView>


</LinearLayout>





<?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:orientation="vertical" >
<ImageView
android:name="@+id/fruit_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

<TextView
android:name="@+id/fruit_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="10dip"
/>


</LinearLayout>



package com.eby.listviewtest;

public class Fruit {
private String name;
private int imageId;
public Fruit(String name,int imageId)
{
this.name=name;
this.imageId=imageId;
}
public String getName()
{
return name;
}
public int getImageId()
{
return imageId;
}
}



package com.eby.listviewtest;

import java.util.List;
import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class FruitAdapter extends ArrayAdapter<Fruit> {
private int resourceId;
public FruitAdapter(Context context, int resource, List<Fruit> objects) {
super(context, resource, objects);
// TODO 自动生成的构造函数存根
resourceId=resource;
}
@SuppressLint("ViewHolder")
@Override
public View getView(int position,View convertView,ViewGroup parent)
{
Fruit fruit = getItem(position);
View view = LayoutInflater.from(getContext()).inflate(resourceId, null);
ImageView fruitImage = (ImageView)view.findViewById(R.id.fruit_image);
TextView fruitName = (TextView)view.findViewById(R.id.fruit_name);
fruitImage.setImageResource(fruit.getImageId());
fruitName.setText(fruit.getName());
return view;
}
}




package com.eby.listviewtest;
import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.view.Window;
import android.widget.ListView;

public class MainActivity extends Activity {
private List<Fruit> fruitList = new ArrayList<Fruit>();
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main_layout);
initFruits();
FruitAdapter adapter = new FruitAdapter(MainActivity.this,R.layout.fruit_item,fruitList);
ListView listView= (ListView) findViewById(R.id.list_view);
listView.setAdapter(adapter);
}
private void initFruits() {
Fruit apple = new Fruit("Apple",R.drawable.apple);
fruitList.add(apple);
Fruit banana = new Fruit("Banana",R.drawable.banana);
fruitList.add(banana);
Fruit orange = new Fruit("Orange",R.drawable.orange);
fruitList.add(orange);
Fruit watermelon = new Fruit("Watermelon",R.drawable.watermelon);
fruitList.add(watermelon);
Fruit pear = new Fruit("Pear",R.drawable.pear);
fruitList.add(pear);
Fruit grape = new Fruit("Grape",R.drawable.grape);
fruitList.add(grape);
Fruit pineapple = new Fruit("Pineapple",R.drawable.pineapple);
fruitList.add(pineapple);
Fruit cherry = new Fruit("Cherry",R.drawable.cherry);
fruitList.add(cherry);
}
}



谁能告诉我哪里错了吗,谢谢了啊!
...全文
264 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
SDYYY_FAN 2016-07-29
  • 打赏
  • 举报
回复
书上原本的代码是酱紫的: public View getView(int position, View convertView, ViewGroup parent) { Fruit fruit = getItem(position); View view; ViewHolder viewHolder; if (convertView == null) { view = LayoutInflater.from(getContext()).inflate(resourceId, null); viewHolder = new ViewHolder(); viewHolder.fruitImage = (ImageView) view.findViewById(R.id.fruit_image); viewHolder.fruitName = (TextView) view.findViewById(R.id.fruit_name); view.setTag(viewHolder); } else { view = convertView; viewHolder = (ViewHolder) view.getTag(); } viewHolder.fruitImage.setImageResource(fruit.getImageId()); viewHolder.fruitName.setText(fruit.getName()); return view; } class ViewHolder { ImageView fruitImage; TextView fruitName; }
秃头地中海 2016-07-29
  • 打赏
  • 举报
回复
private Context mContext;
	public FruitAdapter(Context context, int resource, List<Fruit> objects) {
		super(context, resource, objects);     
		this.resourceId=resource;  
		this.mContext = context;		
	}
还有就是3楼所说的一并改掉
「已注销」 2016-07-29
  • 打赏
  • 举报
回复
没有上下文context
  • 打赏
  • 举报
回复
View view = LayoutInflater.from(getContext()).inflate(resourceId, null);
改成

convertView = LayoutInflater.from(getContext()).inflate(resourceId, null);


最后return的也是 convertView
易百洋 2016-07-06
  • 打赏
  • 举报
回复

logcat显示的这个错误
易百洋 2016-07-06
  • 打赏
  • 举报
回复
引用 3 楼 qq_34653481 的回复:
View view = LayoutInflater.from(getContext()).inflate(resourceId, null);
改成

convertView = LayoutInflater.from(getContext()).inflate(resourceId, null);


最后return的也是 convertView


还是不行啊、
passself 2016-07-06
  • 打赏
  • 举报
回复
getContext() 为null , new adapter 的时候需要从外部传进来
xing880307 2016-07-05
  • 打赏
  • 举报
回复
用reclyclerView吧

80,490

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧