android - 回收适配器没有出现在视图寻呼机中

时间:2016-05-29 00:34:47

标签: java android android-fragments android-asynctask

我在ProfileActivity中有一个tablayout。其中一个tablayout将显示我使用asynctask从数据库检索的用户列表的循环视图。我使用getter方法从asynctask类返回用户列表

我的recycleview位于activity_user_view.xml中,我在扩展片段的类中设置了适配器。我没有收到任何错误,但tablayout没有出现在视图寻呼机中。

这是我的代码

FragmentFriends.java(扩展片段的类)

public class FragmentFriends extends Fragment {
ArrayList<UserList> aList = new ArrayList<>();
RecyclerView recyclerView;
RecyclerView.Adapter adapter;
RecyclerView.LayoutManager layoutManager;
@Nullable
@Override

public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View rootview = inflater.inflate(R.layout.activity_user_view,container,false);// The activity that contains recyclerview

    PHPUserList phpUserList = new PHPUserList(getActivity());
    phpUserList.execute();
    aList = phpUserList.getArrayList();
    recyclerView = (RecyclerView) rootview.findViewById(R.id.RVUserList);
    layoutManager = new LinearLayoutManager(getActivity());
    recyclerView.setLayoutManager(layoutManager);
    recyclerView.setHasFixedSize(true);
    adapter = new RecyclerAdapter(aList);
    recyclerView.setAdapter(adapter);

    return rootview;
}

}

PHPUserList.java(asynctask class)

public class PHPUserList extends AsyncTask<Void,UserList,Void> {

private Context context;
ArrayList<UserList> arrayList = new ArrayList<>();

PHPUserList(Context ctx){

    this.context = ctx;
}
@Override
protected Void doInBackground(Void... params) {
    try {
        URL url = new URL("http://njtwicomp.pe.hu/listuser.php");
        HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();
        InputStream inputStream = httpURLConnection.getInputStream();
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
        StringBuilder stringBuilder = new StringBuilder();
        String line;

        while ((line = bufferedReader.readLine())!=null)
        {
            stringBuilder.append(line+"\n");
        }

        httpURLConnection.disconnect();
        String json_string = stringBuilder.toString().trim();
        JSONObject jsonObject = new JSONObject(json_string);
        JSONArray jsonArray = jsonObject.getJSONArray("server_response");
        int count = 0;
        while(count<jsonArray.length()){
            JSONObject JO = jsonArray.getJSONObject(count);
            count++;
            UserList userList = new UserList(JO.getString("Name"),JO.getString("Username"));
            publishProgress(userList);
        }
        Log.d("JSON STRING", json_string);

    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (JSONException e) {
        e.printStackTrace();
    }
    return null;
}

@Override
protected void onPreExecute() {
    super.onPreExecute();
}

@Override
protected void onProgressUpdate(UserList... values) {
    arrayList.add(values[0]);
}

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

public ArrayList<UserList> getArrayList() {
    return arrayList;
}
}

请帮帮我

1 个答案:

答案 0 :(得分:1)

将项目添加到回收站视图或列表视图时,您需要通知适配器已发生这些更改,而您尚未执行此操作。

onProgressUpdate中,您想要致电adapter.notifyItemInserted(position)告诉RecyclerView刷新。要做到这一点,您需要在AsyncTask中使用回调或监听器,因为它没有对您的适配器的引用,并且它不应该。

请参阅此处了解您可以使用的所有通知电话:https://developer.android.com/reference/android/support/v7/widget/RecyclerView.Adapter.html