显示从android listview中的特定行开始的项目

时间:2018-04-22 00:18:25

标签: java android listview

我有一个包含100个项目的列表视图。创建ListView时会显示前10个项目。如果用户从菜单中单击ShowRemainingItems选项,我想显示11到100之间的项目。我尝试使用下面的代码,但它不起作用。单击菜单时列表视图没有被忽略。有人可以帮忙吗?

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // Init user list
    ListView list = (ListView) this.findViewById(R.id.dataList);
    this.listAdapter = new DataListAdapter(this, R.layout.list_view_cell);
    list.setAdapter(listAdapter);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.options_menu,menu);
    return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();
    if (id == R.id.ShowRemainingItems) {
     Toast.makeText(this,"refresh clicked",Toast.LENGTH_SHORT).show();
        listAdapter.clear();
        // Update the 'listData' according to your preferences like displaying the items from 11 to 100
        // Notify the adapter about the change
        updateDataList();
        listAdapter.notifyDataSetChanged();
    }
    return super.onOptionsItemSelected(item);
}

public void updateDataList() {
    Toast.makeText(this,"Update data list called",Toast.LENGTH_SHORT).show();
    ListView list = (ListView) this.findViewById(R.id.dataList);
    list.setSelectionFromTop(11,12);
    Toast.makeText(this,"setSelectionFromTop selected",Toast.LENGTH_SHORT).show();
}

Datalistadapter.java

public class DataListAdapter extends ArrayAdapter {

    private Context context;
    private ArrayList<User> userList;
    private int layoutRessource;

    public ArrayList<User> getUserList() {
        return userList;
    }

    public DataListAdapter(Context ctx, int layoutResourceId) {
        super(ctx, layoutResourceId);
        this.userList = new ArrayList<User>();
        this.layoutRessource = layoutResourceId;
        this.context = ctx;
    }

    public void addUser(User usr) {
        this.userList.add(usr);
    }

    public void removeUser(String usrId) {
        for (User usr : userList) {
            if (usr.getId().equals(usrId)) {
                this.userList.remove(usr);
            }
        }
    }

    @Override
    public void clear() {
        super.clear();
        if(userList != null)
            userList.clear();
    }

    @Override
    public int getCount() {
        //return this.userList.size();
        return Math.min(10, this.userList.size());
    }

    @Override
    public User getItem(int position) {
        return this.userList.get(position);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        View row = convertView;
        if (row == null) {
            LayoutInflater li = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            row = li.inflate(this.layoutRessource, null);
        }

        // Get row user
        User currentUser  = getItem(position);
        Log.d(TAG, "SIZE: " + this.userList.get(position));

        // Id
        TextView idLabel = (TextView) row.findViewById(R.id.id);

        return row;
    }
}

2 个答案:

答案 0 :(得分:0)

您需要先清除更新适配器的数据集,然后通知适配器有关更改的信息,以便它可以相应地更新UI。每次使用时都不要获取ListView并将适配器设置为它,而是将其设置为全局。

    ListView list;
    ArrayList<String> listData = new ArrayList<String>();
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    // Init user list
    list = (ListView) this.findViewById(R.id.dataList);
    this.listAdapter = new DataListAdapter(this, R.layout.list_view_cell, listData);
    list.setAdapter(listAdapter);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.options_menu,menu);
    return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();
    if (id == R.id.ShowRemainingItems)
    {
        Toast.makeText(this,"refresh clicked",Toast.LENGTH_SHORT).show();
        listData.clear();
        // Update the 'listData' according to your preferences like displaying the items from 11 to 100
        updateDataList();
        // Notify the adapter about the change
        listAdapter.notifyDataSetChanged();

    }
    else if(id == R.id.action_settings)
    {
        Toast.makeText(this,"Settings clicked",Toast.LENGTH_SHORT).show();
    }
    return super.onOptionsItemSelected(item);
}

答案 1 :(得分:0)

您的代码中存在多个问题。但是,我负责修复,这里是修改后的代码。请注意,代码未经过测试,因此请根据您的进一步要求进行修改。

我在你犯错的地方添加了评论。请仔细检查。感谢。

private ArrayList<User> userList;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // Initialize the userList here
    userList = new ArrayList<User>();
    userList = getFirst10Items();

    // Initialize ListView and pass the userList into your adapter
    ListView list = (ListView) this.findViewById(R.id.dataList);
    this.listAdapter = new DataListAdapter(this, R.layout.list_view_cell, userList);
    list.setAdapter(listAdapter);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.options_menu,menu);
    return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();
    if (id == R.id.ShowRemainingItems) {
        // Remove the following. This will not be needed
        // listAdapter.clear();

        updateDataList();
        // Not necessary. As the updateDataList has already the notifyDataSetChanged command
        // listAdapter.notifyDataSetChanged();
    }
    return super.onOptionsItemSelected(item);
}

public void updateDataList() {
    // Do not initiate the ListView again. Just use the ListView that is already created.
    // ListView list = (ListView) this.findViewById(R.id.dataList);

    // This is not the way of selecting items in your ListView. I think your knowledge of how adapter works is wanting
    // list.setSelectionFromTop(11,12);

    // Here's the new implementation. 
    ArrayList<User> userListFor11To100 = getTheRemainingUsers();
    userList.addAll(userListFor11To100);
    listAdapter.notifyDataSetChanged();
}

public List<User> getTheRemainingUsers() {
    // You need to implement the getTheRemainingUsers function yourself which will get the data for the position 11 to 100 in the ListView
    // Just use userList.add(user) function instead of using listAdapter.addUser() function
}

public List<User> getFirst10Items() {
    // You need to implement the getFirst10Items function yourself which will get the data for the position 0 to 10 in the ListView
    // Just use userList.add(user) function instead of using listAdapter.addUser() function
}

现在您需要修改您的适配器,如下所示。

public class DataListAdapter extends ArrayAdapter {

    private Context context;
    private ArrayList<User> userList;
    private int layoutRessource;

    // Modify the constructor to get the userList passed from the activity to the adapter.
    public DataListAdapter(Context ctx, int layoutResourceId, ArrayList<User> userList) {
        super(ctx, layoutResourceId);
        this.userList = userList;
        this.layoutRessource = layoutResourceId;
        this.context = ctx;
    }

    public void addUser(User usr) {
        this.userList.add(usr);
        // Call the notifyDataSetChanged after adding each user to the list
        notifyDataSetChanged();
    }

    public void removeUser(String usrId) {
        for (User usr : userList) {
            if (usr.getId().equals(usrId)) {
                this.userList.remove(usr);
                break;
            }
        }
        // Call the notifyDataSetChanged after removing each user to the list
        notifyDataSetChanged();
    }

    @Override
    public void clear() {
        super.clear();
        if(userList != null) {
            userList.clear();
            // Call the notifyDataSetChanged after removing all user from the list
            notifyDataSetChanged();
        }
    }

    @Override
    public int getCount() {
        return this.userList.size();
        // The following statement is wrong!! Return the actual size of the list all the time. 
        // return Math.min(10, this.userList.size()); 
    }

    @Override
    public User getItem(int position) {
        return this.userList.get(position);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        View row = convertView;
        if (row == null) {
            LayoutInflater li = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            row = li.inflate(this.layoutRessource, null);
        }

        // Get row user
        User currentUser  = getItem(position);
        Log.d(TAG, "SIZE: " + this.userList.get(position));

        // Id
        TextView idLabel = (TextView) row.findViewById(R.id.id);

        // Set something to the TextView. Now its showing nothing. 
        idLabel.setText(currentUser.getId());

        return row;
    }
}