首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Android\Eclipse for-each循环

Android\Eclipse for-each循环
EN

Stack Overflow用户
提问于 2014-04-09 16:48:52
回答 3查看 1.5K关注 0票数 0

这是我的代码Android提取的java类和完整的XML文件:

代码语言:javascript
运行
复制
            for (int i = 0; i < tokens.length; i++) {
                Log.i("myApp0", tokens[0].toString());
                Log.i("myApp1", tokens[1].toString());
                Log.i("myApp2", tokens[2].toString());

                profileImage.setImageUrl(tokens[0].toString());
                textData.setText(tokens[1].toString());
                textTitle.setText(Html.fromHtml(tokens[2].toString()));
                textTitle.setMovementMethod(LinkMovementMethod.getInstance());
            }

布局XML

代码语言:javascript
运行
复制
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <com.loopj.android.image.SmartImageView
        android:id="@+id/profileImage"
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:layout_margin="5dp"
        android:background="#444"
        android:padding="3dp" 
        android:scaleType="fitCenter" />

    <TextView
        android:id="@+id/textData"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textSize="18sp"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/textTitle"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dip"
        android:linksClickable="true"
        android:text="@string/hello"
        android:textSize="18sp"
        android:textStyle="bold"        
        android:maxLines="1"
        android:textColor="@android:color/black" />

</LinearLayout>

我认为每一行的输出都有每个相册的日期和标题,但是我对每一行都有相同的相册,在android布局中,我只有第一次约会和相册.为什么?

代码语言:javascript
运行
复制
04-09 18:38:11.088: I/myApp0(28558): https://Link1
04-09 18:38:11.088: I/myApp1(28558): 09/04/2013
04-09 18:38:11.088: I/myApp2(28558): <a href=http://Link1>Pics1</a>
04-09 18:38:11.178: I/myApp0(28558): https://Link1
04-09 18:38:11.178: I/myApp1(28558): 09/04/2013
04-09 18:38:11.178: I/myApp2(28558): <a href=http://Link1>Pics1</a>
04-09 18:38:11.188: I/myApp0(28558): https://Link1
04-09 18:38:11.188: I/myApp1(28558): 09/04/2013
04-09 18:38:11.188: I/myApp2(28558): <a href=http://Link1>Pics1</a>
04-09 18:38:11.188: I/myApp0(28558): https://Link1
04-09 18:38:11.198: I/myApp1(28558): 09/04/2013
04-09 18:38:11.198: I/myApp2(28558): <a href=http://Link1>Pics1</a>
04-09 18:38:11.198: I/myApp0(28558): https://Link1
04-09 18:38:11.208: I/myApp1(28558): 09/04/2013
04-09 18:38:11.208: I/myApp2(28558): <a href=http://Link1>Pics1</a>
04-09 18:38:11.208: I/myApp0(28558): https://Link1
04-09 18:38:11.208: I/myApp1(28558): 09/04/2013
04-09 18:38:11.208: I/myApp2(28558): <a href=http://Link1>Pics1</a>
04-09 18:38:11.218: I/myApp0(28558): https://Link1
04-09 18:38:11.218: I/myApp1(28558): 09/04/2013
04-09 18:38:11.218: I/myApp2(28558): <a href=http://Link1>Pics1</a>
04-09 18:38:11.228: I/myApp0(28558): https://Link1
04-09 18:38:11.228: I/myApp1(28558): 09/04/2013
04-09 18:38:11.228: I/myApp2(28558): <a href=http://Link1>Pics1</a>
04-09 18:38:11.228: I/myApp0(28558): https://Link1
04-09 18:38:11.228: I/myApp1(28558): 09/04/2013
04-09 18:38:11.228: I/myApp2(28558): <a href=http://Link1>Pics1</a>
04-09 18:38:11.238: I/myApp0(28558): https://Link1
04-09 18:38:11.238: I/myApp1(28558): 09/04/2013
04-09 18:38:11.238: I/myApp2(28558): <a href=http://Link1>Pics1</a>
04-09 18:38:11.248: I/myApp0(28558): https://Link1
04-09 18:38:11.248: I/myApp1(28558): 09/04/2013
04-09 18:38:11.248: I/myApp2(28558): <a href=http://Link1>Pics1</a>
04-09 18:38:11.248: I/myApp0(28558): https://Link1
04-09 18:38:11.248: I/myApp1(28558): 09/04/2013
04-09 18:38:11.248: I/myApp2(28558): <a href=http://Link1>Pics1</a>
04-09 18:38:11.258: I/myApp0(28558): https://Link1
04-09 18:38:11.258: I/myApp1(28558): 09/04/2013
04-09 18:38:11.258: I/myApp2(28558): <a href=http://Link1>Pics1</a>
04-09 18:38:11.268: I/myApp0(28558): https://Link1
04-09 18:38:11.268: I/myApp1(28558): 09/04/2013
04-09 18:38:11.268: I/myApp2(28558): <a href=http://Link1>Pics1</a>
04-09 18:38:11.288: I/myApp0(28558): https://Link1
04-09 18:38:11.288: I/myApp1(28558): 09/04/2013
04-09 18:38:11.288: I/myApp2(28558): <a href=http://Link1>Pics1</a>
04-09 18:38:11.298: I/myApp0(28558): https://Link1
04-09 18:38:11.298: I/myApp1(28558): 09/04/2013
04-09 18:38:11.298: I/myApp2(28558): <a href=http://Link1>Pics1</a>
04-09 18:38:11.298: I/myApp0(28558): https://Link1
04-09 18:38:11.298: I/myApp1(28558): 09/04/2013
04-09 18:38:11.298: I/myApp2(28558): <a href=http://Link1>Pics1</a>
04-09 18:38:11.308: I/myApp0(28558): https://Link1
04-09 18:38:11.308: I/myApp1(28558): 09/04/2013
04-09 18:38:11.308: I/myApp2(28558): <a href=http://Link1>Pics1</a>
04-09 18:38:11.318: I/myApp0(28558): https://Link1
04-09 18:38:11.318: I/myApp1(28558): 09/04/2013
04-09 18:38:11.318: I/myApp2(28558): <a href=http://Link1>Pics1</a>
EN

回答 3

Stack Overflow用户

发布于 2014-04-09 16:52:47

我想是因为你有这个:

代码语言:javascript
运行
复制
         Log.i("myApp0", tokens[0].toString());
            Log.i("myApp1", tokens[1].toString());
            Log.i("myApp2", tokens[2].toString());

你不用我的迭代器..。你用的常数是0,1,2,所以它总是输出相同的值。

票数 0
EN

Stack Overflow用户

发布于 2014-04-09 16:53:41

您没有使用i变量。

让我们看看您的代码:

代码语言:javascript
运行
复制
 for (int i = 0; i < tokens.length; i++) {
            Log.i("myApp0", tokens[0].toString()); // is going to print always the same thing no matter how many rows this has
            Log.i("myApp1", tokens[1].toString());
            Log.i("myApp2", tokens[2].toString());

            profileImage.setImageUrl(tokens[0].toString()); //is always going to set the same image url
            textData.setText(tokens[1].toString());
            textTitle.setText(Html.fromHtml(tokens[2].toString()));
            textTitle.setMovementMethod(LinkMovementMethod.getInstance());
        }

您确定应该使用字符串数组吗?最好的方法是使用具有以下元素的对象:

代码语言:javascript
运行
复制
public class Your_Obj {
    String date, profile_img, title;

//here you implement your object and generate the getters and setters
}

并使用这样的对象列表:而不是使用令牌数组,您应该使用:

代码语言:javascript
运行
复制
List<Your_obj> listYourObjects= getMyObj();

for(Your_Obj obj in listYourObjects){ 

                profileImage.setImageUrl(obj.getProfile_img);
                textData.setText(obj.getDate);
                textTitle.setText(obj.getTitle));
                textTitle.setMovementMethod(LinkMovementMethod.getInstance());

}
票数 0
EN

Stack Overflow用户

发布于 2014-04-09 16:56:27

尝尝这个

代码语言:javascript
运行
复制
for (int i = 0; i < tokens.length/3; i++) {
            Log.i("myApp0", tokens[i*3 + 0].toString());
            Log.i("myApp1", tokens[i*3 + 1].toString());
            Log.i("myApp2", tokens[i*3 + 2].toString());

            profileImage.setImageUrl(tokens[i*3 + 0].toString());
            textData.setText(tokens[i*3 + 1].toString());
            textTitle.setText(Html.fromHtml(tokens[i*3 + 2].toString()));
            textTitle.setMovementMethod(LinkMovementMethod.getInstance());
        }

令牌应该是Array of Object,而不是这个。但是,您可以使用这种技术实现您想要的输出。

要用自己的View显示每条记录,您必须创建ListView,如本教程所示

代码语言:javascript
运行
复制
public class ImageListAdapter extends BaseAdapter {

       private LayoutInflater mLayoutInflater; 
       private ArrayList<String> yourData = new ArrayList<Entry>();

       public ImageListAdapter(Context context) { 
          mLayoutInflater = (LayoutInflater) context
                   .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
       }

       @Override
       public int getCount() {
          return yourData.size()/3; // Specifically for your data -- Not recommended
       }

       @Override
       public Object getItem(int position) {
          return yourData.get(position);
       }

       @Override
       public long getItemId(int position) {
          return position;
       }

       @Override
       public View getView(int position, View convertView,
             ViewGroup parent) {                                        
          RelativeLayout itemView;
          if (convertView == null) {                                      
             itemView = (RelativeLayout) mLayoutInflater.inflate(
                      R.layout.list_item, parent, false);

          } else {
             itemView = (RelativeLayout) convertView;
          }

          ImageView profileImage = (ImageView) itemView.findViewById(R.id.listImage);                       
          TextView textTitle = (TextView) itemView.findViewById(R.id.listTitle);                      
          TextView textData = (TextView) itemView.findViewById(R.id.listDescription);                  

            profileImage.setImageUrl(tokens[i*3 + 0].toString());
            textData.setText(tokens[i*3 + 1].toString());
            textTitle.setText(Html.fromHtml(tokens[i*3 + 2].toString()));
            textTitle.setMovementMethod(LinkMovementMethod.getInstance());

          return itemView;
       }

       public void upDateEntries(ArrayList<String> entries) {
          yourData = entries;
          notifyDataSetChanged();
       }
}

使用Adapter将此ListView设置为ListView.setAdapter(/* ImageListAdapter instance */);

启发

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22968990

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档