【问题标题】:Refresh ListView Items upon button triggered触发按钮时刷新 ListView 项
【发布时间】:2014-11-15 12:40:06
【问题描述】:

在触发按钮后尝试刷新列表视图中的项目时遇到了一些问题。这是我在创建时填充列表视图的方式:

public class EventChat extends Fragment {
Context context;
View eventChat;

String userID, eventID;

private ListView listview;
public ArrayList<EventComment> _commentlist = new ArrayList<EventComment>();
TextView txtDisplayCommentBy, txtDisplayDateTime, txtDisplayCommentDesc,
        txtEventChat;

Button btnChatSubmit;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    eventChat = inflater.inflate(R.layout.event_chat, container, false);
    context = getActivity();

    listview = (ListView) eventChat.findViewById(R.id.listview);
    txtEventChat = (TextView) eventChat.findViewById(R.id.txtEventChat);

    btnChatSubmit = (Button) eventChat.findViewById(R.id.btnChatSubmit);
    btnChatSubmit.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            onSubmitChatClicked();
        }
    });

    Intent i = getActivity().getIntent();
    _commentlist = (ArrayList<EventComment>) i
            .getSerializableExtra("eventCommentObj");
    Event eventModel = (Event) i.getSerializableExtra("eventObj");

    userID = "Gab";
    eventID = eventModel.getEventID();

    listview.setAdapter(new ListAdapter(getActivity()));

    return eventChat;
}

private class ListAdapter extends BaseAdapter {
    LayoutInflater inflater;
    ViewHolder viewHolder;

    public ListAdapter(Context context) {
        inflater = LayoutInflater.from(context);
    }

    public int getCount() {
        return _commentlist.size();
    }

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

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

    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {

            convertView = inflater.inflate(R.layout.eventchat_listview_row,
                    null);
            viewHolder = new ViewHolder();
            viewHolder.txt_dcommentBy = (TextView) convertView
                    .findViewById(R.id.txtDisplayCommentBy);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        viewHolder.txt_dcommentBy.setText(_commentlist.get(position)
                .getCommentBy().trim());
        return convertView;
    }
}

private class ViewHolder {
    TextView txt_dcommentBy;
    TextView txt_ddateTime;
    TextView txt_dcommentDesc;
}
}

当我的按钮被触发并将新记录插入数据库时​​,同时我希望刷新列表视图项:

public void onSubmitChatClicked() {
    EventComment commentModel = new EventComment();
    String currentDate = EventDateTime.getCurrentDate();
    String currentTime = EventDateTime.getCurrentTime();
    String commentDesc = String.valueOf(txtEventChat.getText());
    commentModel.setCommentBy(userID);
    commentModel.setEventID(eventID);
    commentModel.setCommentDate(currentDate);
    commentModel.setCommentTime(currentTime);
    commentModel.setCommentDesc(commentDesc);
    new CreateCommentAsyncTask(context).execute(commentModel);

    txtEventChat.setText("");

}

但是,它不会刷新。有什么想法吗?

提前致谢。

【问题讨论】:

  • 你试过adapter.notifyDataSetChanged吗?数据保存后最后点击in按钮?
  • 但是适配器数据类型是什么?
  • 您的 listview 的适配器。
  • @MysticMagic 是 listview.notifyDataSetChanged() 吗?但是有了这个,我收到错误消息
  • @MysticMagic 你好?有什么想法吗?

标签: java android listview


【解决方案1】:

这看起来很简单。

首先让您的适配器成为类成员,以便您以后可以访问它:

private ListAdapter mAdapter;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    ...
    mAdapter = new ListAdapter(getActivity();
    listview.setAdapter(mAdapter);
    ...
}

然后,当您提交项目时,也将其添加到您的 ArrayList 并更新您的适配器:

public void onSubmitChatClicked() {
    EventComment commentModel = new EventComment();
    String currentDate = EventDateTime.getCurrentDate();
    String currentTime = EventDateTime.getCurrentTime();
    String commentDesc = String.valueOf(txtEventChat.getText());
    commentModel.setCommentBy(userID);
    commentModel.setEventID(eventID);
    commentModel.setCommentDate(currentDate);
    commentModel.setCommentTime(currentTime);
    commentModel.setCommentDesc(commentDesc);

    // Add the new element to your DB
    new CreateCommentAsyncTask(context).execute(commentModel);
    // Add the new element to your current ArrayList
    _commentlist.add(commentModel)
    // Update theListView, by updating the adapter
    mAdapter.notifyDataSetChanged();

    txtEventChat.setText("");
}

编辑:

稍微解释一下:

当您的片段被创建时,您会收到一个EventComment 项目的数组。我想这些是您数据库中的元素。但是,当您更新数据库时,您的 ArrayList 不会得到更新,除非您重新加载整个片段。这就是您将项目手动添加到数据库和列表的原因,并且使用notifyDataSetChanged,您可以强制适配器更新 ListView。

【讨论】:

  • 非常感谢!有用。但是你能帮我澄清一下它是否以这种方式工作:当按钮被触发时,它会将新记录添加到静态 _commentlist 并且适配器的 notifyDataSetChanged 将刷新列表视图?
  • @IWasSoLost 查看我的答案更新以获得更多解释。
【解决方案2】:

你不能通过listview.setAdapter(new ListAdapter(getActivity())); 来实现这一点。你应该创建一个adapter 的实例并调用adapter.add + adapter.notifyDataSetChanged

这是一个很好的教程:

http://www.vogella.com/tutorials/AndroidListView/article.html http://www.javacodegeeks.com/2013/09/android-listview-with-adapter-example.html

【讨论】:

  • 抱歉,您介意给我看一下代码示例吗?因为我不太明白
  • @IWasSoLost Done :) 您可能需要进行一些谷歌搜索,例如“listview adapter android”,因为您需要了解适配器的工作原理
  • 那么我可以假设我必须重构所有代码来实现适配器吗?
  • @IWasSoLost 你的意思是你需要修改很多代码吗?这取决于您要如何处理listview。只是一个友好的建议,adapter 是你应该了解的android中的基本内容......
  • 您是否介意通过为我提供一些解决方案来指导我的情况?因为我试图改变,一切都变红了:(
猜你喜欢
  • 1970-01-01
  • 2013-06-28
  • 2019-05-26
  • 2011-11-23
  • 1970-01-01
  • 2012-03-12
  • 1970-01-01
  • 2010-11-12
  • 2016-07-16
相关资源
最近更新 更多