我的RecyclerView
跳过视图以及第一次加载时它会使所有视图成为数组中的同一个对象,直到我向下滚动并向上然后它们开始出现。我没有看到问题,因为它确实有效,它只是不能正常工作。我正在使用Volley
进行网络通话。
public class ForecastActivity extends AppCompatActivity {
RecyclerView recyclerView;
ListAdapter listAdapter;
RequestQueue requestQueue;
JSONParser jsonParser = JSONParser.getJsonParser();
ForecastListItem[] mArrayList;
TextView mForecastCityandCountyText;
String mForecastCityandContryString;
String mForecastCityNameString;
String mForecastCountryString;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_forcast);
initializeUI();
requestQueue = Volley.newRequestQueue(this);
netWorkRequest();
}
public void initializeUI() {
mForecastCityandCountyText = (TextView) findViewById(R.id.ForecastCityName);
recyclerView = (RecyclerView) findViewById(R.id.Recylerviewlayout);
listAdapter = new ListAdapter();
recyclerView.setAdapter(listAdapter);
recyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
mArrayList = new ForecastListItem[10];
mArrayList[0] = new ForecastListItem();
mArrayList[1] = new ForecastListItem();
mArrayList[2] = new ForecastListItem();
mArrayList[3] = new ForecastListItem();
mArrayList[4] = new ForecastListItem();
mArrayList[5] = new ForecastListItem();
mArrayList[6] = new ForecastListItem();
mArrayList[7] = new ForecastListItem();
mArrayList[8] = new ForecastListItem();
mArrayList[9] = new ForecastListItem();
}
public void netWorkRequest() {
JsonObjectRequest JsonObjectRequest = new JsonObjectRequest(Request.Method.GET, jsonParser.getForecastUrl(), null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
String UniverSalIconString;
Log.d(jsonParser.getForecastUrl(), "");
JSONObject mJsonCityObject = response.getJSONObject("city");
Log.d("here is the object", mJsonCityObject.toString());
mForecastCityNameString = jsonParser.getString("name", mJsonCityObject);
mForecastCountryString = jsonParser.getString("country", mJsonCityObject);
mForecastCityandContryString = mForecastCityNameString + "," + mForecastCountryString;
mForecastCityandCountyText.setText(mForecastCityandContryString);
//*********************************************************************************************************************************************
// add all the information to arraylist so i can be processed by recycler view
// add refresh button
JSONArray JsonListArray = jsonParser.getJSONArray("list", response);
JSONObject Day1 = jsonParser.getJSONObject(0, JsonListArray);
JSONObject TempObj = jsonParser.getJSONObject("temp", Day1);
mArrayList[0].setmForecastTempDouble(jsonParser.getDouble("day", TempObj));
JSONArray JsonDay1WeatherArray = jsonParser.getJSONArray("weather", Day1);
JSONObject Day1WeatherJsonObject = jsonParser.getJSONObject(0, JsonDay1WeatherArray);
UniverSalIconString = jsonParser.getString("icon", Day1WeatherJsonObject);
mArrayList[0].setmForecastDescriptionString(jsonParser.getString("description", Day1WeatherJsonObject));
mArrayList[0].setmForecastIconUrl(jsonParser.getCurrentWeatherIconUrl(UniverSalIconString));
mArrayList[0].setMYNUMBER(1);
UniverSalIconString = null;
JSONObject Day2 = jsonParser.getJSONObject(1, JsonListArray);
JSONObject TempObj2 = jsonParser.getJSONObject("temp", Day2);
mArrayList[1].setmForecastTempDouble(jsonParser.getDouble("day", TempObj2));
JSONArray JsonDay2WeatherArray = jsonParser.getJSONArray("weather", Day2);
JSONObject Day2WeatherJsonObject = jsonParser.getJSONObject(0, JsonDay2WeatherArray);
UniverSalIconString = jsonParser.getString("icon", Day2WeatherJsonObject);
mArrayList[1].setmForecastDescriptionString(jsonParser.getString("description", Day2WeatherJsonObject));
mArrayList[1].setmForecastIconUrl(jsonParser.getCurrentWeatherIconUrl(UniverSalIconString));
mArrayList[1].setMYNUMBER(2);
UniverSalIconString = null;
JSONObject Day3 = jsonParser.getJSONObject(2, JsonListArray);
JSONObject TempObj3 = jsonParser.getJSONObject("temp", Day3);
mArrayList[2].setmForecastTempDouble(jsonParser.getDouble("day", TempObj3));
JSONArray JsonDay3WeatherArray = jsonParser.getJSONArray("weather", Day3);
JSONObject Day3WeatherJsonObject = jsonParser.getJSONObject(0, JsonDay3WeatherArray);
UniverSalIconString = jsonParser.getString("icon", Day3WeatherJsonObject);
mArrayList[2].setmForecastDescriptionString(jsonParser.getString("description", Day3WeatherJsonObject));
mArrayList[2].setmForecastIconUrl(jsonParser.getCurrentWeatherIconUrl(UniverSalIconString));
mArrayList[2].setMYNUMBER(3);
UniverSalIconString = null;
JSONObject Day4 = jsonParser.getJSONObject(3, JsonListArray);
JSONObject TempObj4 = jsonParser.getJSONObject("temp", Day4);
mArrayList[3].setmForecastTempDouble(jsonParser.getDouble("day", TempObj4));
JSONArray JsonDay4WeatherArray = jsonParser.getJSONArray("weather", Day4);
JSONObject Day4WeatherJsonObject = jsonParser.getJSONObject(0, JsonDay4WeatherArray);
UniverSalIconString = jsonParser.getString("icon", Day4WeatherJsonObject);
mArrayList[3].setmForecastDescriptionString(jsonParser.getString("description", Day4WeatherJsonObject));
mArrayList[3].setmForecastIconUrl(jsonParser.getCurrentWeatherIconUrl(UniverSalIconString));
mArrayList[3].setMYNUMBER(4);
UniverSalIconString = null;
JSONObject Day5 = jsonParser.getJSONObject(4, JsonListArray);
JSONObject TempObj5 = jsonParser.getJSONObject("temp", Day5);
mArrayList[4].setmForecastTempDouble(jsonParser.getDouble("day", TempObj5));
JSONArray JsonDay5WeatherArray = jsonParser.getJSONArray("weather", Day5);
JSONObject Day5WeatherJsonObject = jsonParser.getJSONObject(0, JsonDay5WeatherArray);
UniverSalIconString = jsonParser.getString("icon", Day5WeatherJsonObject);
mArrayList[4].setmForecastDescriptionString(jsonParser.getString("description", Day5WeatherJsonObject));
mArrayList[4].setmForecastIconUrl(jsonParser.getCurrentWeatherIconUrl(UniverSalIconString));
mArrayList[4].setMYNUMBER(5);
UniverSalIconString = null;
JSONObject Day6 = jsonParser.getJSONObject(5, JsonListArray);
JSONObject TempObj6 = jsonParser.getJSONObject("temp", Day6);
mArrayList[5].setmForecastTempDouble(jsonParser.getDouble("day", TempObj6));
JSONArray JsonDay6WeatherArray = jsonParser.getJSONArray("weather", Day6);
JSONObject Day6WeatherJsonObject = jsonParser.getJSONObject(0, JsonDay6WeatherArray);
UniverSalIconString = jsonParser.getString("icon", Day6WeatherJsonObject);
mArrayList[5].setmForecastDescriptionString(jsonParser.getString("description", Day6WeatherJsonObject));
mArrayList[5].setmForecastIconUrl(jsonParser.getCurrentWeatherIconUrl(UniverSalIconString));
mArrayList[5].setMYNUMBER(6);
UniverSalIconString = null;
JSONObject Day7 = jsonParser.getJSONObject(6, JsonListArray);
JSONObject TempObj7 = jsonParser.getJSONObject("temp", Day7);
mArrayList[6].setmForecastTempDouble(jsonParser.getDouble("day", TempObj7));
JSONArray JsonDay7WeatherArray = jsonParser.getJSONArray("weather", Day7);
JSONObject Day7WeatherJsonObject = jsonParser.getJSONObject(0, JsonDay7WeatherArray);
UniverSalIconString = jsonParser.getString("icon", Day7WeatherJsonObject);
mArrayList[6].setmForecastDescriptionString(jsonParser.getString("description", Day7WeatherJsonObject));
mArrayList[6].setmForecastIconUrl(jsonParser.getCurrentWeatherIconUrl(UniverSalIconString));
mArrayList[6].setMYNUMBER(7);
UniverSalIconString = null;
JSONObject Day8 = jsonParser.getJSONObject(7, JsonListArray);
JSONObject TempObj8 = jsonParser.getJSONObject("temp", Day8);
mArrayList[7].setmForecastTempDouble(jsonParser.getDouble("day", TempObj8));
JSONArray JsonDay8WeatherArray = jsonParser.getJSONArray("weather", Day8);
JSONObject Day8WeatherJsonObject = jsonParser.getJSONObject(0, JsonDay8WeatherArray);
UniverSalIconString = jsonParser.getString("icon", Day8WeatherJsonObject);
mArrayList[7].setmForecastDescriptionString(jsonParser.getString("description", Day8WeatherJsonObject));
mArrayList[7].setmForecastIconUrl(jsonParser.getCurrentWeatherIconUrl(UniverSalIconString));
mArrayList[7].setMYNUMBER(8);
UniverSalIconString = null;
JSONObject Day9 = jsonParser.getJSONObject(8, JsonListArray);
JSONObject TempObj9 = jsonParser.getJSONObject("temp", Day9);
mArrayList[8].setmForecastTempDouble(jsonParser.getDouble("day", TempObj9));
JSONArray JsonDay9WeatherArray = jsonParser.getJSONArray("weather", Day9);
JSONObject Day9WeatherJsonObject = jsonParser.getJSONObject(0, JsonDay9WeatherArray);
UniverSalIconString = jsonParser.getString("icon", Day9WeatherJsonObject);
mArrayList[8].setmForecastDescriptionString(jsonParser.getString("description", Day9WeatherJsonObject));
mArrayList[8].setmForecastIconUrl(jsonParser.getCurrentWeatherIconUrl(UniverSalIconString));
mArrayList[8].setMYNUMBER(9);
UniverSalIconString = null;
JSONObject Day10 = jsonParser.getJSONObject(9, JsonListArray);
JSONObject TempObj10 = jsonParser.getJSONObject("temp", Day10);
mArrayList[9].setmForecastTempDouble(jsonParser.getDouble("day", TempObj10));
JSONArray JsonDay10WeatherArray = jsonParser.getJSONArray("weather", Day10);
JSONObject Day10WeatherJsonObject = jsonParser.getJSONObject(0, JsonDay10WeatherArray);
UniverSalIconString = jsonParser.getString("icon", Day10WeatherJsonObject);
mArrayList[9].setmForecastDescriptionString(jsonParser.getString("description", Day10WeatherJsonObject));
mArrayList[9].setmForecastIconUrl(jsonParser.getCurrentWeatherIconUrl(UniverSalIconString));
mArrayList[9].setMYNUMBER(10);
UniverSalIconString = null;
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
requestQueue.add(JsonObjectRequest);
}
class ListHolder extends RecyclerView.ViewHolder {
TextView DescriptionText;
TextView TempText;
TextView DateText;
NetworkImageView ForecastIconImageView;
ImageLoader imageLoader = new ImageLoader(requestQueue, new ImageLoader.ImageCache() {
LruCache<String, Bitmap> cache = new LruCache<String, Bitmap>(10);
@Override
public Bitmap getBitmap(String url) {
return cache.get(url);
}
@Override
public void putBitmap(String url, Bitmap bitmap) {
cache.put(url, bitmap);
}
});
public ListHolder(View itemView) {
super(itemView);
DescriptionText = (TextView) itemView.findViewById(R.id.ForcastWeatherDescriptionText);
TempText = (TextView) itemView.findViewById(R.id.ForecastWeatherTempText);
ForecastIconImageView = (NetworkImageView) itemView.findViewById(R.id.ForecastWeatherIconImageview);
// DateText = (TextView) itemView.findViewById(R.id.DateText);
}
void OnBind(ForecastListItem item) {
DescriptionText.setText(item.getmForecastDescriptionString());
TempText.setText(Integer.toString(item.getmForecastTempDouble()));
// DateText.setText(item.getmForecastDateString());
ForecastIconImageView.setImageUrl(item.getmForecastIconUrl(), imageLoader);
}
}
class ListAdapter extends RecyclerView.Adapter<ListHolder> {
@Override
public ListHolder onCreateViewHolder(ViewGroup parent, int viewType) {
ListHolder listHolder = new ListHolder(getLayoutInflater().inflate(R.layout.forecast_item_view, parent, false));
return listHolder;
}
@Override
public void onBindViewHolder(ListHolder holder, int position) {
Log.d("ARRAY NUM", Integer.toString(mArrayList[position].getMYNUMBER()));
holder.OnBind(mArrayList[position]);
// send the array to ViewHolder to set up the UI
}
@Override
public int getItemCount() {
return mArrayList.length;
}
}
答案 0 :(得分:0)
从我所看到的,您使用一些占位符数据(mArrayList)初始化您的适配器,然后启动网络请求以获取数据并更新原始的数据arraylist。
更好的方法是首先显示一个空列表,然后只有当您下载数据时才会在列表中显示它。
另外,当您下载数据时,应致电notifyDataSetChanged
上的ListAdapter
,告知原始数据已更改。
您的网络服务是异步的,通话直到它完成后才会立即返回。
您仍然可以将mArrayList变量传递给ListAdapter的构造函数,并在其中引用它。然后向您的适配器添加方法,例如
public void updateForcastData(ArrayList<ForecastListItem> newData) {
//insert new data into your field here
notifyDataSetChanged(); //consider using one of the other methods like notifyItemInserted() etc either
}