我的列表视图在使用改造时不返回任何内容

时间:2014-06-15 08:53:59

标签: android listview retrofit

我是翻新的新手,我只是想尝试json解析,我不知道我错过了什么?

列表视图未显示。请帮我解决问题。我正在使用ListAcivity和List Adapter.This对我来说很奇怪。请帮忙。 这是Client.java

public class UtubeClient {
private static UtubeApiInterface UtubeService;

public static UtubeApiInterface getTwitchTvApiClient() {
    if (UtubeService== null) {
         RestAdapter restAdapter = new RestAdapter.Builder()
                .setEndpoint("http://tamilpasanga.in")
                .build();

        UtubeService = restAdapter.create(UtubeApiInterface.class);
    }

    return UtubeService;
}

public interface UtubeApiInterface {
    @GET("/mytuts/list.json")
    void getStreams(  Callback<List<UtubeChannel>> callback);
}

}

在mainActvity中获取回调

MainActivity.java

public class MainActivity extends ListActivity {

//private ProgressBar mProgressBar;



private List<UtubeChannel> streamData;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    streamData = new ArrayList<UtubeChannel>();

   // mProgressBar = (ProgressBar) findViewById(R.id.progressBar);



    setListAdapter(new MobileArrayAdapter(this,streamData));

    downloadDatainRetrofit();


}





private void downloadDatainRetrofit() {


       // mProgressBar.setVisibility(View.VISIBLE);

        UtubeClient.getTwitchTvApiClient().getStreams(new Callback<List<UtubeChannel>>() {
                                                          @Override
                                                          public void success(List<UtubeChannel> utubeChannels, Response response) {
                                                          streamData.addAll(utubeChannels) ;
                                                          }

                                                          @Override
                                                          public void failure(RetrofitError retrofitError) {
                                                              //
                                                          }


                                                      }
        );
    }
}

UTubeChannel.java

      public class UtubeChannel {
private  String url;
private  String name;
     UtubeChannel(String url,String  name){
 this.url=url;
 this.name=name;
   }


public String getUrl()
{
    return this.url;

}
public  void setUrl(String url)
{
    this.url=url;
}

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

}

}

移动阵列适配器

public class MobileArrayAdapter extends ArrayAdapter<UtubeChannel> {
protected static final String TAG = null;
private final Context context;

String outFileName;
private LayoutInflater inflater;







public MobileArrayAdapter(final Context context, List<UtubeChannel > objects) {
    super(context, R.layout.listview_item, objects);
    inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    this.context = context;





}

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


    ViewHolder holder = null;
    if (rowView == null) {

        rowView = inflater.inflate(R.layout.listview_item, null);
        holder = new ViewHolder();
        holder.name = (TextView) rowView.findViewById(R.id.name);
        holder.url = (TextView) rowView.findViewById(R.id.url);



        //  viewHolder.button.setTag(viewHolder.text);
        // viewHolder.text.setTag(viewHolder.button);


        rowView.setTag(holder);
    } else {
        holder = (ViewHolder) rowView.getTag();


    }





   UtubeChannel channel= getItem(position);

    holder.name.setText(channel.getName());
    holder.url.setText(channel.getUrl());




    return rowView;
}

static class ViewHolder {
    public TextView url;


    public TextView name;



}

}

1 个答案:

答案 0 :(得分:1)

您将空列表传递给适配器,然后您尝试从Internet下载数据,但是当您不打电话时,notifyDatasetChanged您的列表没有显示任何内容。所以你也需要改变你的代码:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    streamData = new ArrayList<UtubeChannel>();

   // mProgressBar = (ProgressBar) findViewById(R.id.progressBar);
    downloadDatainRetrofit();

}





private void downloadDatainRetrofit() {


       // mProgressBar.setVisibility(View.VISIBLE);

 UtubeClient.getTwitchTvApiClient().getStreams(new Callback<List<UtubeChannel>>() {
 @Override
 public void success(List<UtubeChannel> utubeChannels, Response response) {
       streamData.addAll(utubeChannels) ;
       setListAdapter(new MobileArrayAdapter(this,streamData));
 }

 @Override
 public void failure(RetrofitError retrofitError) {
                                                              //
 }


 });
}

或者您可以使用:

1 - 在您的班级中创建一个全局值:

MobileArrayAdapter adapter.

2- in onCreate使用:

adapter = new MobileArrayAdapter(this,streamData);
setListAdapter(adapter);

3-下载数据后使用:

adapter.notifyDatasetChanged();

用于刷新列表数据