为什么在适配器中设置空文本?

时间:2016-10-13 19:54:43

标签: android adapter

我对这个问题感到困惑!

我在适配器中将文本设置为TextView,但是当生成我的列表视图时,TextView仍然为null。

我的问题是关于MyAdapter,因为当我手动输入文字时,它的效果非常好。

请帮帮我。

这是我的“适配器”:

package ir.sarashpazp.peymanehsarashpaz;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.util.List;

public class MainAdapter extends BaseAdapter {
private Activity activity;
private LayoutInflater inflater;
private List<items> feedItems;
private items item;


public MainAdapter(Activity activity, List<items> feedItems) {
    this.activity = activity;
    this.feedItems = feedItems;
}

@Override
public int getCount() {
    return feedItems.size();
}

@Override
public Object getItem(int location) {
    return feedItems.get(location);
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    item = new items();
    if (inflater == null)
        inflater = (LayoutInflater) activity
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    if (convertView == null)
        convertView = inflater.inflate(R.layout.eachdastoor, null);


    TextView name = (TextView) convertView.findViewById(R.id.name);

    name.setText(item.getName());

    return convertView;
}

}

这是我的“项目”类:

public class items {
private int id;
private String name, status, image, profilePic, timeStamp, url;

public items() {
}

public items(int id, String name, String image, String status,
                String profilePic, String timeStamp, String url) {
    super();
    this.id = id;
    this.name = name;
    this.image = image;
    this.status = status;
    this.profilePic = profilePic;
    this.timeStamp = timeStamp;
    this.url = url;
}

public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}


}

1 个答案:

答案 0 :(得分:0)

您始终访问新对象实例而不是数组项。请替换

 item = new items(); 

 item = feedItems.get(position);