【问题标题】:Display pictures with text after async activity异步活动后显示带文字的图片
【发布时间】:2014-05-26 10:09:51
【问题描述】:

我是 android 开发的新手,我正在尝试显示一些小图像(小国旗),每个图像旁边都有一些描述文本。 我正在做的是创建一个连接到互联网的异步作业来检索我需要的信息并在一个类中填充一些变量,最后它返回这个类的一个对象。 函数 onPostExecute

protected void onPostExecute(String result) {
        System.out.println(xmlData.getCurrency("USD"));
        try {
            activReg.getResult(xmlData);
        } catch (Exception e) {
            // TODO: handle exception
            System.out.println("MESSAGE GET RESULT"+e.getMessage());
        }
   }

MainActivity 中的 getResult 函数看起来像

public void getResult(Currency xmlObj){
   LinearLayout right = new LinearLayout(this);
   Resources resource = getResources();
   ImageView imgUsd = (ImageView) this.findViewById(R.id.usd);
   final Drawable drawable = imgUsd.getDrawable();
   TextView res = new TextView(this);
   imgUsd.setImageDrawable(drawable);
   res.setText("EUR=>USD: "+Double.toString(xmlObj.getCurrency("USD")));
   //res.setText("EUR=>GBP: "+Double.toString(xmlObj.getCurrency("GBP")));
   setContentView(res);

}

现在你可以看到我把所有东西都搞砸了,尝试了很多无论如何都不起作用的东西。所以我想要实现的是有一个xmlObj.getCurrency("USD")xmlObj.getCurrency("GBP") 等列表,旁边有一个小标志图像。当然,如果我不止一个使用 setText,最后一个会覆盖前一个,这就引出了我的两个问题:

  1. 如何在布局中添加更多文本?
  2. 如何添加在主 xml 布局中定义(或未定义)的图像?

供您参考,布局是LinearLayout,我也尝试以这种方式包含图像:

<ImageView 
    android:id="@+id/usd"
    android:layout_width="59dp" 
    android:layout_height="40dp"
    android:layout_gravity="fill_vertical"
    android:src="@drawable/usd" />

在xml中。

【问题讨论】:

    标签: android image text android-asynctask


    【解决方案1】:

    你的代码是这样的:-

      protected void onPostExecute(String result) {
            System.out.println(xmlData.getCurrency("USD"));
            try {
                activReg.getResult(xmlData);
            } catch (Exception e) {
                // TODO: handle exception
                System.out.println("MESSAGE GET RESULT"+e.getMessage());
            }
        }
    

    你是一个一个加载xmlObj还是一起加载所有xmlObj??

    你可以用listview创建一个xml。

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    
        <ListView
            android:id="@+id/listView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_margin="10dp"
            android:divider="@android:color/transparent"
            android:dividerHeight="10dp"
            android:scrollbars="none" >
        </ListView>
    </RelativeLayout>
    

    稍后在你的 getResult 方法中写出来

      View view = LayoutInflater.from(context).inflate(R.layout.list_view, parent, false);
        setContentView(view);
        ArrayList<YourModel> arrayList = new ArrayList<YourModel>();
        for(int i = 0 ; i < xmlObj[].length; i++){
          YourModel model = new YourModel;
          model.setUsd(Double.toString(xmlObj.getCurrency("USD"));
          model.setImageUrl(xmlObj.getString("Url"));
          arrayList.add(model);
        }
        CustomArrayAdapter arrayAdapter = new CustomArrayAdapter(context,R.id.list_item,arrayList);
        ListView view = (ListView) view.findViewById(R.id.listView);
        view.setadapter(arrayAdapter);
    

    YourModel 将是一个单独的 java 类,其中包含图像 url 和文本描述 这将有 getter 和 setter。

    R.id.list_item :-您需要创建另一个 xml 来创建列表行,其中 imageView 和 textview 水平对齐。

    customArrayAdapter 将是您可以找到教程的自定义适配器。

    如果仍有疑问,您可以随时问.. :)

    【讨论】:

      【解决方案2】:

      如果您有多个带有描述的图像,则可以使用列表视图。 你可以关注ListView tutorial

      【讨论】:

      • 我正在关注您发布的教程。我会看看我能从中得到什么。
      • 我确定我遗漏了一些东西,但问题是在从异步作业 onPostExecute() 调用后在 getResult() 中创建此列表。本教程仅显示如何在主要活动中执行此操作。如何使用文本和图标创建此列表视图?
      • 你可以在 onPostExecute() 中调用你的方法 getResult() 你可以在你的方法中膨胀 xml
      • 这对你来说可能很容易,但对我来说不是。你能详细说明一下吗?可能我正在做一些相当复杂的事情来实现可以轻松完成的事情:启动应用程序,异步作业读取并解析 xml,完成后将对象发送到主要活动,该活动应该显示这些数据和一些图片主布局。
      猜你喜欢
      • 1970-01-01
      • 2010-09-29
      • 2023-03-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-23
      • 1970-01-01
      相关资源
      最近更新 更多