gpt4 book ai didi

java - NotifyDataSetChanged() 不刷新列表?

转载 作者:行者123 更新时间:2023-12-01 13:37:10 28 4
gpt4 key购买 nike

我知道这个问题已被问过数千次,但由于某种原因我找不到适合我的情况的答案。

我拥有的是一个从 Web 服务获取数据并用信息填充列表的线程。然后我想按下一个按钮并调用同一线程来获取更多数据并将其添加到列表中。

但是当我调用 notifyDataSetChanged() 时,由于某种原因列表不会刷新。不过数据在适配器中...

代码如下:

    @Override
protected void onPostExecute(PropertyNotesParser result) {

this.progressDialog.dismiss();

ArrayList<PropertyNoteHistory> propertyNoteList = result.getAllTheNotes();
addNoteListItems(propertyNoteList);

Collections.sort(getNoteList());

ArrayList<String> titles = new ArrayList<String>();
ArrayList<String> subtitles = new ArrayList<String>();
DateHandler handleDate = new DateHandler();
DataResolve convert = new DataResolve();

for(Iterator<PropertyNoteHistory> i = getNoteList().iterator(); i.hasNext(); ) {
PropertyNoteHistory item = i.next();

PropertyNoteHistory.Note note = item.getNotes();
PropertyNoteHistory.Jobs jobs = item.getJobs();

// Default value is office in case the xml does not have the tag "job" associated with "note".
String service = "Office";

if(jobs != null){
service = convert.getServiceName(jobs.getServiceID());
}

titles.add(note.getTitle() + " (" + service + ")");
subtitles.add(handleDate.formatDate(note.getDate(), "dd MMM yyyy") + " Ref: " + note.getJobID());
}

if(getConnectionCount() == 0){
adapter = new SimpleListAdapter(getActivity(), titles, subtitles);
lv.setAdapter(adapter);
}

else {
adapter.addItem(titles, subtitles);

}

和我的适配器:

    public class SimpleListAdapter extends BaseAdapter {
private int count = 0;
private static LayoutInflater inflater = null;
private ArrayList<String> titles = new ArrayList<String>();
private ArrayList<String> subtitles = new ArrayList<String>();
private ArrayList<Integer> imageResource = new ArrayList<Integer>();
private boolean hasImage = false;

public SimpleListAdapter(Activity activity, ArrayList<String> titles,
ArrayList<String> subtitles, ArrayList<Integer> imageResource) {
count = titles.size();
inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.titles = titles;
this.subtitles = subtitles;
this.imageResource = imageResource;
this.hasImage = true;
}

/**Constructor that creates an adapter with only a title and subtitle.
* @param activity The context.
* @param titles ArrayList with the titles of each list option.
* @param subtitles ArrayList with the subtitles of each list option. */
public SimpleListAdapter(Activity activity, ArrayList<String> titles,
ArrayList<String> subtitles) {
count = titles.size();
inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.titles = titles;
this.subtitles = subtitles;
this.hasImage = false;
}

public void addItem(ArrayList<String> title, ArrayList<String> subtitle){
this.titles.addAll(title);
this.subtitles.addAll(subtitle);
notifyDataSetChanged();
}

@Override
public int getCount() {
return count;
}

@Override
public Object getItem(int position) {
return position;
}

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

@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if(v == null)
v = inflater.inflate(R.layout.layout_simple_list, null);
final ImageView icon = (ImageView) v.findViewById(R.id.icon);
final TextView title = (TextView) v.findViewById(R.id.title);
final TextView subtitle = (TextView) v.findViewById(R.id.subtitle);

title.setText(titles.get(position));
subtitle.setText(subtitles.get(position));
if (hasImage) {
icon.setImageResource(imageResource.get(position));
}

else {
icon.setVisibility(ImageView.GONE);
}

return v;
}

}

最佳答案

您还需要在 addItem 中更新您的 count 变量,以便在调用 notifyDataSetChanged 时创建所有行

关于java - NotifyDataSetChanged() 不刷新列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21182882/

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