android - 自定义 arrayadapter 组合了在同一 Activity 的不同 fragment 中使用的列表

标签 android android-fragments android-arrayadapter

我有包含 3 个 fragment 的 MainActivity Activity 。这 3 个 fragment 使用相同的数组适配器类 MessageListAdapter。当我使用 MessageListAdapter 使用不同的 ArrayLists 在我的 fragment 中填充 listView 时,它结合了所有这些 ArrayLists 并显示在每个 fragment 中。我希望每个 fragment 都显示自己的列表。

消息列表适配器:

public class MessageListAdapter extends ArrayAdapter<Message>{

Context context;

public MessageListAdapter(Context c, int resourceId, ArrayList<Message> list) {
    super(c, resourceId, list);
    this.context = c;
}

//...
}

主页 fragment :

public class HomeFragment extends Fragment {
View view;
ListView listView1;
ArrayList<Message> contactMessages;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    view = inflater.inflate(R.layout.home_layout, container, false);
    TextView welcomeMessage = (TextView) view.findViewById(R.id.welcomeMessage);
    Account acc = new Account();
    welcomeMessage.setText("Welcome " + acc.getName() + "!");
    contactMessages = new Message().getContactMessages();
    listView1 = (ListView) view.findViewById(R.id.homeList);
    MessageListAdapter adapter = new MessageListAdapter(this.getActivity(), R.layout.activity_message, contactMessages);
    listView1.setAdapter(adapter);
    listView1.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
            // TODO Auto-generated method stub
            
        }
        
    });
    return view;
}

}

个人资料 fragment :

public class ProfileFragment extends Fragment implements View.OnClickListener, OnItemClickListener {
View view;
Intent intent;
ListView listView2;
ArrayList<Message> personalMessages;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    view = inflater.inflate(R.layout.profile_layout, container, false);
    Button button = (Button) view.findViewById(R.id.addMessage);
    button.setOnClickListener(this);
    Button addFriendButton = (Button) view.findViewById(R.id.addFriend);
    addFriendButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            intent = new Intent(getActivity(), AddFriendActivity.class);
            startActivity(intent);
        }
        
    });
    personalMessages = new Message().getPersonalMessages();
//  Log.i("Personal messages ArrayList: ", personalMessages.toString());
    listView2 = (ListView) view.findViewById(R.id.profileList);
    MessageListAdapter adapter = new MessageListAdapter(this.getActivity(), R.layout.activity_message, personalMessages);
    listView2.setAdapter(adapter);
    listView2.setOnItemClickListener(this);
    return view;
}
}

还有第三个 fragment 将使用相同的 MessageListAdapter,但由于遇到这个问题,我还没有实现它。

为了便于理解,我做了截图: 带有橙色图片的项目应该只显示在 ProfileFragment 中,带有蓝色图片的项目应该只显示在 HomeFragment 中

Fragments

最佳答案

问题在于在 Message 类中使用静态 ArrayList。 addPersonalMessage 将 Message 对象添加到 personalMessages 列表中,addContactMessage 将 Message 对象添加到 contactMessages 列表中。在我根据消息类型构建所有消息并将它们分别放入列表后,出于某种原因,应用程序将这两个列表组合在一起。这就是为什么我最终在 fragment 的两个 ListView 中得到相似的内容。通过使用 SQLite 数据库而不是使用静态变量解决了问题。 留言:

public class Message {

private String author;
private String messageTitle;
private Bitmap messageImage;

private static ArrayList<Message> personalMessages = new ArrayList<Message>();
private static ArrayList<Message> contactMessages = new ArrayList<Message>();

public Message() {

}
public Message(String a, String t, Bitmap b) {
    this.author = a;
    this.messageTitle = t;
    this.messageImage = b;
}

public void addPersonalMessage() {
    personalMessages.add(this);
}
public void addContactMessage() {
    contactMessages.add(this);
}

}

关于android - 自定义 arrayadapter 组合了在同一 Activity 的不同 fragment 中使用的列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29749004/

相关文章:

android - 在没有麦克风的情况下在Android中录制音频流

android - 获取 android.content.res.Resources$NotFoundException : Resource ID #0x0? 时如何确定崩溃发生的位置

java - Android:适配器内的静态变量为空

java - 如何动态添加项目到 ListView

java - 为什么 context.startActivity(intent) 没有启动 Activity 以及如何在 android 中处理异常?

android - 使用限制查询 Firebase 数据库

Android R.java 不生成

android - 使用新 fragment Android fragment 更新

Android - 新 BottomNavigationBar 中的 PageView - 防止重新加载 fragment

android - 拖放 API - OnDragListener 问题