Android adapter.notifyDataSetChanged()不';t工作不正常,错误在哪里?
我无法从链接添加成功提取的数据。 我不知道错误在哪里,可能是变量“list”,但我真的不知道为什么应用程序不想启动。 每次涉及“adapter.notifyDataSetChanged();”时,应用程序都会崩溃 我知道这页:,但不要为我工作 MainActivity.java:Android adapter.notifyDataSetChanged()不';t工作不正常,错误在哪里?,android,listview,android-recyclerview,android-adapter,Android,Listview,Android Recyclerview,Android Adapter,我无法从链接添加成功提取的数据。 我不知道错误在哪里,可能是变量“list”,但我真的不知道为什么应用程序不想启动。 每次涉及“adapter.notifyDataSetChanged();”时,应用程序都会崩溃 我知道这页:,但不要为我工作 MainActivity.java: public class RecyclerViewActivity extends AppCompatActivity { Adapter adapter; private LinearLayoutManager ll
public class RecyclerViewActivity extends AppCompatActivity {
Adapter adapter;
private LinearLayoutManager llm;
private static List<Mymodel> ddd;
final String URL = "url";//this url contains an array: {"user":[{"message":"hi","from":"earth"}]}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
recycler = (RecyclerView)findViewById(R.id.recyclerview);
linearlm = new LinearLayoutManager(this);
recycler.setLayoutManager(llm);
recycler.setHasFixedSize(true);
//this list is empty, i want only start the adapter
List<Mymodel> list= new ArrayList<>();
adapter = new Adapter(list);
recycler.setAdapter(adapter);
new myfunction().execute(URL);
}
public class myfunctionextends AsyncTask<String, String, List<Mymodel>> {
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected List<Mymodel> doInBackground(String... params) {
HttpURLConnection connection = null;
BufferedReader reader = null;
try {
URL url = new URL(params[0]);
connection = (HttpURLConnection) url.openConnection();
connection.connect();
InputStream stream = connection.getInputStream();
reader = new BufferedReader(new InputStreamReader(stream));
StringBuffer buffer = new StringBuffer();
String line = "";
while ((line = reader.readLine()) != null) {
buffer.append(line);
}
String finalJson = buffer.toString();
JSONObject parentObject = new JSONObject(finalJson);
JSONArray parentArray = parentObject.getJSONArray("user");
List<Mymodel> modelist = new ArrayList<>();
Gson gson = new Gson();
for (int i = 0; i < parentArray.length(); i++) {
JSONObject finalObject = parentArray.getJSONObject(i);
Mymodel mymodel= gson.fromJson(finalObject.toString(), Mymodel.class);
modelist .add(mymodel);
}
return modelist ;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
} finally {
if (connection != null) {
connection.disconnect();
}
try {
if (reader != null) {
reader.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
@Override
protected void onPostExecute(final List<Mymodel> result) {
super.onPostExecute(result);
if (result != null) {
list.add(new Adapter(result));
adapter.notifyDataSetChanged();
}else{
Toast.makeText(getApplicationContext(), "manca internet", Toast.LENGTH_SHORT).show();
}
}
}
而不是list.add(新适配器(结果))代码>将新项目添加到适配器
内的列表中,即用户
。那就行了 而不是list.add(新适配器(结果))代码>将新项目添加到适配器
内的列表中,即用户
。那就行了 试试这个
@Override
protected void onPostExecute(final List<Mymodel> result) {
if (result != null) {
// remove existing elements to prevent duplicates
list.clear();
// update dataset
list.addAll(result);
// notify the adapter
adapter.notifyDataSetChanged();
} else {
Toast.makeText(getApplicationContext(), "manca internet", Toast.LENGTH_SHORT).show();
}
}
@覆盖
受保护的void onPostExecute(最终列表结果){
如果(结果!=null){
//删除现有图元以防止重复
list.clear();
//更新数据集
list.addAll(结果);
//通知适配器
adapter.notifyDataSetChanged();
}否则{
Toast.makeText(getApplicationContext(),“manca internet”,Toast.LENGTH_SHORT.show();
}
}
试试这个
@Override
protected void onPostExecute(final List<Mymodel> result) {
if (result != null) {
// remove existing elements to prevent duplicates
list.clear();
// update dataset
list.addAll(result);
// notify the adapter
adapter.notifyDataSetChanged();
} else {
Toast.makeText(getApplicationContext(), "manca internet", Toast.LENGTH_SHORT).show();
}
}
@覆盖
受保护的void onPostExecute(最终列表结果){
如果(结果!=null){
//删除现有图元以防止重复
list.clear();
//更新数据集
list.addAll(结果);
//通知适配器
adapter.notifyDataSetChanged();
}否则{
Toast.makeText(getApplicationContext(),“manca internet”,Toast.LENGTH_SHORT.show();
}
}
@Override
protected void onPostExecute(final List<Mymodel> result) {
if (result != null) {
// remove existing elements to prevent duplicates
list.clear();
// update dataset
list.addAll(result);
// notify the adapter
adapter.notifyDataSetChanged();
} else {
Toast.makeText(getApplicationContext(), "manca internet", Toast.LENGTH_SHORT).show();
}
}