自定义arrayadapter组合了在同一活动的不同片段中使用的列表

评估

我有3个片段的MainActivity活动。这三个片段使用相同的arrayadapter类MessageListAdapter。当我使用MessageListAdapter使用不同的ArrayList在片段中填充listView时,它将所有这些ArrayList合并并显示在每个片段中。我希望每个片段都显示其自己的列表。

MessageListAdapter:

public class MessageListAdapter extends ArrayAdapter<Message>{

Context context;

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

//...
}

HomeFragment:

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;
}

}

ProfileFragment:

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;
}
}

也有第3个片段,它将使用相同的MessageListAdapter,但是由于遇到此问题,我尚未实现它。

我制作了屏幕截图以便于理解:带有橙色图片的项目仅应在ProfileFragment中显示,带有蓝色图片的项目应仅在HomeFragment中显示

碎片

评估

问题在于在Message类内部使用静态ArrayList。addPersonalMessage将Message对象添加到personalMessages列表中,而addContactMessage将Message对象添加到contactMessages列表中。在我根据消息的类型构建所有消息并将它们分别放入列表之后,出于某种原因,应用程序将这两个列表合并在一起。这就是为什么我最终在两个列表视图中都包含类似内容的原因。通过使用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);
}

}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何在自定义对象的android中使用ArrayAdapter

来自分类Dev

从活动中调用自定义片段

来自分类Dev

在不同的活动中使用相同的 ArrayAdapter?

来自分类Dev

在TextView的片段中使用自定义字体

来自分类Dev

Android在片段中使用AlertDialog和自定义布局

来自分类Dev

如何使用roboguice 2.0将自定义arrayadapter插入片段

来自分类Dev

WTL如何在同一窗口中使用2个自定义绘制CListViewCtr

来自分类Dev

WTL如何在同一窗口中使用2个自定义绘制CListViewCtr

来自分类Dev

如何在同一个 RestTemplate 中使用自定义 ResponseErrorHandler 和 ClientHttpRequestFactory?

来自分类Dev

GridView通过使用自定义ArrayAdapter

来自分类Dev

GridView通过使用自定义ArrayAdapter

来自分类Dev

如何将自定义数组列表从片段传递到活动

来自分类Dev

在同一活动中使用相同的textView和不同的文本

来自分类Dev

过滤自定义ArrayAdapter或在活动中实施搜索

来自分类Dev

在终端中使用的同一命令无法与Thunar自定义操作一起使用

来自分类Dev

使用导航组件将自定义类型对象从片段传递到活动

来自分类Dev

SwiftUI自定义列表无法在ForEach中使用绑定

来自分类Dev

C#在列表中使用自定义类

来自分类Dev

每个片段的Dagger 2自定义范围(或活动等)

来自分类Dev

带有活动和片段的自定义导航抽屉

来自分类Dev

带有活动和片段的自定义导航抽屉

来自分类Dev

没有列表类型参数的自定义ArrayAdapter

来自分类Dev

使用工厂模式在同一张表中加载不同的自定义单元格

来自分类Dev

如何在自定义列表视图中对项目单击启动不同的活动?

来自分类Dev

Android:用于不同自定义对象的自定义ArrayAdapter

来自分类Dev

在同一活动中引用来自不同片段的视图

来自分类Dev

通过自定义列表视图从活动A转到活动B

来自分类Dev

扩展自定义ArrayAdapter

来自分类Dev

在Android的自定义列表视图中从一个片段移动到另一个片段

Related 相关文章

  1. 1

    如何在自定义对象的android中使用ArrayAdapter

  2. 2

    从活动中调用自定义片段

  3. 3

    在不同的活动中使用相同的 ArrayAdapter?

  4. 4

    在TextView的片段中使用自定义字体

  5. 5

    Android在片段中使用AlertDialog和自定义布局

  6. 6

    如何使用roboguice 2.0将自定义arrayadapter插入片段

  7. 7

    WTL如何在同一窗口中使用2个自定义绘制CListViewCtr

  8. 8

    WTL如何在同一窗口中使用2个自定义绘制CListViewCtr

  9. 9

    如何在同一个 RestTemplate 中使用自定义 ResponseErrorHandler 和 ClientHttpRequestFactory?

  10. 10

    GridView通过使用自定义ArrayAdapter

  11. 11

    GridView通过使用自定义ArrayAdapter

  12. 12

    如何将自定义数组列表从片段传递到活动

  13. 13

    在同一活动中使用相同的textView和不同的文本

  14. 14

    过滤自定义ArrayAdapter或在活动中实施搜索

  15. 15

    在终端中使用的同一命令无法与Thunar自定义操作一起使用

  16. 16

    使用导航组件将自定义类型对象从片段传递到活动

  17. 17

    SwiftUI自定义列表无法在ForEach中使用绑定

  18. 18

    C#在列表中使用自定义类

  19. 19

    每个片段的Dagger 2自定义范围(或活动等)

  20. 20

    带有活动和片段的自定义导航抽屉

  21. 21

    带有活动和片段的自定义导航抽屉

  22. 22

    没有列表类型参数的自定义ArrayAdapter

  23. 23

    使用工厂模式在同一张表中加载不同的自定义单元格

  24. 24

    如何在自定义列表视图中对项目单击启动不同的活动?

  25. 25

    Android:用于不同自定义对象的自定义ArrayAdapter

  26. 26

    在同一活动中引用来自不同片段的视图

  27. 27

    通过自定义列表视图从活动A转到活动B

  28. 28

    扩展自定义ArrayAdapter

  29. 29

    在Android的自定义列表视图中从一个片段移动到另一个片段

热门标签

归档