我正在使用自定义数组适配器在列表视图中添加自定义列表,但是此处仅添加列表中的最后一项是我的自定义数组适配器
public class CustomCreditsOffer extends ArrayAdapter<Credits> {
private LayoutInflater inflater;
Context context;
Credits items;
ArrayList<Credits> resultList;
public CustomCreditsOffer(Context context, ArrayList<Credits> credits) {
super(context, R.layout.credit_offers_sale, credits);
resultList=credits;
inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
Holder holder;
items=getItem(position);
if(convertView==null){
convertView = inflater.inflate(R.layout.credit_offers_sale,parent,false);
holder=new Holder();
holder.creditsTV = (TextView) convertView
.findViewById(R.id.creditSaleNoOfCreditsTV);
holder.typeTV = (TextView) convertView
.findViewById(R.id.creditTypeTV);
holder.creditsPurchaseBT = (Button) convertView
.findViewById(R.id.creditSalePurchaseBT);
holder.creditsPurchaseBT.setText(items.amount);
holder.creditsTV.setText(items.number);
holder.typeTV.setText(items.title);
}
return convertView;
}
public class Holder {
public TextView creditsTV;
public TextView typeTV;
public Button creditsPurchaseBT;
}
}
以下是我如何向addpater添加项目的代码
@Override
public void onSyncSuccess(String controller, String action, boolean status,
JSONObject jsonObject) {
PostCost post=new PostCost();
// getting Current credits
if (status && action.compareTo("credit") == 0) {
try {
Credits credit=new Credits();
credit.yourBalance= jsonObject.getString("your_balance");
credit.premiumAmount= jsonObject.getString("premium_amount");
credit.isPremium=jsonObject.getString("is_premium_mem");
creditPremiumMemberBT.setText("$"+credit.premiumAmount);
creditsYourCredits.setText(credit.yourBalance);
if(!isPremium.equals("0")){
creditPremiumTV.setText("You are a premium member");
creditPremiumMemberBT.setVisibility(View.GONE);
}
JSONArray adminCredit=jsonObject.getJSONArray("adminCredit");
for(int a=0;a<adminCredit.length();a++){
JSONObject detail=adminCredit.getJSONObject(a);
credit.number=detail.getString("number");
credit.amount=detail.getString("amount");
credit.title=detail.getString("type_title");
credit.key=detail.getString("android_key");
credit.id=detail.getString("id");
credit.typeId=detail.getString("type_id");
creditList.add(credit);
}
}
catch (Exception e) {
}
creditAdapter=new CustomCreditsOffer(getActivity(), creditList);
creditAvailableProductOffersLV.setAdapter(creditAdapter);
}
}
这段代码的作用是将我从服务器响应中获得的最后一项添加到列表中。
答案 0 :(得分:1)
将getView方法更改为:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
Holder holder;
items=getItem(position);
if(convertView==null){
convertView = inflater.inflate(R.layout.credit_offers_sale,parent,false);
holder=new Holder();
holder.creditsTV = (TextView) convertView
.findViewById(R.id.creditSaleNoOfCreditsTV);
holder.typeTV = (TextView) convertView
.findViewById(R.id.creditTypeTV);
holder.creditsPurchaseBT = (Button) convertView
.findViewById(R.id.creditSalePurchaseBT);
convertView.setTag(holder);
}
else
holder = (Holder)convertView.getTag();
holder.creditsPurchaseBT.setText(items.amount);
holder.creditsTV.setText(items.number);
holder.typeTV.setText(items.title);
return convertView;
}
如果您的视图为空,则只需处理您的代码。