Search code examples
androidcustom-lists

How to add custom List on a list View


I am Using a custom Array adapter to add custom list on a list view but its adding only the last item in list here is my Custom array adapter

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;

}

}

and here is the code how i am adding items to 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);
    }
    }

What this code does is add the last item i am getting from the response from server to the list.


Solution

  • change getView method to:

    @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;
    }
    

    with your code you just handle if your view is null.