如何将数据添加到Android中的listview?

时间:2014-08-05 11:02:41

标签: android listview

我尝试将数据添加到ListView。但我不知道如何将数据添加到Listview。

DemoActivity的代码如下所示:

public class DemoActivity extends Activity
{
    public static ListView StreamID_list;
    private static ArrayList<HashMap<String, Object>> StreamIDlist = new ArrayList<HashMap<String,Object>>();
    private static String[][] StreamList = new String[10][2];
    private StreamListAdapter mStreamListAdapter;
    private String[] list = {"1","2","3","4","5"};
   @Override
   protected void onCreate(Bundle savedInstanceState)
   {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_demo);


    StreamID_list = (ListView) findViewById(R.id.StreamID_list);

    mStreamListAdapter = new StreamListAdapter(DemoActivity.this);
    StreamID_list.setAdapter(mStreamListAdapter);

   }

   class HttpServiceConnection implements ServiceConnection
{
    public void onServiceConnected(ComponentName name, IBinder boundService)
    {
        service = IHttpService.Stub.asInterface((IBinder) boundService);
        Log.d(DemoActivity.TAG, "onServiceConnected() connected");
        Toast.makeText(DemoActivity.this, "Service connected", Toast.LENGTH_LONG).show();
    }

    public void onServiceDisconnected(ComponentName name)
    {
        service = null;
        Log.d(DemoActivity.TAG, "onServiceDisconnected() disconnected");
        Toast.makeText(DemoActivity.this, "Service disconnected", Toast.LENGTH_LONG).show();
    }
}

public class StreamListAdapter extends BaseAdapter{

    private LayoutInflater mInflator;
    ArrayList<String> mlist;
    public StreamListAdapter(Context context) {
        // TODO Auto-generated constructor stub
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        View view = null;
        if(convertView == null){
            view = getLayoutInflater().inflate(R.layout.streamid_data, null);
        }

        TextView id = (TextView) convertView.findViewById(R.id.ID_text);


        return view;
    }

如何将数据添加到listView并在手机上显示?

1 个答案:

答案 0 :(得分:0)

首先,您必须在创建适配器后将数据设置为适配器,或者创建包含适配器参数的构造函数

  public StreamListAdapter(ArrayList<String> mlist) {
      this.mlist = mlist;
  }

这是第一次就足够了,之后如果您修改数据列表,则必须调用

  notifyDataSetChanged();
适配器上的

;)