如何使用来自另一个活动的自定义适配器更新列表视图?
Posted
技术标签:
【中文标题】如何使用来自另一个活动的自定义适配器更新列表视图?【英文标题】:How do I update a listview with a custom adapter from another activity? 【发布时间】:2015-01-03 18:03:48 【问题描述】:我正在制作一个消息传递应用程序。到目前为止,一切都很好,但是我还没有弄清楚如何在新消息从另一个活动到达时更新列表视图。我希望这样,如果有消息到达,如果用户在消息屏幕上,它将更新列表视图以供他们显示新消息。
事实上,用户必须重新打开应用程序才能查看新消息。我现在想弄清楚如何自动刷新或更新当前的列表视图。
我的MessageAdapter如下:
public class MessageAdapter extends BaseAdapter
// This is the custom adapter for displaying the labdetails in listview of a
// dialog inside the activity
String picsDownloadFolder = Environment.getExternalStorageDirectory() + "/"
+ "Congress" + "/", imageLinkBuilder;
private Activity activity;
private LayoutInflater inflater = null;
private List<MessageModel> data;
int myPosition;
int totalNumAttackers = 0, totalNumDamage = 0;
public Boolean bb[];
int element;
DbUtils dbutil;
File LocImageFile;
WindowManager.LayoutParams WMLP;
List<UserModel> notificationList = new ArrayList<UserModel>();
public MessageAdapter(Activity a, List<MessageModel> notifyList)
activity = a;
inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
dbutil = new DbUtils();
// notificationList = notifyList;
data = notifyList;
@Override
public int getCount()
// TODO Auto-generated method stub
return data.size();
@Override
public Object getItem(int position)
// TODO Auto-generated method stub
return position;
@Override
public long getItemId(int position)
// TODO Auto-generated method stub
return position;
@Override
public View getView(final int position, View convertView, ViewGroup parent)
// TODO Auto-generated method stub
ViewHolder viewHolder;
MessageModel array = data.get(position);
if (convertView == null)
viewHolder = new ViewHolder();
convertView = inflater.inflate(R.layout.message_item, null);
viewHolder.left =(LinearLayout)convertView.findViewById(R.id.left);
viewHolder.right =(LinearLayout)convertView.findViewById(R.id.right);
viewHolder.txt1 = (TextView) convertView
.findViewById(R.id.textView1);
viewHolder.txt3 = (TextView) convertView
.findViewById(R.id.textView3);
viewHolder.txt2 = (TextView) convertView
.findViewById(R.id.textView2);
viewHolder.txt4 = (TextView) convertView
.findViewById(R.id.textView4);
convertView.setTag(viewHolder);
else
// prevent recycling of views
viewHolder = (ViewHolder) convertView.getTag();
if (array.gettype().equals("received"))
viewHolder.right.setVisibility(View.INVISIBLE);
viewHolder.left.setVisibility(View.VISIBLE);
viewHolder.txt1.setText(array.getmsg());
viewHolder.txt3.setText(array.getdate());
if (array.gettype().equals("sent"))
viewHolder.left.setVisibility(View.INVISIBLE);
viewHolder.right.setVisibility(View.VISIBLE);
viewHolder.txt2.setText(array.getmsg());
viewHolder.txt4.setText(array.getdate());
return convertView;
// view holder class
public static class ViewHolder
LinearLayout left,right;
TextView txt1,txt3,txt2,txt4;
我尝试过使用:
mListView.setAdapter(mAdapter);
但它一直强制关闭应用程序。
【问题讨论】:
【参考方案1】:打电话
mAdapter.notifyDataSetChanged();
【讨论】:
以上是关于如何使用来自另一个活动的自定义适配器更新列表视图?的主要内容,如果未能解决你的问题,请参考以下文章
如何将最新数据附加到android中的自定义基本适配器列表视图?
如何使用 glide* 使用数组列表中的自定义适配器将图像设置为列表视图