ListView混排:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="add" />
<ListView
android:id="@+id/list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
public class MainActivity extends Activity {
private String[] Name = { "Davi", "Danis", "Horker", "Jim", "Tom", "John" };
private ArrayList<String> mArrayList;
private ListAdapter mAdapter;
private ListView mListView;
private int count;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// mListView = new ListView (this);使用这初始化后,需要使用setContentView(mListView)刷新界面,因为你初始化的listview不再main的布局文件中,这里实际上是替换了布局,没有达到布局文件中的效果,仅仅是一个listview 看不见button
count = 0;
mArrayList = new ArrayList<String>();
mListView = (ListView) findViewById(R.id.list);//获取布局文件中的ListView,以后都是操作的main.xml中的布局中的list部分
mAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_multiple_choice, mArrayList);
Button mButton = (Button) findViewById(R.id.button1);
mListView.setAdapter(mAdapter);//更新界面中的list信息
mButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (count < Name.length) {
mArrayList.add(Name[count]);
count++;
mListView.setAdapter(mAdapter);
} else {
Log.e("Listtest","Name has not");
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
没有继承ListActivity类。
自定义Adapter:
http://www.cnblogs.com/wangjianhui/archive/2011/06/15/2081714.html 根据这篇文章学习的结果,介绍的不错
人口:
package com.example.myselfadapter;
import java.util.ArrayList;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.widget.ListView;
public class AdpActivity extends Activity {
private MyAdapter mMyAdapter;
private ListView mListView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_adp);
//自定义Adapter的使用,和一般的Adapter使用差别不大,主体需要实现的部分可以在自定义的类中实现
mListView = (ListView)findViewById(R.id.list);
mMyAdapter = new MyAdapter(this);
mListView.setAdapter(mMyAdapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.adp, menu);
return true;
}
}
MyAdapter.java:
package com.example.myselfadapter;
import java.util.ArrayList;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
//自定义Adapter的关键在与实现几个内部函数,getcount,getItem,getItemId,getView4个函数。getcount返回Adapter中包含的list的个数;getItem根据位置返回list列表中其中一项;getItemId根据位置返回ID值,一般返回位置就可以了;getView返回自定义的listView中其中一个item的布局的view,布局可以自己实现,获取布局信息后使用list列表里面存储的信息填充进item布局中的每一个元素然后返回。
public class MyAdapter extends BaseAdapter {
private Context mContext;
private LayoutInflater mLayoutInflater;
private ArrayList<String> mArrayList;
private String[] Name = {"Davi", "Danis", "Horker", "Jim", "Tom", "John"};
//构造函数根据需求编写
public MyAdapter(Context context) {
mContext = context;
mLayoutInflater = LayoutInflater.from(mContext);
mArrayList = new ArrayList<String>();
for (int i = 0; i < Name.length; i++) {
mArrayList.add(Name[i]);
}
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return mArrayList.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return mArrayList.get(position);
}
@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
if (convertView == null)
convertView = mLayoutInflater.inflate(R.layout.item,null);
TextView mTextView = (TextView)convertView.findViewById(R.id.text);
mTextView.setText(mArrayList.get(position));
return convertView;
}
}
main.xml 布局文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
//main布局文件
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<ListView
android:id = "@+id/list"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
item.xml listview中每一个item的布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<TextView
android:id="@+id/text"
android:layout_width="242dp"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>