适配器未显示结果

时间:2013-04-11 06:14:30

标签: android json adapter android-arrayadapter

我有一个json响应,我已经创建了一个适配器来将json列入视图,应用程序中没有错误,但它也没有显示结果。这是我的适配器类:

  package com.JustDine.Infrastructure;

  import java.util.ArrayList;
  import java.util.List;

  import android.content.Context;
  import android.view.LayoutInflater;
  import android.view.View;
  import android.view.ViewGroup;
  import android.widget.ArrayAdapter;
  import android.widget.Filter;
  import android.widget.ImageView;
  import android.widget.LinearLayout;
  import android.widget.TextView;

  import com.JustDine.GUI.R;
  import com.JustDine.JSON.Response.RedemptionResult;
  import com.JustDine.JSON.Response.SearchResult;

  public class RedemptionListAdapter extends ArrayAdapter<RedemptionResult> {
Context context;

private List<RedemptionResult> redemptionResultList = new ArrayList<RedemptionResult>();
//public ArrayList<RedemptionResult> filtered = new ArrayList<RedemptionResult>();
// public boolean isFiltered;
int position;
int textViewResourceId;
ImageLoader imageLoader;

public RedemptionListAdapter(Context context, int textViewResourceId,
        List<RedemptionResult> redemptionResultList) {
    super(context, textViewResourceId, redemptionResultList);
    this.context = context;
    this.redemptionResultList = redemptionResultList;
    this.textViewResourceId = textViewResourceId;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View v = convertView;
    this.position = position;
    LinearLayout redemptionView;
    RedemptionResult redemptionResult = null;

    redemptionResult = redemptionResultList.get(position);



    if (v == null) {
        redemptionView = new LinearLayout(getContext());
        LayoutInflater vi = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v = vi.inflate(textViewResourceId, redemptionView, true);
    }


    TextView tvName = (TextView) v.findViewById(R.id.resName);

    tvName.setText(redemptionResult.getName());


    return v;

}
 }

请帮忙,为什么没有显示结果。

更新:Json响应类

 package com.JustDine.JSON.Response;

 import java.util.List;

 import com.google.gson.annotations.SerializedName;

 public class RedemptionResultResponse {

@SerializedName("Success")
public boolean Success;

@SerializedName("redemption_partners")
private List<RedemptionResult> getRedemptionPartners;

@SerializedName("denominations")
private List<DenominationsResult> getDenominations;

@SerializedName("errorMessage")
public String errorMessage;

@SerializedName("Tag")
public String Tag;




public List<RedemptionResult> getRedemptionPartners() {
    return getRedemptionPartners;
}

public List<DenominationsResult> getDenominations() {
    return getDenominations;
}

public void setRedemptionPartners(List<RedemptionResult> getRedemptionPartners) {
    this.getRedemptionPartners = getRedemptionPartners;
}

public void setDenominationss(List<DenominationsResult> getDenominations) {
    this.getDenominations = getDenominations;
}

   }

  package com.JustDine.JSON.Response;

  import com.google.gson.annotations.SerializedName;

  public class RedemptionResult {
@SerializedName("id")
private String id;

@SerializedName("name")
private String name;


public String getId() {
    return id;
}

public void setId(String id) {
    this.id = id;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

}

3 个答案:

答案 0 :(得分:0)

请尝试在适配器的构造函数中查看列表的大小。可能是列表大小为0的情况。此外,我没有看到任何使用

private List<RedemptionResult> redemptionResultList = new ArrayList<RedemptionResult>();

正在构造函数中设置redemptionResultList。所以只需将上面的行设为null然后尝试。

希望这可以帮助你。

答案 1 :(得分:0)

除了代码中可能存在的其他问题之外,您还需要在适配器中正确覆盖以下方法。检查它们是否未被覆盖。

您发布的代码似乎没有覆盖它们。

public int getCount(){
    return vector.size();
}

public Object getItem(int position) {

    return position;
}

public long getItemId(int position) {

    return position;
}

答案 2 :(得分:0)

private ArrayList<ExploreCatgModel> arrayList = new ArrayList<ExploreCatgModel>();
private ExploreCatgModel model;

将数据设置为Model类

@Override
        protected String doInBackground(String... params) {
            webService = new WebService();
            String result = webService.aboutList();
            if (result != null) {
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    JSONArray jsonArray = jsonObject
                            .getJSONArray("About");
                    for (int i = 0; i < jsonArray.length(); i++) {
                        JSONObject explrObject = jsonArray.getJSONObject(i);
                        model = new ExploreCatgModel();
                        model.setMemberID(explrObject.getString("iAboutID"));
                        model.setBuissnessName(explrObject
                                .getString("vAboutTitle"));
                        model.setBusinessDescription(explrObject
                                .getString("vAboutDescription"));
                        model.setImage1(explrObject.getString("vImage1"));
                        model.setImage2(explrObject.getString("vImage2"));
                        model.setImage3(explrObject.getString("vImage3"));
                        model.setImage4(explrObject.getString("vImage4"));
                        model.setImage5(explrObject.getString("vImage5"));
                        arrayList.add(model);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            return result;
        }

在litview中设置适配器。

AboutAdapter aboutAdapter = new AboutAdapter(AboutActivity.this,arrayList);                     listView.setAdapter(aboutAdapter);

关于Adapter calss

 public class AboutAdapter extends BaseAdapter {
        private LayoutInflater inflater;
        private Context mcontext;
        private ArrayList<RedemptionResult> arrayList;

        public AboutAdapter(Context context, ArrayList<RedemptionResult> arrayList) {
            this.mcontext = context;
            this.arrayList= arrayList;
            inflater = LayoutInflater.from(mcontext);
        }

        @Override
        public int getCount() {
            return arrayList.size();
        }

        @Override
        public Object getItem(int position) {
            return position;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        public class Holder {
            TextView txtname;

        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View vi = convertView;
            Holder holder;
            if (vi == null) {
                vi = inflater.inflate(R.layout.raw_aboutlist, null);
                holder = new Holder();

                holder.txtname = (TextView) vi
                        .findViewById(R.id.raw_about_txt_name);


                vi.setTag(holder);

            } else {
                holder = (Holder) vi.getTag();
            }

                holder.txtname.setText(arrayList.get(position).getName());

            return vi;
        }

    }