Android: Dynamically Generated Views added but not shown - android

I have a UI component with this format:
<RelativeLayout
android:id="#+id/item_lists"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="1"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin">
<TextView
android:id="#+id/first_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="visible"
android:text="First List"/>
<ListView
android:id="#+id/list_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/first_list"
android:divider="#android:color/transparent"
android:dividerHeight="10dp" >
</ListView>
</RelativeLayout>
The outer RelativeLayout is the wrapper which I plan to dynamically generate views and inject into, the logic to dynamically add views (a text title and another listview):
private void populateLists() {
View pre = firstList; // this points to the list view "list_view"
if(!titleMap.isEmpty()) { // titleMap is a hashmap that maps titles to lists
Set<String> titles = getTitles();
for(String title : titles){
List<Item> li = titleMap.get(title);
TextView tag = generateTitleTextView(title, pre);
ListView lv = generateListView(li, tag);
sceneLists.addView(tag);
sceneLists.addView(lv);
pre = lv;
}
}
}
generateTitleTextView(String title, View pre) and generateListView(List<Item> list, View pre) are two methods that generate views and place the generated view under View component pre by using
layoutParams.addRule(RelativeLayout.BELOW, pre.getId());
The code can run without exceptions, but the generated views are not visible on the page, I wonder what might be the issue? Thanks in advance!
Edit: The code for generateTitleTextView, generateListView is similar only difference is it generates a ListView
private TextView generateTitleTextView(String title, View pre) {
TextView tag = new TextView(this);
tag.setId(generateRandomViewId());// generates a random int as Id
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.MATCH_PARENT);
layoutParams.addRule(RelativeLayout.BELOW, pre.getId());
tag.setLayoutParams(layoutParams);
tag.setText(title);
return tag;
}

How do you generate the viewId?
You can use HierarchyViewer or turn on Show layout bounds in Developer Options to debug this.
I think you should try generate view ids with this piece of code:
public static int generateViewId() {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1) {
for (; ; ) {
final int result = sNextGeneratedId.get();
// aapt-generated IDs have the high byte nonzero; clamp to the range under that.
int newValue = result + 1;
if (newValue > 0x00FFFFFF) {
newValue = 1; // Roll over to 1, not 0.
}
if (sNextGeneratedId.compareAndSet(result, newValue)) {
return result;
}
}
} else {
return View.generateViewId();
}
}

Related

how to make thumbnail image with initials two char from name android?

I want to thumbnail initials with two word for my image view like "Peter Parker" but am able to get only one word "P"while running code how can get second word after space my code is.
holder.imgName?.text=teamData[position].userImage.substring(0,1)
You can do it functional way:
val peterParker = "Peter Parker"
val initials = peterParker
.split(' ')
.mapNotNull { it.firstOrNull()?.toString() }
.reduce { acc, s -> acc + s }
println(initials) //PP
This would cover cases when a person's name consists of more than 2 words.
I have done some Trick & implemented this avatar with a Button lol ;p
create profile_bg.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid
android:color="#color/colorWhite"/>
<corners
android:radius="500dp"/>
</shape>
then main_activity.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#4300313A"
tools:context=".MainActivity">
<Button
android:onClick="clicked"
android:id="#+id/avatar"
android:clickable="false"
android:focusable="false"
android:textColor="#color/colorPrimary"
android:textSize="65sp"
android:focusableInTouchMode="false"
android:layout_width="150dp"
android:layout_height="150dp"
android:background="#drawable/profile_bg"
android:layout_centerHorizontal="true"
android:layout_marginTop="50dp"/>
<EditText
android:id="#+id/edtname"
android:layout_below="#+id/avatar"
android:layout_centerHorizontal="true"
android:layout_marginTop="50dp"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:textSize="18sp"
android:hint="Enter your name"/>
<Button
android:onClick="clicked"
android:textColor="#color/colorBackground"
android:text="Submit Name"
android:textStyle="bold"
android:focusableInTouchMode="false"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_below="#+id/edtname"
android:layout_marginTop="50dp"/>
</RelativeLayout>
then in MainActivity.java (to split the string and get the first letter of each word ~ name in if condition with stringbuilder)
public class MainActivity extends AppCompatActivity {
EditText editText;
Button button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = (EditText) findViewById(R.id.edtname);
button = (Button) findViewById(R.id.avatar);
}
public void clicked(View view) {
String str = editText.getText().toString();
String[] strArray = str.split(" ");
StringBuilder builder = new StringBuilder();
//First name
if (strArray.length > 0){
builder.append(strArray[0], 0, 1);
}
//Middle name
if (strArray.length > 1){
builder.append(strArray[1], 0, 1);
}
//Surname
if (strArray.length > 2){
builder.append(strArray[2], 0, 1);
}
button.setText(builder.toString());
}
}
Hi you can using following way
String str = "FirstWord SecondWOrd";
String[] strArray = str.split(" ");
StringBuilder builder = new StringBuilder();
if (strArray.length > 0)
builder.append(strArray[0], 0, 1);
if (strArray.length > 1)
builder.append(strArray[1], 0, 1);
Log.d("New Text" , builder.toString());
it look's like your using substring to only grab the letters from position 0 to position 1, This is P in Petter
holder.imgName?.text=teamData[position].userImage
.substring(0,1)
If you'd like to grab the words Petter Parker, you have a few options.
• IndexOf & Substring - find the position of a string and get the subtext after.
• Substring - Subtext of string based on parameters
If you plan to change the text length at any stage, you'll need to find the start of the word ( int start = yourString.indexOf("Petter"));
and end of the word ( int end = yourString.indexOf(" "))
IndexOf will return the position of the first letter in your query - Your case it's P in Petter --- So start+"petter".length()
Here's an example of a barcode price checker app I'm working on
// STRING FORMAT 00000899999:0.99
String currentLine = "00000899999:0.99";
int breakPoint = currentLine.indexOf(":");
int end = currentLine.length();
int start = breakPoint + 1;
String Price = currentLine.substring(start,end);
Price will be starting after (:) With +1 or include (:) with no + 1 and end at the lines length.
I wrote an extension function in Kotlin to get initials for a name. You can use a custom view and use draw text and clip shape you want for avatar view.
val initials = "Vikas Patidar".asInitials()
fun String.asInitials(limit: Int = 2): String {
val buffer = StringBuffer()
trim().split(" ").filter {
it.isNotEmpty()
}.joinTo(
buffer = buffer,
limit = limit,
separator = "",
truncated = "",
) { s ->
s.first().uppercase()
}
return buffer.toString()
}

ImageView not updated in ItemList

I am trying to update the image inside ItemList, I am using the below method
This is how I am getting ImageView 'imgView' Object using the below methods -
protected void onPostExecute(Void result) {
super.onPostExecute(result);
// Dismiss the progress dialog
if (pDialog.isShowing())
pDialog.dismiss();
/**
* Updating parsed JSON data into ListView
* */
ListAdapter adapter = new SimpleAdapter(JSONParsingMainActivity.this, contactList,
R.layout.list_item, new String[]{"name", "email", "mobile"}, new int[]{R.id.name,
R.id.email, R.id.mobile});
lv.setAdapter(adapter);
if(markedQuestion != null){
try {
JSONObject jsonObj = new JSONObject(markedQuestion);
JSONArray contacts = jsonObj.getJSONArray("data");
for (int i = 0; i < contacts.length(); i++) {
JSONObject c = contacts.getJSONObject(i);
String questionid = c.getString("questionid");
System.out.println("Question No :" + i + "Question Id is :" + questionid);
Long itemId = lv.getItemIdAtPosition(Integer.parseInt(questionid));
System.out.println("Item Id xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxasdasd" + itemId);
changedView = (CardView) getViewByPosition(Integer.parseInt(questionid),lv);
imgView = (ImageView) changedView.findViewById(R.id.imageViewstickId);
imgView.setImageResource(R.drawable.cancel);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
mp.start();
position++;
Intent intent = new Intent(context, DetailsActivity.class);
intent.putExtra("position", Integer.toString(position));
context.startActivity(intent);
}
});
}
public View getViewByPosition(int pos, ListView listView) {
final int firstListItemPosition = listView.getFirstVisiblePosition();
final int lastListItemPosition = firstListItemPosition + listView.getChildCount() - 1;
if (pos < firstListItemPosition || pos > lastListItemPosition ) {
return listView.getAdapter().getView(pos, null, listView);
} else {
final int childIndex = pos - firstListItemPosition;
return listView.getChildAt(childIndex);
}
}
This code is not updating the cancel image in the list item, when I debug I am getting correct id (i.e. 'imageViewstickId' as shown in XML), but the image is not updating, nor giving any error
imgView.setImageResource(R.drawable.cancel);
My XML code also -
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
android:id="#+id/cardViewQuestionId"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:card_view="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
app:cardCornerRadius="15dp"
card_view:cardElevation="2dp"
card_view:cardCornerRadius="5dp">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="10dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<TextView
android:id="#+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingBottom="2dp"
android:paddingTop="6dp"
android:text="test"
android:textColor="#color/gradientStart"
android:textSize="16sp"
android:textStyle="bold"
/>
<ImageView
android:id="#+id/imageViewstickId"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_gravity="center"
/>
</LinearLayout>
<TextView
android:id="#+id/email"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingBottom="2dip"
android:textColor="#color/gradientStop" />
<TextView
android:id="#+id/mobile"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#5d5d5d"
android:textStyle="bold" />
</LinearLayout>
</android.support.v7.widget.CardView>
I have tried everything modifying the code, etc. but nothing works for me
Any help shall be appreciated, thanks
Edits - (Added Debug screen Shot) Please check in debug window, CardViewId is same as XML ImageView id... Still not updating
Solution:
Instead of this:
imgView.setImageResource(R.drawable.cancel);
write:
imgView.setImageDrawable(ContextCompat.getDrawable(context, R.drawable.cancel));
Try it. Let's hope it works.
You can simply use this:
If you are in Activity then use this
int id = getResources().getIdentifier("cancel","drawable", getPackageName());
Or in Fragment
int id = context.getResources().getIdentifier("cancel","drawable", context.getPackageName());
// "cancel" drawable resource name
// "drawable" drawable directory
This will return the id of the drawable you want to access... then you can set the image in the imageview by doing the following
imgView.setImageResource(id);
hope this will work.
There are two best solutions for that:-
1) Instead of using this
imgView.setImageResource(R.drawable.cancel);
Use This Method:-
imgView.setImageDrawable(ContextCompat.getDrawable(context, R.drawable.cancel));
2)Using Glide Library
https://bumptech.github.io/glide/doc/download-setup.html
Your id is correct still it is not updating then it might be a problem that your Resource file (R.java) is not updating try cleaning your project and then check.
If this not works you can also try following code:-
Resources resources = getResources();
imgView.setImageDrawable(resources.getDrawable(R.drawable.cancel));
May be helpful for others to think this way -
I got Clue from this post - how to know when listview is finished loading data on android
My data may be getting updated, but the list was not fully loaded and thats why the data is over writing. I used the following code , thanks for the actual user who posted the above answer
lv.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
#Override
public void onLayoutChange(View view, int i, int i1, int i2, int i3, int i4, int i5, int i6, int i7) {
Log.d(TAG, "list got updated, do what ever u want");
if(markedQuestion != null){
try {
JSONObject jsonObj = new JSONObject(markedQuestion);
JSONArray contacts = jsonObj.getJSONArray("data");
for (int x = 0; x < contacts.length(); x++) {
JSONObject c = contacts.getJSONObject(x);
String questionid = c.getString("questionid");
System.out.println("Question No :" + x + "Question Id is :" + questionid);
System.out.println(getResources());
changedView = (CardView) getViewByPosition(Integer.parseInt(questionid),lv);
imgView = (ImageView) changedView.findViewById(R.id.imageViewstickId);
// imgView.setImageResource(R.drawable.cancel);
imgView.setImageDrawable(ContextCompat.getDrawable(context,R.drawable.cancel));
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
});

ListView inside RecyclerView

I am using recyclerViewAdapter to fill recycler view.
Inside each recyclerItem must be list with items, and I decided to make it listView.
so inside onBindViewHolder of recyclerView i create new cursor, make new query (with help of contentResolver) and than i need to fill my listView with this data.
here is some my code:
Cursor.moveToFirst();
int size = Cursor.getCount();
if (size > 0){
for (int i = 0; i<size; i++){
Cursor.moveToPosition(i);
setCurrentItem();
}
}
private void setCurrentItem(){
View View = LayoutInflater.from(mContext).inflate(R.layout.list_item_test, null);
mLogo = (ImageView)cardView.findViewById(R.id.iv_Item_logo);
mName = (TextView)cardView.findViewById(R.id.tv_cardItem_Name);
mNumber = (TextView)cardView.findViewById(R.id.tv_cardItem_Number);
mBlocked = (TextView)cardView.findViewById(R.id.tv_Item_blocked);
mDate = (TextView)cardView.findViewById(R.id.tv_Item_Date);
String Name = Cursor.getString(AccountsFragment.NAME);
String Numb = Cursor.getString(AccountsFragment.NUMBER);
Numb = USB_Converter.Numbers(Numb);
String Date = Cursor.getString(AccountsFragment.DATE);
Date = USB_Converter.friendlyDate(Date);
int Status = Cursor.getInt(AccountsFragment.STATUS);
if (Status == UserAccountsEntry.STATUS_ID_ACTIVE){
mBlocked.setVisibility(View.VISIBLE);
} else {
mBlocked.setVisibility(View.GONE);
}
mName.setText(Name);
mNumber.setText(Numb);
mDate.setText(Date);
mHolder.mListView.addFooterView(cardView);
}
this works without any errors, but it's does not show listView...
any idea how to fix this?
EDIT
listView.xml:
<ListView
android:id="#+id/lv_accountList"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:dividerHeight="1dp"
/>
You don't show the content of list_item_test.xml, but you'll probably need to give the inner ListView an absolute height in dp (or give it's parent an absolute height and then use match_parent on the ListView).
You can't use wrap_content for a ListView (it will shrink to 0 height), and you can't use match_parent if the parent doesn't have an independently derived or explicitly declared height.

How to use Android view element by tag

I have some TextView elements in my layout file like below:
<Table Layout>
...
<TableRow android:gravity="center_horizontal">
<TextView
android:id="#+id/tv1"
android:text="#string/text_value"
android:tag="id1">
...
<TableRow android:gravity="center_horizontal">
<TextView
android:id="#+id/tv1"
android:text="#string/text_value"
android:tag="idN">
...
</TableLayout>
I want to use
for (int i=0; i < 10; i++) {
myText = (TextView) findViewbyTag("id"+i);
myText.setText("id is - "+i);
}
construction to match each tag value and set text value into each TextView element.
It doesn't work!
I understand how tp use it by id, but id is int value from resources and I can't
generate it in my for() construction.
What can I do? How do I use findViewbyTag?
You don't need to use tags. You should rather use dynamical resource retrieval as following.
for (int i=0; i < 10; i++) {
// generate id dynamically
int id = getResources().getIdentifier("tv"+i, "id", getPackageName());
// find view by generated id
TextView myText = (TextView) findViewbyId(id);
myText.setText("id is - tv"+i);
}
The both getResources() and getPackageName() are the methods of Context.

ListView item background changes depending on scroll position

I'm running into a very strange ListView behaviour. I have a simple chat application which uses a ListView and a custom implementation of BaseAdapter to show messages.
The idea I had was to shade messages from the "local" user grey and have messages from the "remote" user white, to help the user distinguish between the two.
The two screenshots below show what's happening. The second is the exact same activity, xml etc etc, simply scrolled down a bit.
Scrolled up:
Scrolled down:
Look at the message sent by "Me" # 23:05. When at the top it has no contrast to its neighbours, but when it is scrolled to the bottom, the difference is clear to see.
This occurs on a nexus 4 and 7 on 4.2.2, and a GS3 running 4.1.2.
Here is the XML for one of the ListView items:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/activity_view_conversation_message_list_item_wrapper"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingBottom="2dp"
android:paddingLeft="5dp"
android:paddingRight="10dp" >
<ImageView
android:id="#+id/activity_view_conversation_message_list_item_user_image"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignParentLeft="true"
android:layout_marginTop="5dp"
android:src="#drawable/default_user_image" />
<TextView
android:id="#+id/activity_view_conversation_message_list_item_heading"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="1dp"
android:layout_toRightOf="#+id/activity_view_conversation_message_list_item_user_image"
android:text="Martyn"
android:textColor="#000000"
android:textSize="18sp"
android:textStyle="bold" />
<TextView
android:id="#+id/activity_view_conversation_message_list_item_contents"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/activity_view_conversation_message_list_item_heading"
android:layout_marginLeft="10dp"
android:layout_marginRight="5dp"
android:layout_marginTop="2dp"
android:layout_toLeftOf="#+id/activity_view_conversation_message_list_item_ack"
android:layout_toRightOf="#+id/activity_view_conversation_message_list_item_user_image"
android:text="Hello this is some text"
android:textColor="#333333"
android:textIsSelectable="true"
android:textSize="18sp" />
<TextView
android:id="#+id/activity_view_conversation_message_list_item_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginTop="2dp"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:paddingTop="2dp"
android:text="12:08"
android:textSize="14sp" />
<ImageView
android:id="#+id/activity_view_conversation_message_list_item_ack"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/activity_view_conversation_message_list_item_time"
android:layout_marginRight="2dp"
android:layout_marginTop="8dp"
android:layout_toLeftOf="#+id/activity_view_conversation_message_list_item_time"
android:src="#drawable/red_dot_8dp" />
</RelativeLayout>
And here is where I set the colour of the RelativeLayout:
if(localUserId.equals(remoteUserId)){
itemLayout.setBackgroundColor(Color.parseColor("#F9F9F9"));
}
That code runs inside the getView() method of my Adapter.
I've Googled this a fair bit and turned up nothing, there are a lot of SO questions regarding the android:cacheColorHint issue but I don't think that is what is going on here.
Has anyone run into this before? I'm stumped!
EDIT: Here's the baseadapter code:
public class MessageListAdapter extends BaseAdapter {
private ArrayList<Message> messageList;
Context context;
/**
* Constructor
* #param newConversationsList An ArrayList of Conversation objects that this adapter will use
* #param newContext The context of the activity that instantiated this adapter
*/
MessageListAdapter(ArrayList<Message> newMessageList, Context newContext){
messageList = newMessageList;
//reload();
context = newContext;
}
public int getCount() {
return messageList.size();
}
public Object getItem(int position) {
return messageList.get(position);
}
public long getItemId(int position) {
return position;
}
/**
* Adds a message to the chat list
* #param message A Message object containing all the message's information
*/
public void add(Message message){
//nMessagesToShow++; //A new message has been added, so increase the number to show by one
Log.d(TAG, "COUNT: "+getCount());
//refresh();
}
public void refresh(){
this.notifyDataSetChanged();
}
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
if(view!=null){
//return view;
}
LayoutInflater vi = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//Get the Message object from the list
Message message = messageList.get(position);
//Get the data from the message
String senderId = message.getFromUser();
int messageType = message.getType();
String senderFirstName;
ImageView userImage, messageImage;
TextView messageHeading, messageBody;
switch(messageType){
case Message.MESSAGE_TYPE_TEXT: //Standard text message
//The layout we inflate for this list item will vary depending on whether this message has the same sender as the previous
if(position>0 && senderId.equals(messageList.get(position-1).getFromUser())){ //True if this is not the first message AND the sender id matches that of the previous message
view = vi.inflate(R.layout.activity_view_conversation_message_list_item_alternate, null); //Inflate an alternate version of the list item which has no heading or user image
}
else{ //This is the first message OR the sender id is different to the previous
view = vi.inflate(R.layout.activity_view_conversation_message_list_item, null); //Inflate the standard version of the layout
userImage = (ImageView) view.findViewById(R.id.activity_view_conversation_message_list_item_user_image);
messageHeading = (TextView) view.findViewById(R.id.activity_view_conversation_message_list_item_heading);
//Use the sender's ID to get the sender's image and first name
Contact contact = database.getContact(senderId);
if(senderId.equals(localUserId)){ //True if the local user sent this message
senderFirstName = "Me";
}
else{
senderFirstName = contact.getFirstName();
}
userImage.setImageBitmap(contact.getImageBitmap(100, 100, 6));
messageHeading.setText(senderFirstName);
}
messageBody = (TextView) view.findViewById(R.id.activity_view_conversation_message_list_item_contents);
messageBody.setText(message.getContents(null));
break;
case Message.MESSAGE_TYPE_IMAGE: //Image message
view = vi.inflate(R.layout.activity_view_conversation_message_list_item_image, null); //Inflate a list item template for displaying an image
userImage = (ImageView) view.findViewById(R.id.activity_view_conversation_message_list_item_user_image);
//Sender's first name
messageHeading = (TextView) view.findViewById(R.id.activity_view_conversation_message_list_item_heading);
Contact contact = database.getContact(senderId);
if(senderId.equals(localUserId)){ //True if the local user sent this message
senderFirstName = "Me";
}
else{
senderFirstName = contact.getFirstName();
}
messageHeading.setText(senderFirstName);
messageImage = (ImageView) view.findViewById(R.id.activity_view_conversation_message_list_item_image);
String imageResourceId = null;
//The message is a JSON object containing several fields, one of which is the file name which we will use to get the image
try {
JSONObject messageJSON = new JSONObject(message.getContents(null));
String imagePath = Environment.getExternalStorageDirectory()+"/epicChat/resources/"+messageJSON.getString("fileName");
int imageWidth = messageJSON.getInt("width"); //We want the dimensions in order to calculate the aspect ratio of the image
int imageHeight = messageJSON.getInt("height");
if(messageJSON.has("resourceId")){
imageResourceId = messageJSON.getString("resourceId"); //This is used when opening the image gallery
}
int displayWidth = 300;
int displayHeight = (int) ((float) imageHeight / (float) imageWidth * (float) displayWidth);
String imagePathFull = imagePath+displayWidth+displayHeight; //For the caching
Bitmap originalImage = null;
//Check the bitmap cache exists. If not, reinstantiate it
if(MainActivity.bitmapCache==null){ //Cache is null
MainActivity.loadBitmapCache();
}
else{ //Cache is not null, so check it to see if this image is in it
originalImage = MainActivity.bitmapCache.get(imagePathFull);
}
if(originalImage==null){ //True if the bitmap was not in the cache. So we must load from disk instead
new Utils.LoadBitmapAsync(imagePath, messageImage, displayWidth, displayHeight, MainActivity.bitmapCache).execute();
messageImage.getLayoutParams().height = displayHeight;
}
else{
messageImage.setImageBitmap(originalImage);
}
}
catch (JSONException e) {
Log.e(TAG, "Error reading image JSON: "+e.toString());
}
if(imageResourceId!=null){ //Only attach the listener if we got a valid resource ID
final String recourceIdFinal = imageResourceId;
final String conversationIdFinal = message.getUserList();
messageImage.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent showConversationImageGalleryIntent = new Intent(context, ViewConversationImageGalleryActivity.class);
showConversationImageGalleryIntent.putExtra("conversationId", conversationIdFinal);
showConversationImageGalleryIntent.putExtra("resourceId", recourceIdFinal);
startActivityForResult(showConversationImageGalleryIntent, ACTION_SHOW_CONVERSATION_IMAGE_GALLERY);
}
});
}
userImage.setImageBitmap(contact.getImageBitmap(100, 100, 6));
break;
case Message.MESSAGE_TYPE_INVALID:
default:
break;
}
//Some layout items are present in all layouts. Typically these are the status indicator and the message time
RelativeLayout itemLayout = (RelativeLayout) view.findViewById(R.id.activity_view_conversation_message_list_item_wrapper);
//If the message is from the local user, give it a subtle grey background
if(localUserId.equals(message.getFromUser())){
itemLayout.setBackgroundColor(Color.parseColor("#E9E9E9"));
}
else{
itemLayout.setBackgroundColor(Color.parseColor("#FFFFFF"));
}
TextView messageTimeText = (TextView) view.findViewById(R.id.activity_view_conversation_message_list_item_time);
messageTimeText.setText(message.getFormattedTime());
ImageView messageStatusImage = (ImageView) view.findViewById(R.id.activity_view_conversation_message_list_item_ack);
//Set the status image according to the status of the message
switch(message.getStatus()){
case Message.MESSAGE_STATUS_PENDING: //Pending messages should have a red dot
messageStatusImage.setImageResource(R.drawable.red_dot_8dp);
messageStatusImage.setVisibility(View.VISIBLE);
break;
case Message.MESSAGE_STATUS_ACK_SERVER: //Messages that reached the server should have an orange dot
messageStatusImage.setImageResource(R.drawable.orange_dot_8dp);
messageStatusImage.setVisibility(View.VISIBLE);
break;
case Message.MESSAGE_STATUS_ACK_RECIPIENT: //Messages that reached the recipient should have an green dot
messageStatusImage.setImageResource(R.drawable.green_dot_8dp);
messageStatusImage.setVisibility(View.VISIBLE);
break;
case Message.MESSAGE_STATUS_NOT_SET: //Not set typically means the message came from another user, in which case the status image should be hidden
default: //Also default here
messageStatusImage.setVisibility(View.INVISIBLE);
break;
}
return view;
}
}
Since there's no else statement matching your if, this is probably due to view recycling. When an item from a ListView scrolls off the screen, the operating system removes it and hands it back to the adapter in the same state it was removed. This means you need to set the background color when it's not the local user's message as well.

Categories

Resources