Android ListView的问题。

qingchange 2014-12-18 11:25:26
我想在一个*.xml文件里面实现位每次都动态增加数据。用ListView。其中用SimpleAdapter来实现。不过运行效果是这的。



我的xml文件是这样定义的。为什么会这样呢?该怎么改?
[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]

改怎么改才可以啊。
...全文
299 19 打赏 收藏 转发到动态 举报
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
_xianfeng99 2014-12-21
  • 打赏
  • 举报
回复
有时间结下贴
大崔527 2014-12-20
  • 打赏
  • 举报
回复
这,,确实没太看懂。。。
qingchange 2014-12-20
  • 打赏
  • 举报
回复
引用 17 楼 lxfhjjsfq 的回复:
activity_main.xml

<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和添加删除操作,多写多试,然后看看网上资料。
这次帮大忙了!!
_xianfeng99 2014-12-20
  • 打赏
  • 举报
回复
activity_main.xml

<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和添加删除操作,多写多试,然后看看网上资料。
_xianfeng99 2014-12-20
  • 打赏
  • 举报
回复

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;
}
}

}


qingchange 2014-12-20
  • 打赏
  • 举报
回复
引用 14 楼 lxfhjjsfq 的回复:
你还是不理解adapter啊,在adapter中添加数据就可以更新ui了,ListView的adpater本来就有这个功能,不过一般不用系统的adapter,一般都是继承BaseAdapter,这样要灵活的多。一会我贴个代码给你参考下。
多谢了,我也是正在学习之中。。
_xianfeng99 2014-12-20
  • 打赏
  • 举报
回复
你还是不理解adapter啊,在adapter中添加数据就可以更新ui了,ListView的adpater本来就有这个功能,不过一般不用系统的adapter,一般都是继承BaseAdapter,这样要灵活的多。一会我贴个代码给你参考下。
qingchange 2014-12-20
  • 打赏
  • 举报
回复
引用 12 楼 lxfhjjsfq 的回复:
用系统的adapter就要搞清楚怎么用,你直接把 R.layout.activity_main给SimpleAdapter了,不就是在显示吗? Activity的布局是一个,是这个页面的布局,你代码的xml部分没有问题,但是你的SimpleAdapter的item的xml你再写一个啊,还用Activity的xml怎能按你的想法显示呢?

   <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布局文件要正确。
如果是单独放在一个xml文件里是可以正常显示的。不过我就是需要在按钮下有一个ListView并可以动态跟新ListView里面的内容。。这个可以怎么做?
_xianfeng99 2014-12-20
  • 打赏
  • 举报
回复
用系统的adapter就要搞清楚怎么用,你直接把 R.layout.activity_main给SimpleAdapter了,不就是在显示吗? Activity的布局是一个,是这个页面的布局,你代码的xml部分没有问题,但是你的SimpleAdapter的item的xml你再写一个啊,还用Activity的xml怎能按你的想法显示呢?

   <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布局文件要正确。
ChocolateTan 2014-12-19
  • 打赏
  • 举报
回复
你把item里的button删掉,放在listview的页面啊
dai_jiawei 2014-12-19
  • 打赏
  • 举报
回复
确实不知道你想表达什么
s_wenze 2014-12-19
  • 打赏
  • 举报
回复
上下item布局和java代码
「已注销」 2014-12-19
  • 打赏
  • 举报
回复
没看明白你想要表达什么意思
jiang920627 2014-12-19
  • 打赏
  • 举报
回复
没看懂!!!
qingchange 2014-12-19
  • 打赏
  • 举报
回复
引用 4 楼 dai_jiawei 的回复:
确实不知道你想表达什么
抱歉,昨晚说得不清楚。。。。
qingchange 2014-12-19
  • 打赏
  • 举报
回复
引用 7 楼 woshidef 的回复:
楼主的意思是simpleadapter传入的是这个xml,然后动态改变name addr phone?
就是这样。
qingchange 2014-12-19
  • 打赏
  • 举报
回复
抱歉各位,昨晚宿舍快断网了,由于匆忙,说得不清楚。

我打算让一个布局显示这样的信息:
1 , 两个按钮;
2,下面是一个ListView,可以增加数据。



但是在添加完一条数据后是这样显示的。


就是在新添加的数据上多了两个按钮,这不是想要的结果。

我的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>


Activity中关键代码如下:
		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});
麦饼 2014-12-19
  • 打赏
  • 举报
回复
楼主的意思是simpleadapter传入的是这个xml,然后动态改变name addr phone?
liudashao0 2014-12-19
  • 打赏
  • 举报
回复
你在代码里把那两个button也放在listview里面了,你把SimpleAdapter的代码贴出来看看!

80,361

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧