我正在尝试编写一个ListView适配器,该适配器将根据我创建View for的数据的属性,根据几个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;
}
}
}
答案 0 :(得分:2)
您可以执行以下操作:
创建(setTag()
)指定布局的类型时,始终为您convertView
附加标记(在您的案例R.layout.list_item_start
或R.layout.list_item_payment
)。
当您检查convertView != null
是否还需要检查convertView.getTag()
指定的类型是否与您要更新的布局相同时,以避免正常的膨胀操作,因为:
convertView
,因为您需要创建正确的布局类型。 您还可以采用ViewHolder pattern。
来改进整个代码答案 1 :(得分:1)
谢谢! 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。我今天才刚刚了解到这一点。
答案 2 :(得分:0)
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;
}
}