From c92587cf267dfee3115e5394b94ec7e1c508439f Mon Sep 17 00:00:00 2001 From: Pierre Date: Sun, 10 Dec 2017 16:56:50 +0100 Subject: [PATCH] Conversations name displayed in the "Conversation" tab. --- .../ConversationsListAdapter.java | 70 +++++++++++++++++++ .../fragments/ConversationsListFragment.java | 26 ++++++- .../fragment_conversationslist_item.xml | 14 ++++ 3 files changed, 107 insertions(+), 3 deletions(-) create mode 100644 app/src/main/java/org/communiquons/android/comunic/client/data/conversations/ConversationsListAdapter.java create mode 100644 app/src/main/res/layout/fragment_conversationslist_item.xml diff --git a/app/src/main/java/org/communiquons/android/comunic/client/data/conversations/ConversationsListAdapter.java b/app/src/main/java/org/communiquons/android/comunic/client/data/conversations/ConversationsListAdapter.java new file mode 100644 index 0000000..1ac70e3 --- /dev/null +++ b/app/src/main/java/org/communiquons/android/comunic/client/data/conversations/ConversationsListAdapter.java @@ -0,0 +1,70 @@ +package org.communiquons.android.comunic.client.data.conversations; + +import android.content.Context; +import android.support.annotation.NonNull; +import android.support.annotation.Nullable; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.ArrayAdapter; +import android.widget.TextView; + +import org.communiquons.android.comunic.client.R; + +import java.util.ArrayList; +import java.util.zip.Inflater; + +import static android.R.id.list; + +/** + * Conversations adapter + * + * Handles the rendering of the conversations in a {@link android.widget.ListView} + * + * @author Pierre HUBERT + * Created by pierre on 12/10/17. + */ + +public class ConversationsListAdapter extends ArrayAdapter { + + /** + * Class constructor + * + * @param context The context of the application + * @param list The list of conversations to display + */ + public ConversationsListAdapter(Context context, ArrayList list){ + super(context, 0, list); + } + + + /** + * Handle the rendering of the views + * + * @param position The position of the view to inflate in the list + * @param convertView The view to convert / null if it is a new view + * @param parent The parent + * @return The converted view + */ + @NonNull + @Override + public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) { + + //Check if it is the first time the view is used + if(convertView == null){ + convertView = LayoutInflater.from(getContext()) + .inflate(R.layout.fragment_conversationslist_item, parent, false); + } + + //Get information about the conversation + ConversationsInfo infos = getItem(position); + + //Set the name of the conversation + TextView conversationName = convertView + .findViewById(R.id.fragment_conversationslist_item_name); + conversationName.setText(infos.getDisplayName()); + + return convertView; + + } +} diff --git a/app/src/main/java/org/communiquons/android/comunic/client/fragments/ConversationsListFragment.java b/app/src/main/java/org/communiquons/android/comunic/client/fragments/ConversationsListFragment.java index e3ca998..13fb500 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/fragments/ConversationsListFragment.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/fragments/ConversationsListFragment.java @@ -9,6 +9,7 @@ import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; +import android.widget.ListView; import android.widget.Toast; import org.communiquons.android.comunic.client.R; @@ -16,6 +17,7 @@ import org.communiquons.android.comunic.client.data.DatabaseHelper; import org.communiquons.android.comunic.client.data.UsersInfo.GetUsersHelper; import org.communiquons.android.comunic.client.data.UsersInfo.UserInfo; import org.communiquons.android.comunic.client.data.conversations.ConversationsInfo; +import org.communiquons.android.comunic.client.data.conversations.ConversationsListAdapter; import org.communiquons.android.comunic.client.data.conversations.ConversationsListHelper; import java.util.ArrayList; @@ -51,6 +53,16 @@ public class ConversationsListFragment extends Fragment { */ private ConversationsListHelper conversationsListHelper; + /** + * Conversations ListView + */ + private ListView conversationsListView; + + /** + * Conversation list adapter + */ + private ConversationsListAdapter conversationsListAdapter; + @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) { @@ -70,6 +82,9 @@ public class ConversationsListFragment extends Fragment { //Create the conversation list helper conversationsListHelper = new ConversationsListHelper(getActivity()); + //Get the conversation target list view + conversationsListView = view.findViewById(R.id.fragment_conversationslist_list); + //Get the list of conversations new AsyncTask>(){ @Override @@ -188,8 +203,13 @@ public class ConversationsListFragment extends Fragment { return; } - for(ConversationsInfo info : list){ - Log.v(TAG, "Conversation " + info.getID() + " : " + info.getDisplayName()); - } + //Save the list + convList = list; + + //Create the adapter + conversationsListAdapter = new ConversationsListAdapter(getActivity(), convList); + + //Attach it to the view + conversationsListView.setAdapter(conversationsListAdapter); } } diff --git a/app/src/main/res/layout/fragment_conversationslist_item.xml b/app/src/main/res/layout/fragment_conversationslist_item.xml new file mode 100644 index 0000000..48f7f57 --- /dev/null +++ b/app/src/main/res/layout/fragment_conversationslist_item.xml @@ -0,0 +1,14 @@ + + + + + + \ No newline at end of file