Java Android 应用中的动态 ListView

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1917773/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 01:24:07  来源:igfitidea点击:

Dynamic ListView in Android app

javaandroidlistviewdynamic

提问by Nicholas Key

Is there a working example out there that demonstrates how to append additional rows in ListView dynamically? For example:

是否有一个工作示例演示如何在 ListView 中动态附加其他行?例如:

  1. you are pulling RSS feeds from different domains
  2. you then display the first 10 items in the ListView (while you have other threads running in the background continue pulling feeds)
  3. you scroll and reach the bottom of the List and click at a button to view more items
  4. the ListView will then get appended with additional 10 items, which makes 20 items now in total.
  1. 您正在从不同域中提取 RSS 提要
  2. 然后在 ListView 中显示前 10 个项目(同时在后台运行其他线程继续拉取提要)
  3. 您滚动并到达列表底部,然后单击按钮以查看更多项目
  4. 然后 ListView 将附加额外的 10 个项目,现在总共有 20 个项目。

Any advice how to accomplish this?

任何建议如何做到这一点?

Nicholas

尼古拉斯

采纳答案by Ramps

To add new item to your list dynamically you have to get adapter class from your ListActivity and simply add new elements. When you add items directly to adapter, notifyDataSetChanged is called automatically for you - and the view updates itself.

要将新项目动态添加到列表中,您必须从 ListActivity 获取适配器类并简单地添加新元素。当您将项目直接添加到适配器时,将自动为您调用 notifyDataSetChanged - 并且视图会自行更新。

You can also provide your own adapter (extending ArrayAdapter) and override the constructor taking List parameter. You can use this list just as you use adapter, but in this case you have to call adapter.notifyDataSetChanged() by yourself - to refresh the view.
Please, take a look at the example below:

您还可以提供自己的适配器(扩展 ArrayAdapter)并覆盖带 List 参数的构造函数。您可以像使用适配器一样使用此列表,但在这种情况下,您必须自己调用 adapter.notifyDataSetChanged() - 以刷新视图。
请看下面的例子:

public class CustomList extends ListActivity {
private LayoutInflater mInflater;
private Vector<RowData> data;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);        
    mInflater = (LayoutInflater) getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
    data = new Vector<RowData>();
    RowData rd = new RowData("item1", "description1");
    data.add(rd);
    rd = new RowData("item2", "description2");
    data.add(rd);
    rd = new RowData("item2", "description3");
    data.add(rd);

    CustomAdapter adapter = new CustomAdapter(this, R.layout.custom_row,R.id.item, data);
    setListAdapter(adapter);        
    getListView().setTextFilterEnabled(true);
}


public void onListItemClick(ListView parent, View v, int position, long id) {
    CustomAdapter adapter = (CustomAdapter) parent.getAdapter();
    RowData row = adapter.getItem(position);        
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle(row.mItem); 
    builder.setMessage(row.mDescription + " -> " + position );
    builder.setPositiveButton("ok", null);
    builder.show();
}

/**
 * Data type used for custom adapter. Single item of the adapter.      
 */
private class RowData {
    protected String mItem;
    protected String mDescription;

    RowData(String item, String description){
        mItem = item;
        mDescription = description;         
    }

    @Override
    public String toString() {
        return mItem + " " +  mDescription;
    }
}

private class CustomAdapter extends ArrayAdapter<RowData> {

    public CustomAdapter(Context context, int resource,
            int textViewResourceId, List<RowData> objects) {
        super(context, resource, textViewResourceId, objects);

    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;

        //widgets displayed by each item in your list
        TextView item = null;
        TextView description = null;

        //data from your adapter
        RowData rowData= getItem(position);


        //we want to reuse already constructed row views...
        if(null == convertView){
            convertView = mInflater.inflate(R.layout.custom_row, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        }
        // 
        holder = (ViewHolder) convertView.getTag();
        item = holder.getItem();
        item.setText(rowData.mItem);

        description = holder.getDescription();      
        description.setText(rowData.mDescription);

        return convertView;
    }
}

/**
 * Wrapper for row data.
 *
 */
private class ViewHolder {      
    private View mRow;
    private TextView description = null;
    private TextView item = null;

    public ViewHolder(View row) {
        mRow = row;
    }

    public TextView getDescription() {
        if(null == description){
            description = (TextView) mRow.findViewById(R.id.description);
        }
        return description;
    }

    public TextView getItem() {
        if(null == item){
            item = (TextView) mRow.findViewById(R.id.item);
        }
        return item;
    }       
}

}

}

You can extend the example above and add "more" button - which just add new items to your adapter (or vector).
Regards!

您可以扩展上面的示例并添加“更多”按钮 - 这只是将新项目添加到您的适配器(或矢量)。
问候!