回收视图为空

时间:2017-09-21 12:16:27

标签: android android-recyclerview

回收视图为空

这是我第一次使用recycleview进行改造。启动listactivity.java时会出现empty recycleview。在recycleview中不会显示本地主机中保存的项目。任何人都可以帮助我吗?

MainActivity.class

package com.example.kiran.fruitveg;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;

import java.util.List;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void getFruit(View view) {
        Intent intent = new Intent(this,ListActivity.class);
        intent.putExtra("type","fruit");
        startActivity(intent);
    }

    public void getVegetable(View view) {
        Intent intent = new Intent(this,ListActivity.class);
        intent.putExtra("type","vegetable");
        startActivity(intent);
    }
}

ListActivity.class

package com.example.kiran.fruitveg;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

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

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class ListActivity extends AppCompatActivity {

    private RecyclerView recyclerView;
    private RecyclerView.LayoutManager layoutManager;
    private List<Calories> calories;
    private RecyclerAdapter adapter;
    private ApiInterface apiInterface;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list);

        calories = new ArrayList<>();
        recyclerView = (RecyclerView) findViewById(R.id.recyclerview);
        layoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setHasFixedSize(true);
        adapter = new RecyclerAdapter(calories,ListActivity.this);
        recyclerView.setAdapter(adapter);

        if(getIntent().getExtras()!=null)
        {
            String type = getIntent().getExtras().getString("type");
        }

    }
    public void fetchInformation(String type)
    {
        apiInterface = ApiClient.getApiClient().create(ApiInterface.class);
        Call<List<Calories>> call = apiInterface.getCaloriesInfo(type);
        call.enqueue(new Callback<List<Calories>>() {
            @Override
            public void onResponse(Call<List<Calories>> call, Response<List<Calories>> response) {
                calories = response.body();
                adapter = new RecyclerAdapter(calories,ListActivity.this);
                recyclerView.setAdapter(adapter);
            }

            @Override
            public void onFailure(Call<List<Calories>> call, Throwable t) {

            }
        });
    }
}

Calories.java

package com.example.kiran.fruitveg;

import com.google.gson.annotations.SerializedName;

/**
 * Created by kiran on 9/21/2017.
 */
public class Calories {

    @SerializedName("name")
    private String Name;

    @SerializedName("image_path")
    private String Image_path;

    @SerializedName("calories")
    private int Calories;

    public String getName() {
        return Name;
    }

    public String getImage_path() {
        return Image_path;
    }

    public int getCalories() {
        return Calories;
    }
}

RecyclerAdapter.java

package com.example.kiran.fruitveg;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;

import java.util.List;

import retrofit2.Call;

/**
 * Created by kiran on 9/21/2017.
 */
public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.MyViewHolder> {

    private List<Calories> calories;
    private Context context;

    public RecyclerAdapter(List<Calories> calories,Context context)
    {
        this.calories = calories;
        this.context = context;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.raw_layout,parent,false);
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        holder.Name.setText(calories.get(position).getName());
        holder.Calories.setText("Calories:"+ Integer.toString(calories.get(position).getCalories()));
        Glide.with(context).load(calories.get(position).getImage_path()).into(holder.img);
    }

    @Override
    public int getItemCount() {
        return calories.size();
    }

    public static class MyViewHolder extends RecyclerView.ViewHolder
    {
        ImageView img;
        TextView Name,Calories;

        public MyViewHolder(View itemView) {
            super(itemView);
            img = (ImageView)itemView.findViewById(R.id.img);
            Name = (TextView)itemView.findViewById(R.id.name);
            Calories = (TextView)itemView.findViewById(R.id.calories);
        }
    }
}

ApiClient.java

package com.example.kiran.fruitveg;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by kiran on 9/20/2017.
 */
public class ApiClient {

    public static final String BASE_URL = "http://10.0.3.2/caloriesapp/";

    public static Retrofit retrofit = null;

    public static Retrofit getApiClient()
    {
        if(retrofit==null)
        {
            retrofit = new Retrofit.Builder().baseUrl(BASE_URL)
                    .addConverterFactory(GsonConverterFactory.create()).build();
        }
        return retrofit;
    }
}

ApiInterface.java

    package com.example.kiran.fruitveg;

import java.util.List;

import retrofit2.Call;
import retrofit2.http.GET;
import retrofit2.http.POST;
import retrofit2.http.Query;

/**
 * Created by kiran on 9/20/2017.
 */
public interface ApiInterface {


    @GET("getcalories.php")
    Call<List<Calories>> getCaloriesInfo(@Query("item_type") String item_type);
}

1 个答案:

答案 0 :(得分:2)

只需在if循环

中调用fetchInformation(String type)
if(getIntent().getExtras()!=null)
    {
        String type = getIntent().getExtras().getString("type");
        fetchInformation(type)
    }

}
public void fetchInformation(String type)
{
    apiInterface = ApiClient.getApiClient().create(ApiInterface.class);
    Call<List<Calories>> call = apiInterface.getCaloriesInfo(type);
    call.enqueue(new Callback<List<Calories>>() {
        @Override
        public void onResponse(Call<List<Calories>> call, Response<List<Calories>> response) {
            calories = response.body();
            adapter = new RecyclerAdapter(calories,ListActivity.this);
            recyclerView.setAdapter(adapter);
        }

        @Override
        public void onFailure(Call<List<Calories>> call, Throwable t) {

        }
    });
}