无法在列表视图中查看内容

时间:2016-04-03 15:45:08

标签: android json listview android-fragments

我在片段中使用列表视图,使用json加载数据,因为我无法查看列表视图。在sched中显示构造函数从未使用过我不知道我在哪里做错了代码。服务器部分工作正常。

片段:

import { Component, ChangeDetectorRef } from 'angular2/core';

constructor(private cdr:ChangeDetectorRef) {
}
public setValue(a,b,c) {
    this.coutnry = a;
    this.state = b;
    this.city = c;
    this.sharedService.country = this.coutnry;
    this.sharedService.city = this.city;
    this.sharedService.state = this.state;
    console.log(a, b, c);
    this.cdr.detectChanges();
}

ListAdapter:

public class SchedulessFragment extends Fragment{
private static final String TAG = MatAct.class.getSimpleName();
private static final String url = "http://nammakovai2015-001-site1.1tempurl.com/iplspin/schedules.php";
ProgressDialog pDialog;
List<Sched> movieList = new ArrayList<Sched>();
ListView listview;
CustomListAdapter adapter;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.schedule_frag, container, false);
    adapter = new CustomListAdapter(getActivity(), movieList);
    listview = (ListView) v.findViewById(R.id.listView1);

    listview.setAdapter(adapter);
    //  TextView tv = (TextView) v.findViewById(R.id.tvfrag);
    //    tv.setText(getArguments().getString("msg"));
    //   tv.setText("Hello");
    pDialog = new ProgressDialog(getActivity());
    // Showing progress dialog before making http request
    pDialog.setMessage("Loading...");
    pDialog.show();

    // changing action bar color


    // Creating volley request obj
    JsonArrayRequest movieReq = new JsonArrayRequest(url,
            new Response.Listener<JSONArray>() {
                @Override
                public void onResponse(JSONArray response) {
                    Log.d(TAG, response.toString());
                    hidePDialog();

                    // Parsing json
                    for (int i = 0; i < response.length(); i++) {
                        try {

                            JSONObject obj = response.getJSONObject(i);
                            Sched m = new Sched();
                            m.setTeama(obj.getString("teama"));

                            m.setTeamb(obj.getString("teamb"));
                            m.setTdate(obj.getString("tdate"));
                            m.setTtime(obj.getString("ttime"));
                           m.setThumbnailUrl(obj.getString("thumbnailUrl"));
                            m.setTeambthumbnailUrl(obj.getString("teambthumbnailUrl"));
                            m.setVenue(obj.getString("venue"));


                            // adding movie to movies array
                            movieList.add(m);

                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                    }

                    // notifying list adapter about data changes
                    // so that it renders the list view with updated data
                    adapter.notifyDataSetChanged();
                }
            }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            VolleyLog.d(TAG, "Error: " + error.getMessage());
            hidePDialog();

        }
    });

    // Adding request to request queue
    AppController.getInstance().addToRequestQueue(movieReq);

    return v;
}
@Override
public void onDestroy() {
    super.onDestroy();
    hidePDialog();
}

private void hidePDialog() {
    if (pDialog != null) {
        pDialog.dismiss();
        pDialog = null;
    }
}
public static SchedulessFragment newInstance(String text) {

    SchedulessFragment f = new SchedulessFragment();
    Bundle b = new Bundle();
    b.putString("msg", text);

    f.setArguments(b);

    return f;
}}

附表:

public class CustomListAdapter extends BaseAdapter{
private Activity activity;
private LayoutInflater inflater;
 List<Sched> movieItems;
ImageLoader imageLoader = AppController.getInstance().getImageLoader();

public CustomListAdapter(Activity activity, List<Sched> movieItems) {
    this.activity = activity;
    this.movieItems = movieItems;
}

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

@Override
public Object getItem(int location) {
    return movieItems.get(location);
}

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

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

    if (inflater == null)
        inflater = (LayoutInflater) activity
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    if (convertView == null)
        convertView = inflater.inflate(R.layout.listitem_ros, null);

    if (imageLoader == null)
        imageLoader = AppController.getInstance().getImageLoader();
    NetworkImageView thumbNail = (NetworkImageView) convertView
            .findViewById(R.id.pict);
    NetworkImageView teambthumbnail=(NetworkImageView) convertView.findViewById(R.id.pict1);
    TextView teama = (TextView) convertView.findViewById(R.id.scheduleteama_name);
    TextView teamb = (TextView) convertView.findViewById(R.id.schduleteamb_name);
    TextView tdate = (TextView) convertView.findViewById(R.id.datess);
    TextView ttime = (TextView) convertView.findViewById(R.id.timess);
    TextView venue=(TextView)convertView.findViewById(R.id.schedulevenue);
    // getting movie data for the row
    Sched m = movieItems.get(position);

    // thumbnail image
    thumbNail.setImageUrl(m.getThumbnailUrl(), imageLoader);
    teambthumbnail.setImageUrl(m.getTeambthumbnailUrl(),imageLoader);
    // title
    teama.setText(m.getTeama());
    teamb.setText(m.getTeamb());
    tdate.setText(m.getTdate());
    ttime.setText(m.getTtime());
    venue.setText(m.getVenue());


    return convertView;
}}

的xml:

public class Sched {
private String teama,teamb,tdate,ttime,thumbnailUrl,teambthumbnailUrl,venue;

public Sched(){}

public Sched(String teama, String teamb, String tdate, String ttime, String thumbnailUrl, String teambthumbnailurl, String venue)
{
    this.teama=teama;
    this.teamb=teamb;
    this.tdate=tdate;
    this.ttime=ttime;
    this.thumbnailUrl=thumbnailUrl;
    this.teambthumbnailUrl=teambthumbnailurl;
    this.venue=venue;


}


public String getTeama(){
    return teama;
}
public void setTeama(String teama){
    this.teama=teama;
}
public String getTeamb(){
    return teamb;
}
public void setTeamb(String teamb){
    this.teamb=teamb;
}

public String getTdate(){
    return tdate;
}
public void setTdate(String tdate){
    this.tdate=tdate;
}
public String getTtime(){
    return ttime;
}
public void setTtime(String ttime){
    this.ttime=ttime;
}

public String getThumbnailUrl() {
    return thumbnailUrl;
}
public void setThumbnailUrl(String thumbnailUrl) {
    this.thumbnailUrl = thumbnailUrl;
}
public String getTeambthumbnailUrl() {
    return teambthumbnailUrl;
}
public void setTeambthumbnailUrl(String teambthumbnailUrl){
    this.teambthumbnailUrl=teambthumbnailUrl;
}
public String getVenue(){
    return venue;
}
public void setVenue(String venue){
    this.venue=venue;
}}

1 个答案:

答案 0 :(得分:0)

您的问题是您在填充movieList之前正在创建适配器。移动这两行:

    adapter = new CustomListAdapter(getActivity(), movieList);
    listview.setAdapter(adapter);

在您将电影添加到列表中的for循环之后:

for (int i = 0; i < response.length(); i++) {
                    try {

                        JSONObject obj = response.getJSONObject(i);
                        Sched m = new Sched();
                        m.setTeama(obj.getString("teama"));

                        m.setTeamb(obj.getString("teamb"));
                        m.setTdate(obj.getString("tdate"));
                        m.setTtime(obj.getString("ttime"));
                       m.setThumbnailUrl(obj.getString("thumbnailUrl"));
                        m.setTeambthumbnailUrl(obj.getString("teambthumbnailUrl"));
                        m.setVenue(obj.getString("venue"));


                        // adding movie to movies array
                        movieList.add(m);

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
//PUT THOSE TWO LINES HERE

假设您的Request状态良好且返回非空JSONArray,则应该可以。