如何在动态布局中添加更多视图在android中实现scrollview

发布于 2024-10-31 05:56:47 字数 3757 浏览 1 评论 0原文

代码文件中一一加载图像后,首先首先初始加载消息

我正在实现自定义线性布局,在此添加更多视图,在后台过程中使用 Web 服务检索数据库所有数据都是检索消息和消息相关的用户图像,在自定义线性布局main.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout android:id="@+id/frameLayout1" android:layout_width="fill_parent" android:background="@android:color/white"
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_height="fill_parent">
    <ScrollView android:id="@+id/scrollView1" android:layout_height="wrap_content" android:layout_width="fill_parent">
        <LinearLayout android:layout_width="fill_parent" android:id="@+id/linearLayout2" android:layout_height="fill_parent">
            <LinearLayout android:layout_width="fill_parent" android:id="@+id/linearLayout3" android:layout_height="fill_parent">
                <ImageView android:id="@+id/person" android:layout_width="wrap_content" android:layout_height="wrap_content"></ImageView>
                <TextView android:textColor="@android:color/black" android:id="@+id/textView1" android:layout_height="wrap_content" android:layout_width="wrap_content"></TextView>
            </LinearLayout>
        </LinearLayout>
    </ScrollView>
</FrameLayout>

public class MYlist extends Activity 
{
    /** Called when the activity is first created. */
    List<MessageClass> messages;
    int i=0,n=5;
     ParseXMLString parse=new ParseXMLString();
     DataParsingComm dataparsing=new DataParsingComm();
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        String xml="<spGetUserMessages><SearchLocation></SearchLocation><LoginUserID>125</LoginUserID></spGetUserMessages>";
        messages =parse.GetGetUserMessages(dataparsing.ILGetUserMessages(xml));


        addElements(messages,i,n);
    }

    protected void addElements(final List<MessageClass> messages2,int i,int n) 
    {
        int k=0;
     //((LinearLayout)findViewById(R.id.linearLayout1)).removeAllViews();

        for(int i1=i;i1<n;i1++)
        {
            LinearLayout ll1=((LinearLayout)findViewById(R.id.linearLayout2));
            ll1.setBackgroundResource(R.drawable.tabmessage);
            Log.v("11111111","333"+ i1+ n);
          if(k==i1)
          {
            for(int j1=i1;j1<n;j1++)
            {

            TextView msg=((TextView)findViewById(R.id.textView1));
            msg.setText(messages2.get(j1).getmessage());

            }
          }
            ImageView prsnImg=((ImageView)findViewById(R.id.person));
        try{
            String photoXml="<spGetUserPhoto><UserID>125</UserID></spGetUserPhoto>";

            String photoInfoFromDB=new DataParsingComm().ILGetImage(photoXml);

            if(photoInfoFromDB.equalsIgnoreCase("anyType{}")||photoInfoFromDB.equals(null) )
            {
                prsnImg.setImageResource(R.drawable.person);
            }
            else{

                byte[] imgArry= Base64.decode(photoInfoFromDB);

                Bitmap bit=BitmapFactory.decodeByteArray(imgArry,0,imgArry.length);

                prsnImg.setImageBitmap(bit);
            }
        }catch (Exception e) {
            e.printStackTrace();
        }
        finally
            {
                System.gc();
                System.runFinalization();
            }

        }

    }
}

屏幕截图: < a href="http://www.freeimagehosting.net/uploads/8437c22cca.png" rel="nofollow">http://www.freeimagehosting.net/uploads/8437c22cca.png

我的问题是每次显示一项所有数据覆盖布局中的单项视图 如何在线性布局中实现一一显示视图

请告诉我一些解决方案 提前致谢

i am implementing custom linearlayout in this adding more views retrieve database using webservices in back ground process all data are retrive messages and message related userimages also first initally loading messages after one by one loading images in custom linear layout

main.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout android:id="@+id/frameLayout1" android:layout_width="fill_parent" android:background="@android:color/white"
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_height="fill_parent">
    <ScrollView android:id="@+id/scrollView1" android:layout_height="wrap_content" android:layout_width="fill_parent">
        <LinearLayout android:layout_width="fill_parent" android:id="@+id/linearLayout2" android:layout_height="fill_parent">
            <LinearLayout android:layout_width="fill_parent" android:id="@+id/linearLayout3" android:layout_height="fill_parent">
                <ImageView android:id="@+id/person" android:layout_width="wrap_content" android:layout_height="wrap_content"></ImageView>
                <TextView android:textColor="@android:color/black" android:id="@+id/textView1" android:layout_height="wrap_content" android:layout_width="wrap_content"></TextView>
            </LinearLayout>
        </LinearLayout>
    </ScrollView>
</FrameLayout>

codefile:

public class MYlist extends Activity 
{
    /** Called when the activity is first created. */
    List<MessageClass> messages;
    int i=0,n=5;
     ParseXMLString parse=new ParseXMLString();
     DataParsingComm dataparsing=new DataParsingComm();
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        String xml="<spGetUserMessages><SearchLocation></SearchLocation><LoginUserID>125</LoginUserID></spGetUserMessages>";
        messages =parse.GetGetUserMessages(dataparsing.ILGetUserMessages(xml));


        addElements(messages,i,n);
    }

    protected void addElements(final List<MessageClass> messages2,int i,int n) 
    {
        int k=0;
     //((LinearLayout)findViewById(R.id.linearLayout1)).removeAllViews();

        for(int i1=i;i1<n;i1++)
        {
            LinearLayout ll1=((LinearLayout)findViewById(R.id.linearLayout2));
            ll1.setBackgroundResource(R.drawable.tabmessage);
            Log.v("11111111","333"+ i1+ n);
          if(k==i1)
          {
            for(int j1=i1;j1<n;j1++)
            {

            TextView msg=((TextView)findViewById(R.id.textView1));
            msg.setText(messages2.get(j1).getmessage());

            }
          }
            ImageView prsnImg=((ImageView)findViewById(R.id.person));
        try{
            String photoXml="<spGetUserPhoto><UserID>125</UserID></spGetUserPhoto>";

            String photoInfoFromDB=new DataParsingComm().ILGetImage(photoXml);

            if(photoInfoFromDB.equalsIgnoreCase("anyType{}")||photoInfoFromDB.equals(null) )
            {
                prsnImg.setImageResource(R.drawable.person);
            }
            else{

                byte[] imgArry= Base64.decode(photoInfoFromDB);

                Bitmap bit=BitmapFactory.decodeByteArray(imgArry,0,imgArry.length);

                prsnImg.setImageBitmap(bit);
            }
        }catch (Exception e) {
            e.printStackTrace();
        }
        finally
            {
                System.gc();
                System.runFinalization();
            }

        }

    }
}

screenshot: http://www.freeimagehosting.net/uploads/8437c22cca.png

my problem is every time display one item all data override that single item view in layout how can implemented one by one display views in linear layout

please tell me some solution thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

柒夜笙歌凉 2024-11-07 05:56:47

这是因为您总是选择相同的两个对象...

有一种非常基本的方法可以获取我认为您可能想要的内容:

main.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/frameLayout1" 
    android:layout_width="fill_parent" 
    android:background="@android:color/white" 
    android:layout_height="fill_parent">
    <ScrollView android:id="@+id/scrollView1" 
        android:layout_height="wrap_content" 
        android:layout_width="fill_parent">

    </ScrollView>
</FrameLayout>

.java

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        ScrollView scrollView = (ScrollView) findViewById(R.id.scrollView1);

        LinearLayout topLinearLayout = new LinearLayout(this);
        topLinearLayout.setOrientation(LinearLayout.VERTICAL); 

        for (int i = 0; i < 15; i++){

            LinearLayout linearLayout = new LinearLayout(this);
            linearLayout.setOrientation(LinearLayout.HORIZONTAL); 

            ImageView imageView = new ImageView (this);
            TextView textView = new TextView (this);

            imageView.setImageResource(R.drawable.image);
            textView.setText("Text View #" + i);

            linearLayout.addView(imageView);
            linearLayout.addView(textView);

            topLinearLayout.addView(linearLayout);          

        }

        scrollView.addView(topLinearLayout);

    }

您还可以在中定义 topLinearLayout 对象xml 文件。这取决于你。

That is because you are always selecting the same two objects...

There is a very basic way of obtaining what I think you might want:

main.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/frameLayout1" 
    android:layout_width="fill_parent" 
    android:background="@android:color/white" 
    android:layout_height="fill_parent">
    <ScrollView android:id="@+id/scrollView1" 
        android:layout_height="wrap_content" 
        android:layout_width="fill_parent">

    </ScrollView>
</FrameLayout>

.java

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        ScrollView scrollView = (ScrollView) findViewById(R.id.scrollView1);

        LinearLayout topLinearLayout = new LinearLayout(this);
        topLinearLayout.setOrientation(LinearLayout.VERTICAL); 

        for (int i = 0; i < 15; i++){

            LinearLayout linearLayout = new LinearLayout(this);
            linearLayout.setOrientation(LinearLayout.HORIZONTAL); 

            ImageView imageView = new ImageView (this);
            TextView textView = new TextView (this);

            imageView.setImageResource(R.drawable.image);
            textView.setText("Text View #" + i);

            linearLayout.addView(imageView);
            linearLayout.addView(textView);

            topLinearLayout.addView(linearLayout);          

        }

        scrollView.addView(topLinearLayout);

    }

You can also define the topLinearLayout object in the xml file. It is up to you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文