本文介绍了创建的TextView的在Android的清单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想以我的名义的和组织的Andr​​oid应用程序创建的TextView的列表。然而,整个列表是相同的,例如:

I am trying to create a list of TextView's in my Android app of name's and organizations. However, the whole list is the same, for example:

Joe's work for Construction Co.
15hrs of 60hrs
Joe's work for Construction Co.
15hrs of 60hrs
Joe's work for Construction Co.
15hrs of 60hrs
...

重复五次在应用程序列表中。下面是相关code:

is repeated five times in the list in the app. Here is the relevant code:

class IconicAdapter extends ArrayAdapter<Long> {
private ArrayList<Long> items;
private FakeIDO ido;

public IconicAdapter(Context context,int textViewResourceId, ArrayList<Long> ids, FakeIDO ido){
    super(WorkList.this, R.layout.feed_list, ids);
    this.items = ids;
    this.ido = ido;
}


public View getView(int position, View convertView, ViewGroup parent) {
    View v = convertView;
    if (v == null) {
        LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v = vi.inflate(R.layout.row, null);
    }
    for(long id : items){ 
        TextView tt = (TextView) v.findViewById(R.id.toptext);
        TextView bt = (TextView) v.findViewById(R.id.bottomtext);
        if (tt != null) {
              tt.setText(ido.getName(id) + "'s work for " + ido.getOrganization(id));
        }
        if(bt != null){
              bt.setText( ido.totalWorked(id) + "hrs of " + ido.estimatedHours(id) + "hrs");
        }
    }
    return v;
}

下面是XML针对这一类工作断:

Here is the xml view this class is working off of:

<?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="?android:attr/listPreferredItemHeight"
android:padding="6dip">
<ImageView
    android:id="@+id/icon"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:layout_marginRight="6dip"
    android:src="@drawable/icon" />
<LinearLayout
    android:orientation="vertical"
    android:layout_width="0dip"
    android:layout_weight="1"
    android:layout_height="fill_parent">
    <TextView
        android:id="@+id/toptext"
        android:layout_width="fill_parent"
        android:layout_height="0dip"
        android:layout_weight="1"
        android:gravity="center_vertical"
    />
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="0dip"
        android:layout_weight="1" 
        android:id="@+id/bottomtext"
        android:singleLine="true"
        android:ellipsize="marquee"
    />
</LinearLayout>

现在,我明白,当我打电话同一个实例被退回的TextView TT =(TextView中)v.findViewById(R.id.toptext);,但我不知道是什么改变来获得一个新的例如每次对象。我缺少什么?

Now, I understand that the same instance is being returned when I call "TextView tt = (TextView) v.findViewById(R.id.toptext);", but I don't know what to change to get a new instance object each time. What am I missing?

推荐答案

使用 getView 像这样

   public View getView(int position, View convertView, ViewGroup parent) {
    View v = convertView;
    if (v == null) {
        LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v = vi.inflate(R.layout.row, null);
    }

        TextView tt = (TextView) v.findViewById(R.id.toptext);
        TextView bt = (TextView) v.findViewById(R.id.bottomtext);
        // get the 'id' for position.
        int id = items.get(position);
        if (tt != null) {
              tt.setText(ido.getName(id) + "'s work for " + ido.getOrganization(id));
        }
        if(bt != null){
              bt.setText( ido.totalWorked(id) + "hrs of " + ido.estimatedHours(id) + "hrs");
        }

    return v;
}

这篇关于创建的TextView的在Android的清单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-31 11:48