Reputation: 1
In Android, the views added to linearlayout using linearlayout.addView() doesn't maintain order while running. , The order keeps changing on different runs and taps and maintains no particular order. Whenever I try to refresh, the order changes and it is very irritating, I could not find any solution to this. Please Help. In Android, the views added to linearlayout using linearlayout.addView() doesn't maintain order while running. , The order keeps changing on different runs and taps and maintains no particular order. Whenever I try to refresh, the order changes and it is very irritating, I could not find any solution to this. Please Help. In Android, the views added to linearlayout using linearlayout.addView() doesn't maintain order while running. , The order keeps changing on different runs and taps and maintains no particular order. Whenever I try to refresh, the order changes and it is very irritating, I could not find any solution to this. Please Help
package com.example.instagramclone;
import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import com.parse.FindCallback;
import com.parse.GetCallback;
import com.parse.GetDataCallback;
import com.parse.ParseException;
import com.parse.ParseFile;
import com.parse.ParseObject;
import com.parse.ParseQuery;
import com.parse.ParseUser;
import com.shashank.sony.fancytoastlib.FancyToast;
import java.text.SimpleDateFormat;
import java.util.List;
public class usersPost extends AppCompatActivity {
private LinearLayout linearLayout;
private String receivedUserName;
LinearLayout rootlayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_users_post);
linearLayout = (LinearLayout) findViewById(R.id.linearLayout);
// rootlayout = (LinearLayout) findViewById(R.id.rootlayout);
Intent receivedIntentObject = getIntent();
receivedUserName = receivedIntentObject.getStringExtra("username");
FancyToast.makeText(this, receivedUserName, FancyToast.LENGTH_SHORT,
FancyToast.INFO, true).show();
setTitle(receivedUserName + "'s posts");
final ProgressDialog dialog = new ProgressDialog(this);
dialog.setMessage("Loading...");
dialog.show();
// if (linearLayout.getChildCount() > 0)
// linearLayout.removeAllViews();
displayprofilepic();
diaplayprofilename();
displayposts();
dialog.dismiss();
}
public void displayprofilepic()
{
//for displaying profile picture first
try {
final ParseQuery<ParseUser> parseUser = ParseUser.getQuery();
parseUser.whereEqualTo("username", receivedUserName);
parseUser.findInBackground(new FindCallback<ParseUser>() {
@Override
public void done(List<ParseUser> objects, ParseException e) {
if (objects.size()>0 && e == null) {
ParseFile parseFile = objects.get(0).getParseFile("profilepicture");
if (parseFile != null) {
TextView textView = new TextView(usersPost.this);
textView.setText("Profile Picture:");
linearLayout.addView(textView);
parseFile.getDataInBackground(new GetDataCallback() {
@Override
public void done(byte[] data, ParseException e) {
if (data != null && e == null) {
Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
ImageView postImageView = new ImageView(usersPost.this);
LinearLayout.LayoutParams imageview_params = new LinearLayout.LayoutParams(1000, 1000);
imageview_params.setMargins(5, 5, 5, 5);
postImageView.setLayoutParams(imageview_params);
postImageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
postImageView.setImageBitmap(bitmap);
linearLayout.addView(postImageView);
}
}
});
} else {
TextView textView = new TextView(usersPost.this);
textView.setText("Profile Picture:");
linearLayout.addView(textView);
ImageView postImageView = new ImageView(usersPost.this);
LinearLayout.LayoutParams imageview_params = new LinearLayout.LayoutParams(500, 500);
imageview_params.setMargins(5, 5, 5, 5);
postImageView.setLayoutParams(imageview_params);
postImageView.setScaleType(ImageView.ScaleType.FIT_START);
postImageView.setImageResource(R.drawable.empty_profile_pic);
linearLayout.addView(postImageView);
}
}
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
public void diaplayprofilename()
{
/////////profile name
ParseQuery<ParseUser> parseUser = ParseUser.getQuery();
parseUser.whereEqualTo("username", receivedUserName);
parseUser.getFirstInBackground(new GetCallback<ParseUser>() {
@Override
public void done(ParseUser object, ParseException e) {
if (object != null && e == null) {
TextView name = new TextView(usersPost.this);
name.setText(object.get("profileName") + "");
LinearLayout.LayoutParams txt_params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
txt_params.setMargins(5, 5, 5, 50);
name.setLayoutParams(txt_params);
name.setGravity(Gravity.CENTER);
name.setBackgroundResource(R.drawable.txtview_bg);
name.setTextColor(Color.BLACK);
name.setTextSize(20f);
linearLayout.addView(name);
}
}
});
}
public void displayposts()
{
/////////////////
try {
final ParseQuery<ParseObject> parseQuery = new ParseQuery<ParseObject>("photo");
parseQuery.whereEqualTo("username", receivedUserName);
parseQuery.orderByDescending("createdAt");
parseQuery.findInBackground(new FindCallback<ParseObject>() {
@Override
public void done(List<ParseObject> objects, ParseException e) {
if (objects.size() > 0 && e == null) {
for (ParseObject post : objects) {
final String s = (post.get("caption") != null) ? post.get("caption") + "" : "";
SimpleDateFormat formatter = new SimpleDateFormat("dd MMMM yyyy");
// formatter.setTimeZone(TimeZone.getTimeZone("IST"));
final String dt = formatter.format(post.getCreatedAt());
Log.i("data", dt);
ParseFile postpicture = (ParseFile) post.get("picture");
postpicture.getDataInBackground(new GetDataCallback() {
@Override
public void done(byte[] data, ParseException e) {
if (data != null && e == null) {
Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
ImageView postImageView = new ImageView(usersPost.this);
LinearLayout.LayoutParams imageview_params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
imageview_params.setMargins(0, 5, 145, 5);
postImageView.setLayoutParams(imageview_params);
postImageView.setPadding(15, 0, 15, 0);
postImageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
postImageView.setBackgroundResource(R.drawable.imgview_bg);
Drawable d = new BitmapDrawable(getResources(), bitmap);
//postImageView.setImageBitmap(bitmap);
postImageView.setImageDrawable(d);
linearLayout.addView(postImageView);
TextView date = new TextView(usersPost.this);
date.setText(dt);
LinearLayout.LayoutParams dt_params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
dt_params.setMargins(5, 5, 5, 50);
date.setLayoutParams(dt_params);
date.setGravity(Gravity.START);
date.setBackgroundResource(R.drawable.txtview_bg);
date.setTextColor(Color.BLACK);
date.setTextSize(15f);
linearLayout.addView(date);
TextView caption = new TextView(usersPost.this);
caption.setText(s);
LinearLayout.LayoutParams des_params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
des_params.setMargins(5, 5, 5, 50);
caption.setLayoutParams(des_params);
caption.setGravity(Gravity.CENTER);
caption.setBackgroundResource(R.drawable.txtview_bg);
caption.setTextColor(Color.BLUE);
caption.setTextSize(20f);
linearLayout.addView(caption);
}
}
});
}
} else {
FancyToast.makeText(usersPost.this, "No Posts!!",
FancyToast.LENGTH_SHORT, FancyToast.INFO, true).show();
//finish();
// LinearLayout linearLayout1 =
// (LinearLayout) findViewById(R.id.rootlayout);
TextView textView = new TextView(usersPost.this);
textView.setGravity(Gravity.CENTER);
textView.setTextSize(25);
//textView.setScaleX(0.5f);
textView.setText(receivedUserName + " has no posts");
linearLayout.addView(textView);
}
}
});
}catch (Exception e){
e.printStackTrace();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menuchat,menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
if(item.getItemId()==R.id.chatItem)
{
Intent intent = new Intent(usersPost.this,ChatActivity.class);
intent.putExtra("username",receivedUserName);
startActivity(intent);
}
return super.onOptionsItemSelected(item);
}
}
My XML code is as follows:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:id="@+id/rootlayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".usersPost"
android:orientation="vertical">
<ScrollView
android:layout_width="409dp"
android:layout_height="0dp"
android:layout_weight="2.7"
tools:layout_editor_absoluteX="1dp"
tools:layout_editor_absoluteY="1dp"
>
<LinearLayout
android:id="@+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" />
</ScrollView>
</LinearLayout>
Upvotes: 0
Views: 166
Reputation: 5241
You call displayprofilepic()
, diaplayprofilename()
and displayposts()
3 method call asynchronous
it means done
callback asynchronous
too. it will not keep the order when you addView. You should call displayprofilepic
first and when it done call diaplayprofilename
so on displayposts
Upvotes: 1
Reputation: 25
You don't pass a position to addView(). As a second parameter you are able to pass the position of the added View, e.g. addView (button, 2).
Upvotes: 1