我的应用程序中有2个标签。一个是联系人,第二个是收藏夹。
我为每个标签使用2个片段和2个列表视图。
在我的列表视图中,我想添加选项
的名称和星号用户可以单击联系人片段中的星标,并将用户添加到收藏夹列表视图片段。
我正在使用自定义适配器。而且我不知道我该怎么做。
这是我的自定义适配器:
public class ContactsAdapter extends ArrayAdapter<ListViewItem> {
public ContactsAdapter(Context context, List<ListViewItem> items) {
super(context, R.layout.contacts_list_item, items);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
if(convertView == null) {
// inflate the GridView item layout
LayoutInflater inflater = LayoutInflater.from(getContext());
convertView = inflater.inflate(R.layout.contacts_list_item, parent, false);
// initialize the view holder
viewHolder = new ViewHolder();
viewHolder.ivIcon = (ImageView) convertView.findViewById(R.id.ivIcon);
viewHolder.ivStarIcon = (ImageView)convertView.findViewById(R.id.FavoriteStarImageView);
viewHolder.tvTitle = (TextView) convertView.findViewById(R.id.tvTitle);
viewHolder.tvDescription = (TextView) convertView.findViewById(R.id.tvDescription);
convertView.setTag(viewHolder);
} else {
// recycle the already inflated view
viewHolder = (ViewHolder) convertView.getTag();
}
// update the item view
ListViewItem item = getItem(position);
if(item.icon != null)
viewHolder.ivIcon.setImageURI(Uri.parse(item.icon));
else
viewHolder.ivIcon.setImageResource(R.drawable.ic_stub);
viewHolder.tvTitle.setText(item.title);
viewHolder.tvDescription.setText(item.description);
if(item.favoriteIcon)
viewHolder.ivStarIcon.setImageResource(R.drawable.ic_action_important);
else
viewHolder.ivStarIcon.setImageResource(R.drawable.ic_action_not_important);
return convertView;
}
private static class ViewHolder {
ImageView ivIcon;
ImageView ivStarIcon;
TextView tvTitle;
TextView tvDescription;
}
}
class ListViewItem {
public final String id;
public final String icon; // the drawable for the ListView item ImageView
public final String title; // the text for the ListView item title
public final String description; // the text for the ListView item description
public final Boolean favoriteIcon;
public ListViewItem(String id,String icon, String title, String description, Boolean favoriteIcon) {
this.id = id;
this.icon = icon;
this.title = title;
this.description = description;
this.favoriteIcon = favoriteIcon;
}
}
谢谢
答案 0 :(得分:1)
zeds
然后,您可以找到获取其他片段实例的方法,并在onclick方法中对其进行更改。不要忘记在修改列表内容后调用notifydatachange
答案 1 :(得分:0)
出现一个包含所有数据的数组(示例代码中的ListViewItem),所以只需将新项放入此数组并通过调用
通知适配器数据集已更改notifyDataSetChanged()
答案 2 :(得分:0)
创建一个回调来更新当前片段中的另一个片段。
interface updatecallback
{
public void updatelist(User user); //user is the parameter clicked in contacts fragment
}
在联系人片段的父活动中实现上述接口。
class parentActivity extends Activity implements updatecallback {
//instantiating both fragments with the updatecallback instance
Fragment f=new FavouritesFragment(favourite_list,this);
Fragment f=new FavouritesFragment(contact_list,this);
// other code
public void updatelist(User user) {
// update the favourites activity by adding the user to the userlist
Fragment f=new FavouritesFragment(updated_user_list,this);
}
contactfragment
public class ContactFragment extends Fragment
{
private ,updatecallback updatecall;
public ContactFragment(list,updatecallback updatecall)
{
this.updatecall=updatecall;
}
//while pressing star for a particular contact call in getview
ContactAdapter adapter=new ContactAdapter(list,updatecallback updatecall);
}
}
public class ContactAdapter extends BaseAdapter
{
private ,updatecallback updatecall;
public ContactAdapter (list,updatecallback updatecall)
{
this.updatecall=updatecall;
}
//while pressing star for a particular contact call in getview
updatecall.updatecallback(list.get(position));
}
}
这个想法是使用回调
将事件填充到另一个片段