gpt4 book ai didi

java - 多listView android

转载 作者:行者123 更新时间:2023-11-29 20:31:59 25 4
gpt4 key购买 nike

我尝试创建多个 ListView,但我有 java.lang.NullPointerException。好像还行。

主要内容:

for(int i = 0; i < 10; i++) {
ActivityObject ao = new ActivityObject();
if(i%2 == 0) {
ao.setType("0");
ao.setTitle("Im null");
} else {
ao.setType("1");
ao.setTitle("Im one");
}

dataActivity.add(ao);
}

ActivityAdapter adapter = new ActivityAdapter(getApplicationContext(), dataActivity);
listActivity.setAdapter(adapter);

super 适配器

public class ActivityAdapter extends ArrayAdapter {

private Context context;
private ArrayList<ActivityObject> object;
LayoutInflater vi;

public ActivityAdapter(Context context, ArrayList<ActivityObject> object) {
super(context, 0, object);

this.context = context;
this.object = object;

vi = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

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

ViewHolder holder = null;

if(convertView == null) {

if(object.get(position).equals("0")) {
convertView = vi.inflate(R.layout.p_activity_detected, null);
holder.textActivity = (TextView) convertView.findViewById(R.id.detected_text);
}

if(object.get(position).equals("1")) {
convertView = vi.inflate(R.layout.p_activity_people, null);
holder.textPeople = (TextView) convertView.findViewById(R.id.people_text);
}

holder = new ViewHolder();
convertView.setTag(holder);

} else {
holder = (ViewHolder) convertView.getTag();
}

//set text
if(object.get(position).getType().equals("0")) {
holder.textActivity.setText(object.get(position).getTitle());
}

if(object.get(position).getType().equals("1")) {
holder.textPeople.setText(object.get(position).getTitle());
}

return convertView;
}

static class ViewHolder {
public TextView textActivity;
public TextView textPeople;
}
}

错误

FATAL EXCEPTION: main
java.lang.NullPointerException
at adapters.ActivityAdapter.getView(ActivityAdapter.java:54)

这是线convertView.setTag(holder);

最佳答案

用这些代码替换你的适配器

public class ActivityAdapter extends ArrayAdapter {

private Context context;
private ArrayList<ActivityObject> object;
LayoutInflater vi;

ViewHolder holder;

public ActivityAdapter(Context context, ArrayList<ActivityObject> object) {
super(context, 0, object);

this.context = context;
this.object = object;

vi = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

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

holder = new ViewHolder();;

if(convertView == null) {

if(object.get(position).equals("0")) {
convertView = vi.inflate(R.layout.p_activity_detected, null);
holder.textActivity = (TextView) convertView.findViewById(R.id.detected_text);
}

if(object.get(position).equals("1")) {
convertView = vi.inflate(R.layout.p_activity_people, null);
holder.textPeople = (TextView) convertView.findViewById(R.id.people_text);
}


convertView.setTag(holder);

} else {
holder = (ViewHolder) convertView.getTag();
}

//set text
if(object.get(position).getType().equals("0")) {
holder.textActivity.setText(object.get(position).getTitle());
}

if(object.get(position).getType().equals("1")) {
holder.textPeople.setText(object.get(position).getTitle());
}

return convertView;
}

static class ViewHolder {
public TextView textActivity;
public TextView textPeople;
}
}

关于java - 多listView android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31804193/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com