【问题标题】:multiple xml views with convertView android带有convertView android的多个xml视图
【发布时间】:2015-02-17 18:15:55
【问题描述】:

我正在尝试编写一个 ListView 适配器,该适配器将根据我为其创建视图的数据的属性来传递基于几个 xml 文件之一的视图。我的代码工作正常,除非我尝试使用 convertView 来加快进程。无论我如何尝试使用它,我要么崩溃我的程序,要么得到奇怪的输出。我理解为什么这对 convertView 来说是个问题,但我强烈怀疑我仍然可以完成这项工作。谁能告诉我如何修复我的代码? (现在我使用 convertView 作为我返回的视图的名称,即使我没有 'if (convertView==null)' 例程正常工作)

public class PaymentListFragment extends ListFragment {

private ArrayList<Payment> mPayments;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getActivity().setTitle(R.string.payment_schedule_title);
    mPayments = PaymentSchedule.get(getActivity()).getPayments();
    PaymentAdapter adapter = new PaymentAdapter(mPayments);
    setListAdapter(adapter);
}


private class PaymentAdapter extends ArrayAdapter<Payment> {

    public PaymentAdapter(ArrayList<Payment> payments) {
        super(getActivity(), 0, payments);
    }

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

        Payment p = mPayments.get(position);
        //normal
        //if (convertView == null) {
        //cant work out how to use convertview conditional without screwing up project
        //it would speed it up to fix this however
        if (p.type == Payment.TYPE_START) {


            convertView = getActivity().getLayoutInflater().inflate(R.layout.list_item_start, null);

            TextView remainDisplay =
                    (TextView) convertView.findViewById(R.id.remainDisplayStart);
            remainDisplay.setText(p.getRemainString());
            TextView paymentDate =
                    (TextView) convertView.findViewById(R.id.paymentDateStart);
            paymentDate.setText(p.getDateString());
            TextView paymentDisplay =
                    (TextView) convertView.findViewById(R.id.paymentDisplayStart);
            paymentDisplay.setText(p.getDefaultPaymentString());
            TextView aprDisplay =
                    (TextView) convertView.findViewById(R.id.aprDisplayStart);
            aprDisplay.setText(p.getInterestRate() * 1200 + "%");
        } else {
             //if (convertView == null) {
                convertView = getActivity().getLayoutInflater().inflate(R.layout.list_item_payment, null);
            //}
            TextView paymentDate =
                    (TextView) convertView.findViewById(R.id.paymentDate);
            paymentDate.setText(p.getDateString());
            TextView paymentDisplay =
                    (TextView) convertView.findViewById(R.id.paymentDisplay);
            paymentDisplay.setText(p.getPaymentString());
            TextView principalDisplay =
                    (TextView) convertView.findViewById(R.id.principalDisplay);
            principalDisplay.setText(p.getPrincipalString());
            TextView interestDisplay =
                    (TextView) convertView.findViewById(R.id.interestDisplay);
            interestDisplay.setText(p.getInterestString());
            TextView remainDisplay =
                    (TextView) convertView.findViewById(R.id.remainDisplay);
            remainDisplay.setText(p.getRemainString());
        }
        //}
        return convertView;
    }
}
}

【问题讨论】:

  • 您是否尝试过将getItemViewType()getViewTypeCount()ViewHolder 一起使用而不是getView() 中的代码?
  • 我正要写@anil 提到的内容——你可能想在这里查看这个问题stackoverflow.com/questions/4777272/…
  • 看起来是基于位置的。随着我的继续,我希望能够根据错过的付款、额外付款、罚款等添加其他类型,并让它们按时间顺序出现在列表中。我将尝试 bonnyz 使用标签的方法。我之前试图实现引用数据的 if 语句,但无法弄清楚如何将它们链接到 convertView。

标签: android listview adapter convertview


【解决方案1】:

您可以使用以下技巧:

  1. 在创建时始终将 标签 附加到您 convertView (setTag()),指定布局的 type(在您的情况下为 @987654325 @ 或 R.layout.list_item_payment)。

  2. 当您检查convertView != null 时,您还需要检查convertView.getTag() 指定的类型是否与您正在更新的布局相同,以避免正确地进行膨胀操作,因为:

    • 如果类型不同,您需要使用新的膨胀操作覆盖convertView,因为您需要创建正确的布局类型。
    • 如果类型相等,您可以简单地更新布局而无需膨胀操作

您还可以通过采用ViewHolder pattern来改进整个代码。

【讨论】:

    【解决方案2】:

    谢谢! Bonnyz 的建议奏效了。我添加了以下代码:

    Payment p = mPayments.get(position);
    
            if (convertView == null || convertView.getTag() != p.type) {
                if(p.type == Payment.TYPE_START) {
                    convertView = getActivity().getLayoutInflater().inflate(R.layout.list_item_start, null);
                    convertView.setTag(Payment.TYPE_START);
                } else{
                    convertView = getActivity().getLayoutInflater().inflate(R.layout.list_item_payment, null);
                    convertView.setTag(Payment.TYPE_PAYMENT);
                }
    
            }
    

    我还将尝试使用 ViewHolder。我今天才知道。

    【讨论】:

    • 对我帮助很大。谢谢@Thomas。
    【解决方案3】:
    public class CustomAdapter extends BaseAdapter {
    String[] frName;
    String[] lrName;
    String[] myLocation;
    Context context;
    int[] imageId;
    private static LayoutInflater inflater = null;
    
    
    public CustomAdapter(MainActivity mainActivity, String[] fstName, String[] lstName, String[] location, int[] prgmImages) {
        super();
        context = mainActivity;
        frName = fstName;
        lrName = lstName;
        myLocation = location;
        imageId = prgmImages;
        inflater = (LayoutInflater) context.
                getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }
    
    
    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return 12;
    
    }
    
    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return position;
    }
    
    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }
    
    public class Holder {
        TextView tv_frname, tv_lrname, tv_location, tv_fstname, tv_lstname;
        ImageView img;
    
    }
    
    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        Holder holder = new Holder();
        if (position < frName.length) {
            convertView = inflater.inflate(R.layout.myadapterdesin, null);
            holder.tv_frname = (TextView) convertView.findViewById(R.id.frname);
            holder.tv_lrname = (TextView) convertView.findViewById(R.id.lrname);
            holder.tv_location = (TextView) convertView.findViewById(R.id.place);
            holder.tv_frname.setText(frName[position]);
            holder.tv_lrname.setText(lrName[position]);
            holder.tv_location.setText(myLocation[position]);
            convertView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    Toast.makeText(context, "You Clicked " + position, Toast.LENGTH_SHORT).show();
                }
            });
            return convertView;
    
        } else if (position >= frName.length) {
            convertView = inflater.inflate(R.layout.mylist, null);
            holder.img = (ImageView) convertView.findViewById(R.id.img_view);
            holder.tv_fstname = (TextView) convertView.findViewById(R.id.fstname);
            holder.tv_lstname = (TextView) convertView.findViewById(R.id.lstname);
            holder.tv_fstname.setText(frName[(position) - (frName.length)]);
            holder.tv_lstname.setText(lrName[(position) - (frName.length)]);
            holder.img.setImageResource(imageId[(position) - (frName.length)]);
    
            convertView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    Toast.makeText(context, "You Clicked " + position, Toast.LENGTH_SHORT).show();
                }
            });
            return convertView;
        }
        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Toast.makeText(context, "You Clicked " + position, Toast.LENGTH_SHORT).show();
            }
        });
        return convertView;
    
    }
    

    }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-11
      • 2010-11-27
      • 2011-07-28
      • 1970-01-01
      相关资源
      最近更新 更多