80,361
社区成员
发帖
与我相关
我的任务
分享
[code=html]
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="添加联系人" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="删除联系人" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="姓名" />
<TextView
android:id="@+id/addr"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="地址" />
<TextView
android:id="@+id/phone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="电话号码" />
</LinearLayout>
<ListView
android:id="@+id/listview"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
[/code]
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="添加联系人" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="删除联系人" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="姓名" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="地址" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="电话号码" />
</LinearLayout>
<ListView
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<TextView
android:id="@+id/item_name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="姓名" />
<TextView
android:id="@+id/item_addr"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="地址" />
<TextView
android:id="@+id/item_phone"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="电话号码" />
</LinearLayout>
剩下的工作就是你优化下adapter和添加删除操作,多写多试,然后看看网上资料。
import java.util.ArrayList;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
public class MainActivity extends Activity {
private MyAdapter adapter;
private int count = 0;//测试用变量
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView listview = (ListView) findViewById(R.id.listview);
adapter = new MyAdapter(this);
listview.setAdapter(adapter);
Button button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
adapter.addUser(new User("user" + count, "addr" + count, "1311111111" + count));
count++;
}
});
}
class MyAdapter extends BaseAdapter{
//存储数据的list
private ArrayList<User> mList = new ArrayList<User>();
private Context mContext;
public MyAdapter(Context context){
mContext = context;
}
//添加user
public void addUser(User user){
mList.add(user);
notifyDataSetChanged();//这句很重要,有了它,就可以刷新加入user后的ui了
}
//BaseAdapter的抽象方法需要实现
public int getCount() {
return mList.size();
}
//BaseAdapter的抽象方法需要实现
public Object getItem(int position) {
return mList.get(position);
}
//BaseAdapter的抽象方法需要实现
public long getItemId(int position) {
return position;
}
//BaseAdapter的抽象方法需要实现,显示的每条都是由这个方法得到的
public View getView(int position, View convertView, ViewGroup parent) {
User user = mList.get(position);
//可以用holder进行优化,具体可以网上百度一下
View view= LayoutInflater.from(mContext).inflate(R.layout.item, null);
TextView name = (TextView) view.findViewById(R.id.item_name);
TextView addr = (TextView) view.findViewById(R.id.item_addr);
TextView phone = (TextView) view.findViewById(R.id.item_phone);
name.setText(user.getName());
addr.setText(user.getAddr());
phone.setText(user.getPhone());
return view;
}
}
class User{
private String name;
private String addr;
private String phone;
public User(String name, String addr, String phone){
this.name = name;
this.addr = addr;
this.phone = phone;
}
public String getName() {
return name;
}
public String getAddr() {
return addr;
}
public String getPhone() {
return phone;
}
}
}
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/item_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="姓名" />
<TextView
android:id="@+id/item_addr"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="地址" />
<TextView
android:id="@+id/item_phone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="电话号码" />
</LinearLayout>
把上面代码写到另外一个xml中,如item.xml中
然后
adapter = new SimpleAdapter(MainActivity.this, mydata,
R.layout.item,
new String[] { "name", "addr", "phone" }, new int[] {
R.id.item_name,R.id.item_addr , R.id.item_phone});
这样在试试,Adapter的使用就是根据你提供的数据和xml来解析出显示的view,所以数据可提供的xml布局文件要正确。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="添加联系人" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="删除联系人" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="姓名" />
<TextView
android:id="@+id/addr"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="地址" />
<TextView
android:id="@+id/phone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="电话号码" />
</LinearLayout>
<ListView
android:id="@+id/listview"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
listview = (ListView) findViewById(R.id.listview);
Bundle bundle = data.getExtras();
mydata = new ArrayList<Map<String, String>>();
HashMap<String, String> map = new HashMap<String, String>();
if (requestCode == resultCode && resultCode == CODE
&& !bundle.isEmpty()) {
System.out.println("有信息*************");
map.put("name", bundle.getString("name"));
map.put("addr", bundle.getString("addr"));
map.put("phone", bundle.getString("phone"));
mydata.add(map);
}
adapter = new SimpleAdapter(MainActivity.this, mydata,
R.layout.activity_main,
new String[] { "name", "addr", "phone" }, new int[] {
R.id.name,R.id.addr , R.id.phone});