[在Android中,使用linearlayout.addView()添加到linearlayout的视图在运行时不会保持顺序

问题描述 投票:0回答:2

在Android中,使用linearlayout.addView()添加到linearlayout的视图在运行时不会保持顺序。 ,订单会在不同的运行和水龙头上不断变化,并且不维护特定的订单。每当我尝试刷新时,顺序都会发生变化,并且非常烦人,我找不到任何解决方案。请帮忙。在Android中,使用linearlayout.addView()添加到linearlayout的视图在运行时不会保持顺序。 ,订单会在不同的运行和水龙头上不断变化,并且不维护特定的订单。每当我尝试刷新时,顺序都会发生变化,并且非常烦人,我找不到任何解决方案。请帮忙。在Android中,使用linearlayout.addView()添加到linearlayout的视图在运行时不会保持顺序。 ,订单会在不同的运行和水龙头上不断变化,并且不维护特定的订单。每当我尝试刷新时,顺序都会发生变化,并且非常烦人,我找不到任何解决方案。请帮助

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

我的XML代码如下:

<?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>
java android android-linearlayout
2个回答
0
投票

您没有将位置传递给addView()。作为第二个参数,您可以传递添加的视图的位置,例如addView(按钮2)。


0
投票

您调用displayprofilepic()diaplayprofilename()displayposts() 3方法调用asynchronous,这也意味着done回调asynchronous。当您添加视图时,它不会保持顺序。您应该先调用displayprofilepic,完成后再调用diaplayprofilename,依此类推displayposts

© www.soinside.com 2019 - 2024. All rights reserved.