将列表项添加到列表后如何更新?

时间:2016-07-02 06:54:58

标签: java android list arraylist android-recyclerview

我有一个主列表,其中我展示了一些选项。在这里我有一个事件选项,在主列表的第二个索引处添加。我有事件arrayList,我从asyncTask获取此列表。所以我在onPostExecute方法中得到了eventsArrayList的大小。

但我需要在主要列表的项目中显示此计数。在获得数组大小后,我尝试在onPstExceute中调用prepareData方法。但是,当它与服务器连接时,加载显示主UI需要花费很多时间。

所以我想首先将eventArrayList大小显示为0并使用新大小更新onPostexecute方法中的主列表Item。

但是这个剂量不会改变主列表中显示的计数。我在recyclerview上尝试了invalidate()仍然无法刷新回收器视图。

MainActivity:

public class MainActivity extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener,GetEventsAsyncTask.GetEvents {

    private RecyclerView recyclerView;

    private List<MainList> mainLists = new ArrayList<>();

    private String userUsername;

    public int PERMISSIONS_WRITE_EXTERNAL_STORAGE =10;
    public static final String MyPREFERENCES = "MyPrefs" ;
    SharedPreferences sharedpreferences;

    ArrayList<Event> eventArrayList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        sharedpreferences = getSharedPreferences("username",Context.MODE_PRIVATE);

        userUsername = sharedpreferences.getString("UserUsername","No name defined");

        GetEventsAsyncTask getEventsAsyncTask = new GetEventsAsyncTask(this,MainActivity.this,eventArrayList);
        getEventsAsyncTask.execute(userUsername);

        recyclerView = (RecyclerView) findViewById(R.id.recycler_view);

        eventArrayList = new ArrayList<>();


        prepareData();

        recyclerView.setAdapter(new MainListAdapter(mainLists, new MainListAdapter.OnItemClickListener() {
            @Override public void onItemClick(MainList item) {

                if(item.getTitle().equals("Plan Event")) {
                    startActivity(new Intent(MainActivity.this, PlanEventActivity.class));
                }
                if(item.getTitle().equals("Events")) {
                    startActivity(new Intent(MainActivity.this, EventsListActivity.class));
                }
                if(item.getTitle().equals("Photos")) {

                    photos = true;

                    checkFilePermissions();

                    sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);

                    permissionsAccepted = sharedpreferences.getBoolean("permission",false);

                    if(dir.exists()) {

                        listFile = dir.listFiles();

                        if (permissionsAccepted) {


                            if (listFile.length == 0) {


                                showAlert("No Images");

                            } else {

                                new MainActivity.SingleMediaScanner(MainActivity.this, listFile[0]);
                            }

                      }
                        else {

                            // permission denied, boo! Disable the
                            // functionality that depends on this permission.
                            showAlert("Please accept permissions to get access to photos.");

                        }
                    }
                    else {

                        showAlert("No Folder");
                    }

                }
                if(item.getTitle().equals("Reminders")) {
                    startActivity(new Intent(MainActivity.this, RemindersActivity.class));
                }
                if(item.getTitle().equals("Chat")) {

                }
                if(item.getTitle().equals("Notes")) {
                    startActivity(new Intent(MainActivity.this, NotesActivity.class));
                }
                if(item.getTitle().equals("Notifications")) {
                    startActivity(new Intent(MainActivity.this, NotificationsActivity.class));
                }
                if(item.getTitle().equals("Profile")) {
                    startActivity(new Intent(MainActivity.this, ProfileActivity.class));
                }
            }
        }));

        RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
        recyclerView.setLayoutManager(mLayoutManager);
        recyclerView.setItemAnimator(new DefaultItemAnimator());


    }

    private void prepareData() {


        MainList mainList = new MainList("Plan Event","", R.drawable.plan);
        mainLists.add(mainList);

        if(eventArrayList != null && eventArrayList.size() >= 0)
        {
            mainList = new MainList("Events",String.valueOf(eventArrayList.size()), R.drawable.events);
        }
        else
        {
            mainList = new MainList("Events", "0", R.drawable.events);

        }
        mainLists.add(mainList);


        listFile = dir.listFiles();

        if(listFile != null && listFile.length >= 0)
        {

            mainList = new MainList("Photos", String.valueOf(listFile.length), R.drawable.phone);
        }
        else
        {
            mainList = new MainList("Photos", "0", R.drawable.phone);
        }

        mainLists.add(mainList);

        mainList = new MainList("Reminders","20", R.drawable.remind);
        mainLists.add(mainList);

        mainList = new MainList("Chat","2", R.drawable.chat);
        mainLists.add(mainList);

        mainList = new MainList("Notes","2", R.drawable.notes);
        mainLists.add(mainList);

        mainList = new MainList("Notifications","20", R.drawable.remind);
        mainLists.add(mainList);

        mainList = new MainList("Profile","50%", R.drawable.user);
        mainLists.add(mainList);

    }

    @Override
    public void doPostExecute(ArrayList<Event> response) {

        eventArrayList = response;

        MainList mainList = new MainList("Events","eventArrayList.size()", R.drawable.events);

        mainLists.set(2,mainList);

        recyclerView.invalidate();

    }
}

怎么办?请帮忙。谢谢。

1 个答案:

答案 0 :(得分:0)

我认为您必须在班级中将MainListAdapter创建为私有字段,而不是在recyclerview setAdapter()调用期间将其添加为匿名类。

的确,在onPostExecute()回调中,您必须在更新notifyDataSetChanged()变量后调用适配器mainLists