一尘不染

Android应用程序中的动态ListView

java

是否有一个可行的示例演示了如何在ListView中动态附加行?例如:

  1. 您正在从不同的域中提取RSS feed
  2. 然后在ListView中显示前10个项目(当您在后台运行其他线程时,继续提取提要)
  3. 您滚动并到达列表的底部,然后单击按钮以查看更多项目
  4. 然后,ListView将附加附加的10个项目,现在总共有20个项目。

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

尼古拉斯


阅读 272

收藏
2020-09-08

共1个答案

一尘不染

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

您还可以提供自己的适配器(扩展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;
    }       
}

}

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

2020-09-08