需要帮助以列表形式显示带有图像的消息文本

发布于 2024-12-04 11:48:21 字数 711 浏览 0 评论 0原文

我需要启动一个包含文本和图像列表的应用程序。我需要在带有图像的列表中显示这些文本消息。我使用线性布局用图像显示这些消息,如下所示。

for(int i=0;i<messages.size();i++)
{
    FrameLayout f1= new FrameLayout(this);
    LinearLayout l1 = new LinearLayout(this);
    LinearLayout l2 = new LinearLayout(this);
    im =new ImageView(this);
    TextView tv =new TextView(this);
    tv.setText(messages.get(i).getmessage());
    im.setImageResource(R.drawable.person);
    l1.addView(tv);
    l2.addView(im);
    f1.addView(l1);
    f1.addView(l2);
    ((LinearLayout)findViewById(R.id.LinearlayoutMessage)).addView(f1);
}

一段时间后,我只需要刷新图像视图中的图像。我试图通过为每个图像视图设置 id 来做到这一点,但我只得到最后一个图像视图 id。

我知道有listview,但是它适合我的要求吗?我不知道我该如何完成我的任务。有人可以帮我吗?

I need to start an application that contains a list of text with images. I need to display these text messages in a list with images. I am displaying these messages with images by using linear layouts as shown below.

for(int i=0;i<messages.size();i++)
{
    FrameLayout f1= new FrameLayout(this);
    LinearLayout l1 = new LinearLayout(this);
    LinearLayout l2 = new LinearLayout(this);
    im =new ImageView(this);
    TextView tv =new TextView(this);
    tv.setText(messages.get(i).getmessage());
    im.setImageResource(R.drawable.person);
    l1.addView(tv);
    l2.addView(im);
    f1.addView(l1);
    f1.addView(l2);
    ((LinearLayout)findViewById(R.id.LinearlayoutMessage)).addView(f1);
}

After some time I need to refresh only images in the image view. I am trying to do this by setting id's to each image view's but I get only last image view id.

I know there is listview, but is it suitable for my requirement? I don't know how can I do my task. Can anyone please help me?

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

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

发布评论

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

评论(2

玩物 2024-12-11 11:48:21

我的建议是使用适配器来显示图像和相应的文本。它将帮助您选择/聚焦项目。

请分享结果。

My suggestion would be to use Adapter for showing image and respective text. It will help you get selected/focused item.

Please share result.

一曲爱恨情仇 2024-12-11 11:48:21

我做了类似的事情..我只是做了一个 TextViews 列表(每行两个 TextView),我需要更新列表中的一个 TextView ..我所做的是一个垂直 LinearLayout,并添加包含我的 TextViews 的水平 LinearLayouts ..然后我有一个字符串列表来查找我需要修改的字符串的索引,然后使用 LinearLayout.getChildAt(int) 搜索它并更新值...

代码:
我有一个字符串列表和一个计数器,显示该字符串出现了多少次...

private ArrayList<String> strList;
private ArrayList<Integer> counterList;

当字符串是新的时:

strList.add(new_string);
TextView str = new TextView(getApplicationContext());
str.setText(sTemp);
str.setLayoutParams(strsTextView.getLayoutParams());
TextView strcounter = new TextView(getApplicationContext());
strcounter.setLayoutParams(counterTextView.getLayoutParams());
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT,1);

LinearLayout ll = new LinearLayout(this);
ll.setLayoutParams(new LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT));
ll.setOrientation(LinearLayout.HORIZONTAL);
ll.addView(str, layoutParams);

layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT,3);

counterList.add(1);
strcounter.setText("1");
ll.addView(strcounter, layoutParams);
mLinearLayout.addView(ll,new LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT));

我检查字符串是否不是新的(已经在 strList 中)if(strList.indexOf( sTemp)!=0) 然后执行以下操作:

Integer intaux =counterList.get(index);
intaux++;
counterList.set(index, intaux);
LinearLayout aux = (LinearLayout)mLinearLayout.getChildAt(index);
TextView aux2 = (TextView)aux.getChildAt(1);
aux2.setText(Integer.toString(intaux));

我希望我没有错过任何内容,并且这对您有用..如果您不明白任何内容,请告诉我,我会尝试解释/纠正它。

PS:我认为@abhijeet的适配器解决方案是我的实现的更优雅的版本,但我还没有这样做。

I did something similar.. I just did a list of TextViews (two TextView per row), and i needed to update one TextView in the list.. What i did was a vertical LinearLayout, and add Horizontal LinearLayouts that contained my TextViews.. Then i had a List of strings to look for the index of the one i needed to modify, and then search it with LinearLayout.getChildAt(int) and update the value...

CODE:
I have a list of strings and a counter that says how many times come this string...

private ArrayList<String> strList;
private ArrayList<Integer> counterList;

When the String is new:

strList.add(new_string);
TextView str = new TextView(getApplicationContext());
str.setText(sTemp);
str.setLayoutParams(strsTextView.getLayoutParams());
TextView strcounter = new TextView(getApplicationContext());
strcounter.setLayoutParams(counterTextView.getLayoutParams());
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT,1);

LinearLayout ll = new LinearLayout(this);
ll.setLayoutParams(new LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT));
ll.setOrientation(LinearLayout.HORIZONTAL);
ll.addView(str, layoutParams);

layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT,3);

counterList.add(1);
strcounter.setText("1");
ll.addView(strcounter, layoutParams);
mLinearLayout.addView(ll,new LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT));

I check if the String is not new (is already in the strList)if(strList.indexOf(sTemp)!=0) and then do as follows:

Integer intaux =counterList.get(index);
intaux++;
counterList.set(index, intaux);
LinearLayout aux = (LinearLayout)mLinearLayout.getChildAt(index);
TextView aux2 = (TextView)aux.getChildAt(1);
aux2.setText(Integer.toString(intaux));

I hope i didn't miss anything and that this works for you.. If you don't understand anything tell me an i try to explain/correct it.

PS: I suppose that the Adapter solution of @abhijeet is a more elegant version of my implementation, but i haven't done it.

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