2

저는 작업 관리자 앱을 개발 중입니다. 기본적으로 google places API를 사용하여 위치를 선택하여 sqlite databse에 저장하고 RecyclerView에 채 웁니다. 모든 위치에서 나는 해당 위치의 현재 날씨를 얻고 RecyclerView에 표시하기 위해 retrofit2openweather api를 사용합니다. 이 작업은 onBindViewHolder 메서드의 recyclerview 어댑터에있는 비동기 작업에서 수행됩니다. 이제는 작업을 생성 할 때 문제없이 그 위치에 데이터를 가져옵니다. 문제는 더 많은 작업을 추가 할 때 시작됩니다. 기본적으로 새 위치로 새 작업을 추가하면 아래 이미지와 같이 마지막으로 추가 된 작업의 현재 날씨를 덮어 씁니다. 그것은 이전 작업을하지를 덮어 않도록RecyclerView는 목록 항목을 덮어 씁니다.

어떻게 내가 그것을 할 수 https://s9.postimg.org/l6tmi3wm7/Screenshot_20160830_143608.png ?

public class TaskAdapter extends RecyclerView.Adapter<TaskAdapter.ViewHolder> { 

    private List<Task> taskList; 

    private final String API_KEY = "8617b30a6fc114ad2ad929c111b76edf"; 
    private final String UNITS = "metric"; 
    private double latitude, longitude; 
    private Task task; 
    private WeatherInfo weatherInfo; 
    private Context context; 

    public class ViewHolder extends RecyclerView.ViewHolder { 
     private TextView taskName, destination, currentWeather; 

     private ImageView weatherImg; 

     public ViewHolder(View view) { 
      super(view); 
      taskName = (TextView) view.findViewById(R.id.task); 
      destination = (TextView) view.findViewById(R.id.date); 
      currentWeather = (TextView) view.findViewById(R.id.weather); 
      weatherImg = (ImageView) view.findViewById(R.id.weather_icon); 
     } 
    } 

    public TaskAdapter(List<Task> taskList) { 
     this.taskList = taskList; 
    } 

    public void add(int position, Task item) { 
     taskList.add(position, item); 
     notifyItemInserted(position); 
    } 

    public void remove(Task item) { 
     int position = taskList.indexOf(item); 
     taskList.remove(position); 
     notifyItemRemoved(position); 
    } 

    public Task getTask(int position) { 
     return taskList.get(position); 
    } 

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

    @Override 
    public void onBindViewHolder(ViewHolder holder, int position) { 
     task = taskList.get(position); 
     latitude = task.getDestinationLatitude(); 
     longitude = task.getDestinationLongitude(); 


     new getWeatherDataAsync(holder).execute(); 


     holder.taskName.setText(task.getTaskName()); 
     holder.destination.setText(task.getDestinationName()); 
    } 

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

    private class getWeatherDataAsync extends AsyncTask<Void, Void, Void> { 

     private ViewHolder holder; 
     private ProgressDialog progressDialog; 

     public getWeatherDataAsync(ViewHolder holder) { 
      this.holder = holder; 
     } 

     @Override 
     protected void onPreExecute() { 
      progressDialog=ProgressDialog.show(context,"Loading...","Getting weather."); 
     } 

     @Override 
     protected Void doInBackground(Void... vHolders) { 
      try { 

       WeatherApi weatherApi = WeatherApi.retrofit.create(WeatherApi.class); 
       Call<WeatherInfo> call = weatherApi.getWeatherData(latitude, longitude, API_KEY, UNITS); 
       weatherInfo = call.execute().body(); 

// 
      } catch (IOException e) { 
       Log.e("get weather coordinates", "something went wrong: " + e.getMessage()); 
      } 
      return null; 
     } 

     @Override 
     protected void onPostExecute(Void aVoid) { 
      super.onPostExecute(aVoid); 

      if (weatherInfo != null) { 
       holder.currentWeather.setText(String.valueOf(weatherInfo.getMain().getTemp()) + "\u2103"); 
       getWeatherIcon(holder); 

      } else { 
       holder.currentWeather.setText("N/A \u2103"); 
       Picasso.with(context).load("file:///android_asset/md-weather-iconset/weather-none-available.png").into(holder.weatherImg); 
      } 


      progressDialog.dismiss(); 

     } 

    } 

    /** 
    * Display the correct weather icon from assets based on the data returned from the Retrofit query. 
    * @param viewHolder 
    */ 
    private void getWeatherIcon(ViewHolder viewHolder){ 
     String base="file:///android_asset/md-weather-iconset"; 



     switch (weatherInfo.getWeather().get(0).getIcon()) { 
      case "01d": 
       Picasso.with(context).load(base+"/weather-clear.png").into(viewHolder.weatherImg); 
       break; 
      case "02d": 
       Picasso.with(context).load(base+"/weather-few-clouds.png").into(viewHolder.weatherImg); 
       break; 
      case "03d": 
       Picasso.with(context).load(base+"/weather-clouds.png").into(viewHolder.weatherImg); 
       break; 
      case "04d": 
       Picasso.with(context).load(base+"/weather-clouds.png").into(viewHolder.weatherImg); 
       break; 
      case "09d": 
       Picasso.with(context).load(base+"/weather-showers-day.png").into(viewHolder.weatherImg); 
       break; 
      case "10d": 
       Picasso.with(context).load(base+"/weather-rain-day.png").into(viewHolder.weatherImg); 
       break; 
      case "11d": 
       Picasso.with(context).load(base+"/weather-storm-day.png").into(viewHolder.weatherImg); 
       break; 
      case "13d": 
       Picasso.with(context).load(base+"/weather-snow.png").into(viewHolder.weatherImg); 
       break; 
      case "50d": 
       Picasso.with(context).load(base+"/weather-mist.png").into(viewHolder.weatherImg); 
       break; 
      case "01n": 
       Picasso.with(context).load(base+"/weather-clear-night.png").into(viewHolder.weatherImg); 
       break; 
      case "02n": 
       Picasso.with(context).load(base+"/weather-few-clouds-night.png").into(viewHolder.weatherImg); 
       break; 
      case "03n": 
       Picasso.with(context).load(base+"/weather-clouds-night.png").into(viewHolder.weatherImg); 
       break; 
      case "04n": 
       Picasso.with(context).load(base+"/weather-clouds-night.png").into(viewHolder.weatherImg); 
       break; 
      case "09n": 
       Picasso.with(context).load(base+"/weather-showers-night.png").into(viewHolder.weatherImg); 
       break; 
      case "10n": 
       Picasso.with(context).load(base+"/weather-rain-night.png").into(viewHolder.weatherImg); 
       break; 
      case "11n": 
       Picasso.with(context).load(base+"/weather-storm-night.png").into(viewHolder.weatherImg); 
       break; 
      case "13n": 
       Picasso.with(context).load(base+"/weather-snow.png").into(viewHolder.weatherImg); 
       break; 
      case "50n": 
       Picasso.with(context).load(base+"/weather-mist.png").into(viewHolder.weatherImg); 
       break; 


     } 
    } 


} 
+0

보십시오'holder.currentWeather.setText (task.getTemperatur()); ' – Abbas

답변

0

비동기 작업을 시작하고 holder에 대한 참조를 전달하려면 onBindViewHolder()을 사용하고 있습니다. 불행하게도, RecyclerView가 어떻게 작동해야하는지는 아닙니다. RecyclerView는 ViewHolder 패턴을 사용하여 더 이상 보이지 않는 이미 인스턴스화 된 레이아웃을 재사용하므로 네트워크 응답이 시작될 때 홀더가 완전히 다른 항목을 나타낼 수 있습니다.

수행해야 할 작업은 비동기 호출을 다른 곳으로 이동하는 것입니다 (예 : 조각/활동/발표자), 콜백에서 taskList 수집을 업데이트하고 데이터 세트는 다음 방법 중 하나를 사용하여 변경 어댑터 통지 : 녹화를 살펴 보자

notifyDataSetChanged()

notifyItemInserted()

을 yclerView.Adapter 워드 프로세서 :

ReyclerView.Adapter

+0

당신이 올바른 방향으로 나를 가리켰다 건배. –

+0

천만 ~, 좋은 하루 되세요. – pawelo

0

먼저 예상되는 출력이 무엇인지 분명히 할 수 있습니다. 어쩌면 예상 출력을 보여주는 이미지와 출력으로 충분할 것입니다. 또한 어댑터 내부의 네트워크에서 가져 오지 말고, 대신 모든 weatherdata를 조각으로 가져 와서 어댑터에 전달하는 방법에 대한 조언을 드리겠습니다. 여러분에게 recyclerview를 보유한 활동이 있다고 가정 해 보겠습니다. 인터페이스를 사용하여 어댑터에서 asynctask를 호출하십시오.

뷰티 홀더에 위도와 경도를 저장하고 작동하는지 다시 확인하십시오.

0

문제

private WeatherInfo weatherInfo; 

은 AsyncTask를 내부 WeatherInfo 객체를 생성합니다. 이것은 당신의 문제를 해결할 것입니다. AsyncTask가 실행되고 새 WeatherInfo 개체가 만들어집니다. 이제 해당 객체를 메소드에 전달합니다. 이

getWeatherIcon(ViewHolder viewHolder,WeatherInfo wetherInfo) 

이 같은

변경 방법은 문제를 해결할 것입니다.

관련 문제