动态的ListView在Android应用 [英] Dynamic ListView in Android app

查看:111
本文介绍了动态的ListView在Android应用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个工作的例子,在那里,演示了如何动态添加额外行的ListView? 例如:

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

  1. 您是拉从RSS源 不同的域
  2. 您然后显示前10项 在ListView(当你有 在运行的其他线程 后台继续拉供稿)
  3. 您滚动并到达底部 列表并点击一个按钮, 查看更多项目
  4. 在ListView然后将得到追加 与另外10个项目,其中 使得目前共20个项目。
  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.

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

Any advice how to accomplish this?

尼古拉斯

推荐答案


新项目添加到列表中动态,你必须从你的ListActivity获得适配器类,并简单地添加新的元素。和视图更新自己 - 当您直接将项目添加到适配器,notifyDataSetChanged自动为您调用。

您也可以提供自己的适配器(扩展ArrayAdapter),并重写构造以列表参数。你可以使用这个列表,就像您使用的适配器,但在这种情况下,你必须调用adapter.notifyDataSetChanged()自己 - 刷新视图。
请看看下面的例子:


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.

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:

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);		
    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!

这篇关于动态的ListView在Android应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆