Android 试图在recyclerview中获取json,但没有';我不会成功的
以前我使用过ArrayAdapter和列表视图,所以我编写了这段代码来更新UIAndroid 试图在recyclerview中获取json,但没有';我不会成功的,android,android-asynctask,android-recyclerview,Android,Android Asynctask,Android Recyclerview,以前我使用过ArrayAdapter和列表视图,所以我编写了这段代码来更新UI private class UserAsync extends AsyncTask<String,Void,List<User>>{ @Override protected List<User> doInBackground(String... urls) { if(urls.length <1 || urls[0] == null){
private class UserAsync extends AsyncTask<String,Void,List<User>>{
@Override
protected List<User> doInBackground(String... urls) {
if(urls.length <1 || urls[0] == null){
return null;
}
List<User> result = null;
try {
result = QueryUtils.fetchJson(urls[0]);
} catch (JSONException e) {
Log.e(LOG_TAG,"Error in fetching json",e);
}
return result;
}
@Override
protected void onPostExecute(List<User> users) {
// Clear the adapter of previous earthquake data
mAdapter.clear();
// If there is a valid list of {@link user}s, then add them to the adapter's
// data set. This will trigger the ListView to update.
if(users != null && !users.isEmpty()){
mAdapter.addAll(users);
// After adding user to the adapter, Notify adapter for UI update
mAdapter.notifyDataSetChanged();
}
}
}
items.xml
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="examle.android.com.recyclerviewnetwork.MainActivity">
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerview"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v7.widget.RecyclerView>
</android.support.constraint.ConstraintLayout>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/card"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:elevation="8dp"
app:cardCornerRadius="5dp"
android:layout_marginTop="5dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:padding="25dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<ImageView
android:id="@+id/avatar"
android:layout_width="108dp"
android:layout_height="108dp"
android:padding="15dp"
tools:src="@mipmap/ic_launcher" />
<LinearLayout
android:paddingRight="15dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/login"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="15dp"
tools:text="LOGIN_USER" />
<TextView
android:id="@+id/type"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="3dp"
tools:text="USER_TYPE" />
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
更新代码:
在您的活动中:
public class MainActivity extends AppCompatActivity {
RecyclerView recyclerView;
UserAdapter mAdapter;
RecyclerView.LayoutManager layoutManager;
private static final String JSON_URL = "https://api.github.com/users";
private static final String LOG_TAG = MainActivity.class.getSimpleName();
public ArrayList users=new ArrayList()
在异步任务中:
public class UserAsyncTask extends AsyncTask<String, Void, List<User>>{
@Override
protected List<User> doInBackground(String... urls) {
if(urls.length < 1 || urls[0] == null ){
return null;
}
try{
users = QueryUtils.fetchJson(urls[0]);
} catch (JSONException e) {
Log.e(LOG_TAG,"Error in fetching json",e);
}
return result;
}
@Override
protected void onPostExecute(List<User> users) {
super.onPostExecute(users);
if(users != null && !users.isEmpty()){
mAdapter.notifyDataSetChanged();
}
}
}
公共类UserAsyncTask扩展了AsyncTask{
@凌驾
受保护列表doInBackground(字符串…URL){
if(url.length<1 | | url[0]==null){
返回null;
}
试一试{
users=QueryUtils.fetchJson(URL[0]);
}捕获(JSONException e){
e(Log_标记,“获取json时出错”,e);
}
返回结果;
}
@凌驾
受保护的void onPostExecute(列出用户){
super.onPostExecute(用户);
if(users!=null&!users.isEmpty()){
mAdapter.notifyDataSetChanged();
}
}
}
或者使用与Listview相同的
onPostExecute
方法,并创建mAdapter.addAll(用户)代码>在recyclerview适配器中使用相同的方法。在适配器中创建以下方法:
要清除您的数据:
public void clear() {
int itemCount = getItemCount();
users.clear();
// notify your adapter that the data has been changed. You can also use notifyDataSetChanged() if you don't have an animation
if (itemCount > 0)
notifyItemRangeRemoved(0, itemCount);
}
要将项目添加到适配器,请执行以下操作:
public void addAll(List<User> addUsers) {
for (User user : addUsers) {
users.add(l_user);
// notify your adapter that the data has been changed. You can also use notifyDataSetChanged() if you don't have an animation.
// If you use notifyDataSetChanged() you can call it on the outside of this loop just once
notifyItemInserted(users.size());
}
}
除非您尝试调试并找到问题的确切位置,否则发布异常代码不会对您有所帮助。@HemantParmar我想知道如何实现这两种异步任务方法我尝试使用mAdapter.addAll(users),但在addAll()处显示错误method@shubhamkumar获取的错误是什么?请发布该错误无法解析方法“addAll”(java.util.ListError:(71,25)错误:找不到符号方法addAll(List)是否使用覆盖注释?
public class MainActivity extends AppCompatActivity {
RecyclerView recyclerView;
UserAdapter mAdapter;
RecyclerView.LayoutManager layoutManager;
private static final String JSON_URL = "https://api.github.com/users";
private static final String LOG_TAG = MainActivity.class.getSimpleName();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = (RecyclerView) findViewById(R.id.recyclerview);
mAdapter = new UserAdapter(users,this);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setAdapter(mAdapter);
UserAsyncTask task = new UserAsyncTask();
task.execute(JSON_URL);
}
public class UserAsyncTask extends AsyncTask<String, Void, List<User>>{
@Override
protected List<User> doInBackground(String... urls) {
if(urls.length < 1 || urls[0] == null ){
return null;
}
try{
users = QueryUtils.fetchJson(urls[0]);
} catch (JSONException e) {
Log.e(LOG_TAG,"Error in fetching json",e);
}
return result;
}
@Override
protected void onPostExecute(List<User> users) {
super.onPostExecute(users);
if(users != null && !users.isEmpty()){
mAdapter.notifyDataSetChanged();
}
}
}
public void clear() {
int itemCount = getItemCount();
users.clear();
// notify your adapter that the data has been changed. You can also use notifyDataSetChanged() if you don't have an animation
if (itemCount > 0)
notifyItemRangeRemoved(0, itemCount);
}
public void addAll(List<User> addUsers) {
for (User user : addUsers) {
users.add(l_user);
// notify your adapter that the data has been changed. You can also use notifyDataSetChanged() if you don't have an animation.
// If you use notifyDataSetChanged() you can call it on the outside of this loop just once
notifyItemInserted(users.size());
}
}
@Override
protected void onPostExecute(List<User> users) {
// clear the previous data
mAdapter.clear();
// add the new data
if(users != null)
mAdapter.addAll(users);
}