Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/343.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在android listview中显示从特定行开始的项目_Java_Android_Listview - Fatal编程技术网

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

Java 在android listview中显示从特定行开始的项目,java,android,listview,Java,Android,Listview,我有一个包含100个项目的列表视图。在创建列表视图时显示前10项。如果用户单击菜单中的ShowRemainingItems选项,我希望显示11到100之间的项目。我尝试使用以下代码,但无效。单击菜单时列表视图无法显示。有人能帮忙吗 protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main

我有一个包含100个项目的列表视图。在创建
列表视图时
显示前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;
    }
}
公共类DataListAdapter扩展了ArrayAdapter{
私人语境;
私有ArrayList用户列表;
私人内部布局资源;
公共ArrayList getUserList(){
返回用户列表;
}
公共DataListAdapter(上下文ctx,内部布局资源ID){
超级(ctx、layoutResourceId);
this.userList=new ArrayList();
this.layoutResource=layoutResourceId;
this.context=ctx;
}
public void addUser(用户usr){
this.userList.add(usr);
}
public void removeUser(字符串usrId){
for(用户usr:userList){
if(usr.getId().equals(usrId)){
this.userList.remove(usr);
}
}
}
@凌驾
公共空间清除(){
super.clear();
if(userList!=null)
userList.clear();
}
@凌驾
public int getCount(){
//返回此.userList.size();
返回Math.min(10,this.userList.size());
}
@凌驾
公共用户getItem(int位置){
返回this.userList.get(position);
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
视图行=转换视图;
if(行==null){
LayoutInflater li=(LayoutInflater)this.context.getSystemService(context.LAYOUT\u INFLATER\u SERVICE);
row=li.inflate(this.layoutResource,null);
}
//获取行用户
用户currentUser=getItem(位置);
Log.d(标记,“大小:”+this.userList.get(position));
//身份证
TextView idLabel=(TextView)row.findViewById(R.id.id);
返回行;
}
}

您需要首先清除“更新适配器的数据集”,然后将更改通知适配器,以便它可以相应地更新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);
}
ListView列表;
ArrayList listData=新的ArrayList();
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//初始化用户列表
list=(ListView)this.findviewbyd(R.id.dataList);
this.listAdapter=newdatalistadapter(this,R.layout.list\u view\u单元格,listData);
list.setAdapter(listAdapter);
}
@凌驾
公共布尔onCreateOptions菜单(菜单){
getMenuInflater().充气(右菜单.选项菜单,菜单);
返回super.onCreateOptions菜单(菜单);
}
@凌驾
公共布尔值onOptionsItemSelected(菜单项项){
int id=item.getItemId();
if(id==R.id.ShowRemainingItems)
{
Toast.makeText(这是“单击刷新”,Toast.LENGTH_SHORT).show();
listData.clear();
//根据您的偏好更新“listData”,例如显示11到100之间的项目
updateDataList();
//将更改通知适配器
listAdapter.notifyDataSetChanged();
}
else if(id==R.id.action\u设置)
{
Toast.makeText(这是“单击设置”,Toast.LENGTH_SHORT).show();
}
返回super.onOptionsItemSelected(项目);
}

您的代码中有几个问题。然而,我承担了修复的责任,下面是修改后的代码。请注意,代码未经测试,因此请根据您的进一步要求进行修改

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

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
}
private ArrayList用户列表;
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//在这里初始化用户列表
userList=newarraylist();
userList=getFirst10Items();
//初始化ListView并将userList传递到适配器中
ListView list=(ListView)this.findViewById(R.id.dataList);
this.listAdapter=newdatalistapter(this,R.layout.list\u view\u单元格,userList);
list.setAdapter(listAdapter);
}
@凌驾
公共布尔onCreateOptions菜单(菜单){
getMenuInflater().充气(右菜单.选项菜单,菜单);
返回super.onCreateOptions菜单(菜单);
}
@凌驾
公共布尔值onOptionsItemSelected(菜单项项){
int id=item.getItemId();
if(id==R.id.ShowRemainingItems){
//删除以下内容。不需要这样做
//listAdapter.clear();
updateDataList();
//不需要。因为updateDataList已经有notifyDataSetChanged命令
//listAdapter.notifyDataSetChanged();
}
返回super.onOptionsItemSelected(项目);
}
public void updateDataList(){
//不要再次启动ListView。只需使用已创建的ListView即可。
//ListView list=(ListView)this.findViewById(R.id.dataList);
//这不是在ListView中选择项目的方法。我认为您需要了解适配器的工作原理
//列表。设置从顶部选择(11,12);
//这是新的实现。
ArrayList UserListFor11到100=getTheRemainingUsers();
addAll(userlistfor11到100);
listAdapter.notifyDataSetChanged();
}
公共列表getTheRemainingUsers(){
//您需要自己实现getTheRemainingUsers函数,该函数将获取ListView中位置11到100的数据
//只需使用userList.add(user)函数,而不用listAdapter.addUser()函数
}
公共列表getFirst10Items(){
//您需要自己实现getFirst10Items函数,该函数将获取ListView中位置0到10的数据
//绝对法
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;
    }
}