为什么我的recyclerview适配器不起作用? /为什么课程没有运行?

时间:2017-03-16 04:37:11

标签: java android android-recyclerview adapter

我一直在尝试让我的RecyclerView适配器工作,但是我无法在卡片上显示任何内容,而且,由于Logcat没有显示任何通过日志设置的日志,因此CycleTrails类不会运行class ...在我设置适配器之前,一切正常,我可以从CycleTrails类中获取所有值。但现在,事情搞砸了,我无法弄清楚原因。

MainActivity.java(我在适配器中添加的onCreate部分/唯一的东西)

@Override
protected void onCreate(Bundle savedInstanceState) {


    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    rvItem = (RecyclerView) findViewById(R.id.rvItem);
    rvItem.setHasFixedSize(true);
    LinearLayoutManager manager = new LinearLayoutManager(this);
    rvItem.setLayoutManager(manager);
    ArrayList<Trails> finalData = getTrails();
    Adapter adapter = new Adapter(this, finalData);
    rvItem.setAdapter(adapter);

CycleTrails.java

public class CycleTrails extends MainActivity {

public static double LAT_MAX = myLatitude + (0.01 * 200);
public static double LAT_MIN = myLatitude - (0.01 * 200);
public static double LON_MAX = myLongitude + (0.01 * 200);
public static double LON_MIN = myLongitude - (0.01 * 200);

public static ArrayList<Trails> getTrails() {
    Log.d("trails", "Cycle trails running");
    ArrayList<Trails> trailList = new ArrayList<>();
    Trails t1 = new Trails(51.71, -3.36, "Bike Park Wales", "", "", "");
    Trails t2 = new Trails(51.66, -3.35, "Mountain Ash", "", "", "");
    Trails t3 = new Trails(50.50, -4.17, "FlyUp Downhill", "", "", "");
    trailList.add(t1);
    trailList.add(t2);
    trailList.add(t3);


    final ArrayList<Trails> localTrails = new ArrayList<>();
    for (final Trails trail : trailList) {
        Log.d("List running", "Local trail list");
        if (trail.lat > LAT_MIN && trail.lat < LAT_MAX && trail.lon > LON_MIN && trail.lon < LON_MAX) {
            localTrails.add(trail);
            Log.d("Calc", "Run code");


            final RequestBuilder weather = new RequestBuilder();

            final Request request = new Request();
            request.setLat(String.valueOf(trail.lat));
            Log.d("Lat used", String.valueOf(trail.lat));
            Log.d("Actual Lat", request.getLat());
            request.setLng(String.valueOf(trail.lon));
            Log.d("Lon used", String.valueOf(trail.lon));
            request.setUnits(Request.Units.UK);
            request.setLanguage(Request.Language.ENGLISH);
            request.addExcludeBlock(Request.Block.CURRENTLY);
            request.removeExcludeBlock(Request.Block.CURRENTLY);


            weather.getWeather(request, new Callback<WeatherResponse>() {
                @Override
                public void success(WeatherResponse weatherResponse, Response response) {
                    Log.d("Weather", "We have weather!");
                    Log.d("Location", weatherResponse.getTimezone());
                    trail.temp = String.valueOf(weatherResponse.getCurrently().getTemperature());
                    Log.d("Temperature", trail.temp);
                    trail.weather = weatherResponse.getCurrently().getIcon();
                }

                @Override
                public void failure(RetrofitError retrofitError) {
                    Log.d("Error while calling: ", "Weather");
                }


            });
        } else {
            Log.d("Too far", "Trails too far" + trail.name);
        }
    }
    return localTrails;
}

}

适配器

public class Adapter extends RecyclerView.Adapter<Adapter.ViewHolder> {
private Context context;
public ArrayList <Trails> finalData;

public Adapter (Context context, ArrayList<Trails> finalData) {
    Log.d("Hello", "Adapter");
    this.context = context;
    this.finalData = finalData;
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    LayoutInflater inflater = LayoutInflater.from(parent.getContext());
    Log.d("Hello", "2");
    View view = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.trail_card, parent, false);
    ViewHolder viewHolder = new ViewHolder(view);
    return viewHolder;
}

@Override
public void onBindViewHolder(ViewHolder holder, int position) {
    Trails trails = finalData.get(position);
    Log.d("Hello", "3");
    Picasso.with(context)
            .load("http://www.wigglestatic.com/product-media/5360108808/Wiggle-Road-Bike-Road-Bikes-Black-1WGMY16R7048UK0001-6.jpg?w=2000&h=2000&a=7")
            .placeholder(R.drawable.cycle)
            .error(android.R.drawable.stat_notify_error)
            .into(holder.locationImage);
    holder.locationText.setText(trails.name);
    Log.d("Location", trails.name);
    holder.temperatureText.setText(trails.temp);
    Log.d("Temperature", trails.temp);
    holder.weatherText.setText(trails.weather);
    holder.conditionsText.setText(trails.conditions);
}

@Override
public int getItemCount() {
    Log.d("Hello", "4");
    if(finalData != null) {
        Log.d("Hello", "5");
        return finalData.size();
    }
    return 0;
}

public static class ViewHolder extends RecyclerView.ViewHolder {

    public CardView cardView;
    public ImageView locationImage;
    public TextView locationText;
    public TextView temperatureText;
    public TextView weatherText;
    public TextView conditionsText;

    public ViewHolder(View itemView) {
        super(itemView);
        cardView = (CardView)itemView.findViewById(R.id.cardView);
        locationImage = (ImageView)itemView.findViewById(R.id.locationImage);
        locationText = (TextView)itemView.findViewById(R.id.locationText);
        temperatureText = (TextView)itemView.findViewById(R.id.temperatureText);
        weatherText = (TextView)itemView.findViewById(R.id.weatherText);
        conditionsText = (TextView)itemView.findViewById(R.id.conditionsText);
    }
}
}

Trails.java

public class Trails {
public String name;
public double lat;   // Object fields
public double lon;
public String temp;
public String weather;
public static String conditions;


Trails(double lat, double lon, String name, String temp, String weather, String conditions){ // construcor
    this.lat = lat;
    this.lon = lon;
    this.name = name;
    this.temp = temp;
    this.weather = weather;
    this.conditions = conditions;
}
}

这是整个运行

的应用程序的logcat
03-16 10:32:48.877 20535-20535/com.example.android.cycleuk D/Lat: 52.1981981981982
03-16 10:32:48.877 20535-20535/com.example.android.cycleuk D/Lon: -2.2341297062250933
03-16 10:33:19.859 20535-20542/com.example.android.cycleuk W/art: Suspending all threads took: 10.141ms
03-16 10:33:19.956 20535-20535/com.example.android.cycleuk D/Hello: Adapter
03-16 10:33:20.023 20535-20535/com.example.android.cycleuk D/Hello: 4
03-16 10:33:20.023 20535-20535/com.example.android.cycleuk D/Hello: 4
03-16 10:33:20.069 20535-20535/com.example.android.cycleuk D/Lat: 52.1981981981982
03-16 10:33:20.069 20535-20535/com.example.android.cycleuk D/Lon: -2.2341297062250933

4 个答案:

答案 0 :(得分:1)

使用recycleview绑定适配器..

$arr_dt = array();

foreach ($dt as $r){
    $arr_dt[$r->criteria][$r->alternative] = $r->value;
}

答案 1 :(得分:0)

我认为你必须稍微更改你的Adapter类。首先 您必须将适配器添加到您的recyclerView。请确保recyclerView使用布局管理器初始化而不是null。

Adapter adapter = new Adapter(this);
rvItem.setAdapter(adapter);

这是我们的适配器类实现。您必须在Adapter类中创建List。

public class Adapter {

ArrayList<Trails> finalData;
Context context;

public Adapter(Context context){
     this.context = context;
     finalData = new ArrayList<>();
}

.....

....

public void addItem(Trails data){
    finalData.add(data);
    notifyDataSetChanged(); // Note this method is important because it will let adapter know size of list changed and render the list
}

}

在您的片段或Activity类中添加该方法

Adapter adapter = new Adapter(this);
rvItem.setAdapter(adapter);

/// here your methods what ever you want

dataList = getTrails();
for(Trails trails: dataList){
    adapter.addItem(trails);
}

我希望这会对你有所帮助。

答案 2 :(得分:0)

I think your getTrails method always return size 0 list that why you never get anything in recyclerview. All code is working fine . just check your code that you get any data in your list before setting it into recyclerview.



public static ArrayList<Trails> getTrails() {
    Log.d("trails", "Cycle trails running");
    ArrayList<Trails> trailList = new ArrayList<>();
    Trails t1 = new Trails(51.71, -3.36, "Bike Park Wales", "", "", "");
    Trails t2 = new Trails(51.66, -3.35, "Mountain Ash", "", "", "");
    Trails t3 = new Trails(50.50, -4.17, "FlyUp Downhill", "", "", "");
    trailList.add(t1);
    trailList.add(t2);
    trailList.add(t3);


    final ArrayList<Trails> localTrails = new ArrayList<>();
    for (final Trails trail : trailList) {
        Log.d("List running", "Local trail list");
        if (trail.lat > LAT_MIN && trail.lat < LAT_MAX && trail.lon > LON_MIN && trail.lon < LON_MAX) {
            localTrails.add(trail);
            Log.d("Calc", "Run code");


            final RequestBuilder weather = new RequestBuilder();

            final Request request = new Request();
            request.setLat(String.valueOf(trail.lat));
            Log.d("Lat used", String.valueOf(trail.lat));
            Log.d("Actual Lat", request.getLat());
            request.setLng(String.valueOf(trail.lon));
            Log.d("Lon used", String.valueOf(trail.lon));
            request.setUnits(Request.Units.UK);
            request.setLanguage(Request.Language.ENGLISH);
            request.addExcludeBlock(Request.Block.CURRENTLY);
            request.removeExcludeBlock(Request.Block.CURRENTLY);


            weather.getWeather(request, new Callback<WeatherResponse>() {
                @Override
                public void success(WeatherResponse weatherResponse, Response response) {
                    Log.d("Weather", "We have weather!");
                    Log.d("Location", weatherResponse.getTimezone());
                    trail.temp = String.valueOf(weatherResponse.getCurrently().getTemperature());
                    Log.d("Temperature", trail.temp);
                    trail.weather = weatherResponse.getCurrently().getIcon();
                }

                @Override
                public void failure(RetrofitError retrofitError) {
                    Log.d("Error while calling: ", "Weather");
                }


            });
        } else {
            Log.d("Too far", "Trails too far" + trail.name);
        }
    }
    return localTrails;
}

答案 3 :(得分:0)

您的from c in contacts join ls in leadSales on c.ContactID equals ls.SoldToContactID into c_ls select new LeadBuyersByStateItem { ContactID = c.ContactID, Name = c.LastName + ", " + c.FirstName, LeadsCount = c_ls.Count() }; 不同步吗?这可能会导致weather.getWeather()始终为空。也许您可以尝试将此代码localTrails置于同步调用的localTrails.add(trail)方法中。