ListView 填充了相同的值

Posted

技术标签:

【中文标题】ListView 填充了相同的值【英文标题】:ListView being populated with same values 【发布时间】:2015-12-06 00:22:12 【问题描述】:

我正在使用从 JSONArray 读取的项目填充 ListView,被放置到自定义布局中,并填充 listView,但每当我尝试运行它时,我的 ArrayList 都会填充所有唯一值,但 listView 具有所有相同的值(ArrayList 中的第一个值)。我觉得问题是我没有在我的 getView 中循环遍历列表视图中的每个项目,但我不知道该怎么做。

适配器类

private class adapter extends BaseAdapter 
    Context context;
    ArrayList<PropertyInformationValues> data;
    LayoutInflater inflater;

    public adapter (Context context, ArrayList<PropertyInformationValues> data) 
        this.context = context;
        this.data = data;
        inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    

    @Override
    public int getCount () 
        return data.size();
    

    @Override
    public Object getItem (int position) 
        return data.get(position);
    

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

    @Override
    public View getView (int position, View convertView, ViewGroup parent) 
        View vi = convertView;

        vi = inflater.inflate(R.layout.property_result_item, null);

        TextView address = (TextView) vi.findViewById(R.id.propertyAddress);
        TextView city = (TextView) vi.findViewById(R.id.propertyCity);
        TextView owners = (TextView) vi.findViewById(R.id.propertyOwner);
        TextView apn = (TextView) vi.findViewById(R.id.propertyAPN);

        for (int i = 0; i < data.size(); ++i) 
            address.setText(data.get(i).getAddress1() + " " + data.get(i).getAddress2());
            city.setText(data.get(i).getCity() + ", " + data.get(i).getState() + " " +
                    data.get(i).getZip());
            owners.setText(data.get(i).getOwner());
            apn.setText(data.get(i).getApn());
        

        return vi;
    

创建数组列表

for (int i = 0; i < propertyResults.length(); ++i) 
            JSONObject row = propertyResults.getJSONObject(i);
            String address1, address2, adapterCity, adapterState, adapterZip,
                    adapterOwner, adapterApn;

            address1 = row.getString(TAG_ADDRESS);
            address2 = row.getString(TAG_ADDRESS2);
            adapterCity = row.getString(TAG_CITY);
            adapterState = row.getString(TAG_STATE);
            adapterZip = row.getString(TAG_ZIP);
            adapterOwner = row.getString(TAG_OWNER);
            adapterApn = row.getString(TAG_APN);

            values.add(new PropertyInformationValues(address1, address2, adapterCity,
                    adapterState, adapterZip, adapterOwner, adapterApn));
        

        listView.setAdapter(new adapter(this, values));

property_result_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_
android:layout_>


<TextView
    android:layout_
    android:layout_
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="Large Text"
    android:paddingTop="20sp"
    android:textSize="30sp"
    android:textColor="@color/black"
    android:paddingLeft="20dp"
    android:id="@+id/propertyAddress" />

<TextView
    android:layout_
    android:layout_
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:text="Medium Text"
    android:textSize="22sp"
    android:textColor="@color/black"
    android:paddingLeft="20dp"
    android:id="@+id/propertyCity" />

<TextView
    android:layout_
    android:layout_
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:text="Medium Text"
    android:textSize="22sp"
    android:textColor="@color/black"
    android:paddingLeft="20dp"
    android:id="@+id/propertyOwner" />

<TextView
    android:layout_
    android:layout_
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:text="Medium Text"
    android:textSize="22sp"
    android:textColor="@color/black"
    android:paddingBottom="20dp"
    android:paddingLeft="20dp"
    android:id="@+id/propertyAPN" />

<View
    android:layout_
    android:layout_
    android:background="@android:color/darker_gray"/>
</LinearLayout>

【问题讨论】:

你收集了哪些日志来验证你的理论?对应用程序的行为进行一些基本级别的分析。我首先将日志添加到 getView() 并确认正在创建视图。 【参考方案1】:
@Override
public View getView (int position, View convertView, ViewGroup parent) 
    View vi;
    if (convertView != null) 
        // view was already created, reuse what i have
        vi = convertView;
     else 
        // no view available, create new one
        // always provide parent view, but don't attach to view hierarchy
        vi = inflater.inflate(R.layout.property_result_item, parent, false);
    

    // this is ineffective, google ViewHolder
    TextView address = (TextView) vi.findViewById(R.id.propertyAddress);
    TextView city = (TextView) vi.findViewById(R.id.propertyCity);
    TextView owners = (TextView) vi.findViewById(R.id.propertyOwner);
    TextView apn = (TextView) vi.findViewById(R.id.propertyAPN);

    // only retrieve data associated with currently asked position
    // don't keep calling get() all the time, that's wasteful
    PropertyInformationValues values = data.get(position);

    // assign values
    address.setText(values.getAddress1() + " " + values.getAddress2());
    city.setText(values.getCity() + ", " + values.getState() + " " + values.getZip());
    owners.setText(values.getOwner());
    apn.setText(values.getApn());

    // return loaded view
    return vi;

【讨论】:

【参考方案2】:

嗯嗯,我想我看到了问题。

for (int i = 0; i < data.size(); ++i) 
    address.setText(data.get(i).getAddress1() + " " + data.get(i).getAddress2());
    city.setText(data.get(i).getCity() + ", " + data.get(i).getState() + " " +
                                                     data.get(i).getZip());
    owners.setText(data.get(i).getOwner());
    apn.setText(data.get(i).getApn());

所有这一切都在循环,一遍又一遍地将所有值放入同一个文本视图中,可能会在所有视图中显示最后一个值。它对每个视图都这样做。

我通常喜欢做这样的事情。

@Override
public View getView(int position, View convertView, ViewGroup parent)

    ViewHolder holder;
    PhoneData phoneData = mPhoneDatas.get(position);

    if (convertView == null) 
        convertView = mInflater.inflate(R.layout.phone_list_item, null);

        holder = new ViewHolder();

        holder.tvPhone = (TextView) convertView.findViewById(R.id.textViewRosterPhone);
        holder.llPhone = (LinearLayout) convertView.findViewById(R.id.linearLayoutRosterPhone);

        convertView.setTag(holder);
     else 
        holder = (ViewHolder) convertView.getTag();
    

    holder.tvPhone.setText(phoneData.getNum() + " " + phoneData.getType());
    holder.llPhone.setTag(Integer.valueOf(position));

    return convertView;


static class ViewHolder

    TextView tvPhone;
    LinearLayout llPhone;

问题本质上是您试图循环列表视图,即使它已经自然地做到了。

【讨论】:

发布后我才意识到这一点。我基本上去掉了 for 循环并将所有 data.get(i) 替换为 data.get(position)

以上是关于ListView 填充了相同的值的主要内容,如果未能解决你的问题,请参考以下文章

如何使用特定值填充ListView?

Listview 没有被填充,getView() 没有被调用

ListView 始终具有相同的意图

如何检索名称 | Listview中多个选中项目的值对?

用 Kumulos 填充 ListView?

未填充 ListView。 ViewPager 和片段的问题