Listview有两种不同的布局

时间:2011-05-26 21:20:55

标签: android listview layout

在listview中滚动几秒后,应用程序崩溃了。

 String[] names = new String[] {"Cook","2/Person/15/5","Cashier","2/Person/15/5"};
 @Override
 public View getView(int position, View convertView, ViewGroup parent) {


        ViewHolder holder;
        View rowView = convertView;
        LayoutInflater inflater =(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        String[] info = names.get(position).split("/");

        if (rowView == null) {

                if (info.length != 1) {
                    rowView = inflater.inflate(R.layout.employee, null, true);
                    holder = new ViewHolder();
                    holder.name = (TextView) rowView.findViewById(R.id.name);
                    holder.money= (TextView) rowView.findViewById(R.id.money);
                    holder.rating = (RatingBar) rowView.findViewById(R.id.rating);
                    rowView.setTag(holder);
                }else{
                    rowView = inflater.inflate(R.layout.lable, null, true);
                    holder = new ViewHolder();
                    holder.lable = (TextView) rowView.findViewById(R.id.name);

                    rowView.setTag(holder);
                }


            }

        holder = (ViewHolder) rowView.getTag();


        if (holder.lable==null) {
            holder.name.setText(info[1]);
            holder.money.setText(info[2]);
            holder.rating.setRating(Float.valueOf("1"));
        }else{
            holder.lable.setText(names.get(position));
        }

        //}



        return rowView;
    }

3 个答案:

答案 0 :(得分:3)

由于您使用的是Viewholder模式和循环视图,因此最终会出现混乱。 这是因为您的列表中有两种不同类型的视图,当您获得转换视图时,它将是其中一种视图。

想象一个带有布局/视图a的数据集A,对于B b

也是如此

( - B-表示它尚未显示在屏幕上) 因此,如果您的初始列表是Aa,Bb,Bb,Aa,-B-,-B-并且您滚动以将数据B带入列表..那么您将获得具有布局的转换视图或OR布局b,因此无法可靠地使用convertview。

我正在寻找一种方法来支持ListView中的不同视图,同时使用convertView / viewHolder-pattern ..到目前为止,我发现这个有趣:https://github.com/commonsguy/cwac-merge#readme < / p>

您要做的是覆盖getItemViewType(位置)。 这是一个例子。你当然有你的dataTypes而不是MoreResultsLoader和YPContact。

@Override
public int getItemViewType(int position) {

    if (resultsList.get(position) instanceof MoreResultsLoader) {
        return VIEW_TYPE_MORE_RESULTS_LOADER;
    }
    if (resultsList.get(position) instanceof YPContact) {
        YPContact ypCon = (YPContact) resultsList.get(position);
        if(checkForGold(ypCon))
            return VIEW_TYPE_YPCONTACT_GOLD;
        else
            return VIEW_TYPE_YPCONTACT_REG;
    }
  }

然后在getView中,您需要检查要处理的视图类型,并使用正确的ViewHolder类进行膨胀/填充。

public View getView(final int position, View convertView, ViewGroup parent) {

    View ourView = convertView;
    int itemViewType = getItemViewType(position);
    switch (itemViewType) {
    case VIEW_TYPE_MORE_RESULTS_LOADER:
        MoreResultsViewHolder moreResVH = null;
        if (ourView == null) {

            ourView = layoutInflator.inflate(R.layout.load_more_items,
                    null, true);
            moreResVH = new MoreResultsViewHolder(ourView);
            ourView.setTag(moreResVH);

        } else {
            moreResVH = (MoreResultsViewHolder) ourView.getTag();
        }
        if (moreResVH != null) {
            moreResVH.populate((MoreResultsLoader) resultsList
                    .get(position), context);
        }
        return ourView;
    case VIEW_TYPE_YPCONTACT_GOLD:
        ContactViewHolderGold contactVHGold = null;
        if (ourView == null) {

            ourView = layoutInflator.inflate(
                    R.layout.yp_search_result_list_item_gold, null, true);
            contactVHGold = new ContactViewHolderGold(ourView);
            ourView.setTag(contactVHGold);

        } else {
            contactVHGold = (ContactViewHolderGold) ourView.getTag();
        }
        if (contactVHGold != null) {
            final YPContact ypContact = (YPContact) resultsList
                    .get(position);
            contactVHGold.populate(ypContact, mCurrentLocation,
                    ypSearchResultsActivity);
            // moreResVH.populate(
            // (MoreResultsLoader)resultsList.get(position),context);
        }
        return ourView;

答案 1 :(得分:0)

Android在ddms透视图中有一个名为logcat的调试工具,您可以在其中找到日志。您将能够准确地找出代码崩溃的行号。

当所有列表项均匀时,主要使用convertView。因为它有助于回收意见。在这里,我们不确定循环视图是否是R.layout.employee或R.layout.lable。这可能会导致问题。

再次猜测。 logcat将帮助您缩小问题范围。

答案 2 :(得分:0)

查看第一项"Cook"

String[] names = new String[] {"Cook","2/Person/15/5","Cashier","2/Person/15/5"};

你在做:

String[] info = names.get(position).split("/");

然后:

holder.name.setText(info[1]);
holder.money.setText(info[2]);

我不必查看logcat以查看ArrayIndexOutOfBoundsException