自定义ListView与图像

时间:2014-10-14 19:13:18

标签: android listview

这是我的MainActivity。

public class MainActivity extends Activity implements OnItemClickListener {


public static final String[] titles = new String[] { "Strawberry",
        "Banana", "Orange", "Mixed" };

public static final String[] descriptions = new String[] {
        "It is an aggregate accessory fruit",
        "It is the largest herbaceous flowering plant", "Citrus Fruit",
        "Mixed Fruits" };

public static final int[] image = { R.drawable.a_1, R.drawable.a_2,
        R.drawable.a_3, R.drawable.a_4 };

public ListView listView;
List<RowItem> rowItems;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    rowItems = new ArrayList<RowItem>();
    for (int i = 0; i < titles.length; i++) {
        RowItem item = new RowItem(image[i], titles[i], descriptions[i]);
        rowItems.add(item);
    }

    listView = (ListView) findViewById(R.id.list);
    customeBaseAdapter adapter = new customeBaseAdapter(this, rowItems);
    listView.setAdapter(adapter);
    listView.setOnItemClickListener(this);
}

@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
        long id) {
    // TODO Auto-generated method stub
}
}

这是我的CustomAdapter

public class customeBaseAdapter extends BaseAdapter {


Context context;
List<RowItem> rowItems;

public customeBaseAdapter(Context context, List<RowItem> items) {
    // TODO Auto-generated constructor stub

    this.context = context;
    this.rowItems = items;
}

@Override
public int getCount() {
    // TODO Auto-generated method stub
    return 4;
}

@Override
public Object getItem(int position) {
    // TODO Auto-generated method stub
    return rowItems.get(position);
}

@Override
public long getItemId(int position) {
    // TODO Auto-generated method stub
    return 0;
}

@Override
public View getView(int position, View view, ViewGroup group) {
    // TODO Auto-generated method stub
    LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    if (view == null) {
        view = inflater.inflate(R.layout.list_item, null);
    }

    ImageView imageView = (ImageView) view.findViewById(R.id.image);
    TextView titleTextView = (TextView) view.findViewById(R.id.title);
    TextView desc = (TextView) view.findViewById(R.id.desc);
    imageView.setImageResource(rowItems.get(position).getImageId());
    titleTextView.setText(rowItems.get(position).getTitle());
    desc.setText(rowItems.get(position).getDesc());
    return view;
}
}

这是我的RowItem类

package com.example.customlistview;

import android.R.integer;

public class RowItem {

private int imageId;
private String title;
private String desc;

public RowItem(int imageId, String title, String desc) {
    this.imageId = imageId;
    this.title = title;
    this.desc = desc;
}

public int getImageId() {
    return imageId;
}

public void setImageId(int imageId) {
    this.imageId = imageId;
}

public String getDesc() {
    return desc;
}

public void setDesc(String desc) {
    this.desc = desc;
}

public String getTitle() {
    return title;
}

public void setTitle(String title) {
    this.title = title;
}

@Override
public String toString() {
    return title + "\n" + desc;
}
}

Logcat错误。

10-15 01:04:05.135: E/AndroidRuntime(9901): FATAL EXCEPTION: main
10-15 01:04:05.135: E/AndroidRuntime(9901): java.lang.NullPointerException
10-15 01:04:05.135: E/AndroidRuntime(9901):     at com.example.customlistview.customeBaseAdapter.getView(customeBaseAdapter.java:56)
10-15 01:04:05.135: E/AndroidRuntime(9901):     at android.widget.AbsListView.obtainView(AbsListView.java:2350)
10-15 01:04:05.135: E/AndroidRuntime(9901):     at android.widget.ListView.measureHeightOfChildren(ListView.java:1409)
10-15 01:04:05.135: E/AndroidRuntime(9901):     at android.widget.ListView.onMeasure(ListView.java:1273)
10-15 01:04:05.135: E/AndroidRuntime(9901):     at android.view.View.measure(View.java:15286)
10-15 01:04:05.135: E/AndroidRuntime(9901):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4832)
10-15 01:04:05.135: E/AndroidRuntime(9901):     at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1390)
10-15 01:04:05.135: E/AndroidRuntime(9901):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:681)
10-15 01:04:05.135: E/AndroidRuntime(9901):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:574)
10-15 01:04:05.135: E/AndroidRuntime(9901):     at android.view.View.measure(View.java:15286)
10-15 01:04:05.135: E/AndroidRuntime(9901):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4832)
10-15 01:04:05.135: E/AndroidRuntime(9901):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
10-15 01:04:05.135: E/AndroidRuntime(9901):     at android.view.View.measure(View.java:15286)

在我的自定义适配器类中发生了什么,我不知道。 我尝试了很多,但不知道如何解决它。

3 个答案:

答案 0 :(得分:0)

我不在家测试代码,但是如果你想尝试另一个应用程序做得很好

https://github.com/thest1/LazyList

从这里开始我

Lazy load of images in ListView

答案 1 :(得分:0)

正如错误本身所解释的那样,您的适配器的 getView 存在问题。我认为您需要执行以下更改:

if (view == null) {
    LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    view = inflater.inflate(R.layout.list_item, group, false);
}

希望它有所帮助。

答案 2 :(得分:0)

更改getView,如我在您的代码中所示,并添加ViewHolder类。

private class ViewHolder { ImageView imageView; TextView txtTitle; TextView txtDesc; }

@Override
public View getView(int position, View convertView, ViewGroup group) {
    // TODO Auto-generated method stub
    ViewHolder holder = null;
    LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    if (convertView == null) {
        convertView = inflater.inflate(R.layout.list_item, null);
        holder = new ViewHolder();
        holder.txtDesc = (TextView) convertView.findViewById(R.id.desc);
        holder.txtTitle = (TextView) convertView.findViewById(R.id.title);
        holder.imageView = (ImageView) convertView.findViewById(R.id.icon);
        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }

    RowItem rowItem = (RowItem) getItem(position);
    holder.txtDesc.setText(rowItem.getDesc());
    holder.txtTitle.setText(rowItem.getTitle());
    holder.imageView.setImageResource(rowItem.getImageId());

    return convertView; 
}

`

您正在访问listview行而不添加它。

Android Official Doc for ListView

和完整代码Read Here