单击按钮时不会添加列表视图项

时间:2013-04-03 09:35:24

标签: android listview android-listview adapter android-arrayadapter

我有一个问题,即适配器在按钮点击时将数据添加到listview。

我有一个名为createteam的活动,它有名称和电子邮件edittext以及一个createplayer按钮。单击“创建播放器”按钮,应将名称和电子邮件添加到列表视图中。

我想使用适配器来做到这一点(这样我就可以学习)。

更新:  点击按钮,第一个条目会被添加,但是当我填写时  再次 edittexts,listview确实会更新。我在更新时通知适配器  数据但只有第一个条目获取并仍然添加到列表视图

create_team.xml

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
<LinearLayout 
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >


    <EditText
    android:id="@+id/teamname"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textColor="@color/blue"
    android:hint="Team Name"
    android:gravity="center"
     android:textStyle="bold">

    </EditText>



<Button
    android:id="@+id/addplayer"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textColor="@color/blue"
    android:text="Add Player"
    android:layout_gravity="center"
     android:textStyle="bold">

    </Button>



<LinearLayout
    android:id="@+id/view_addplayer"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
 >
 <Button
    android:id="@+id/createplayer"
       android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textColor="@color/red"
    android:text="Create Player"
     android:layout_gravity="center"
     android:textStyle="bold">

    </Button>
    <EditText
        android:id="@+id/name"
        android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textColor="@color/blue"
    android:hint="Name"
    android:gravity="center"
     android:textStyle="bold">

    </EditText>
        <EditText
    android:id="@+id/email"
    android:layout_marginTop="10dp"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
      android:textColor="@color/blue"

    android:hint="Email "
     android:textStyle="bold"

    android:gravity="center">

    </EditText>



  </LinearLayout>

      <ListView 
          android:id="@+id/android:list"
          android:layout_width="match_parent"
          android:layout_height="wrap_content">

          </ListView>


</LinearLayout>
</ScrollView>

add_player_listview.xml

  <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="100dp"
    android:orientation="horizontal" 
    android:weightSum="2">



<TextView android:id="@+id/name" 
android:textSize="20sp" 
android:textStyle="bold" 
android:color="@color/blue"
android:textColor="#FFFF00" 
android:layout_width="wrap_content" 
android:layout_height="50dp"/>

<TextView android:id="@+id/email"
android:textSize="15sp" 
android:textStyle="bold" 
android:color="@color/mustard"
android:layout_width="wrap_content" 
android:layout_height="50dp"/>
</LinearLayout>

CreateTeamActivity.java

package com.recscores.android;

import java.util.ArrayList;
import java.util.HashMap;

import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TableLayout;
import android.widget.Toast;
import com.recscores.android.PlayerInfo;

public class CreateTeamActivity extends ListActivity {
    Button createPlayer;
    EditText Email;
    EditText Name;
    private ArrayList<PlayerInfo> newList;
    private PlayerAddAdapter newAdpt;
    private int i = 0;
    private ListView lv;


    protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.create_team);

    newList = new ArrayList<PlayerInfo>();

    // CREATE TEAM
    getWindow().setBackgroundDrawableResource(R.drawable.app_background2);

    createPlayer = (Button)findViewById(R.id.createplayer);


    Email = (EditText)findViewById(R.id.email);
    Name = (EditText)findViewById(R.id.name);

    lv = (ListView)findViewById(android.R.id.list);
    newAdpt = new PlayerAddAdapter(CreateTeamActivity.this,android.R.id.list,newList);
    lv.setAdapter(newAdpt);

    createPlayer.setOnClickListener(new OnClickListener() {


        public void onClick(View v) {
        //  newList = new ArrayList<PlayerInfo>();
            PlayerInfo info = new PlayerInfo();
            info.SetName(Name.getText().toString());
            info.SetEmail(Email.getText().toString());
            newList.add(info);          
            newAdpt.notifyDataSetChanged();
            //Thread.sleep(2000);

            Name.setText("");
            Email.setText("");


            }
            });


    }


}

PlayerAddAdapter.java

package com.recscores.android;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;


public class PlayerAddAdapter extends ArrayAdapter<PlayerInfo> {
    private Activity activity;
    //private final Context mContext;
    private ArrayList<PlayerInfo> items;


    public PlayerAddAdapter(Activity a, int textViewResourceId, ArrayList<PlayerInfo> items){
    super(a,textViewResourceId,items);
    this.items=items;
    this.activity = a;
    }

    public static class ViewHolder{
        public TextView name;
        public TextView email;
    }
    @Override


    public View getView(int position, View convertView, ViewGroup parent) {
        View v = convertView;
        ViewHolder holder;
        if (v == null) {
            LayoutInflater vi =
                (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                 vi.inflate(R.layout.add_player_listview, null);
            holder = new ViewHolder();
            holder.name = (TextView)v.findViewById(R.id.name);
            holder.email = (TextView)v.findViewById(R.id.email);

            v.setTag(holder);
        }
        else
            holder=(ViewHolder)v.getTag();

        final PlayerInfo custom = items.get(position);
        if (custom != null) {
            **holder.name.setText(custom.GetName());
            holder.email.setText(custom.GetEmail());
        }
        return v;
    }

}

PlayerInfo.java

package com.recscores.android;

public class PlayerInfo {

    private String name="";
    private String email="";

        public void SetName(String name){
        this.name = name;
        }
        public String GetName(){
        return this.name;
        }

        public void SetEmail(String email){
        this.email = email;
        }
        public String GetEmail(){
        return this.email;
        }
}

2 个答案:

答案 0 :(得分:4)

LayoutInflater vi =(LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.add_player_listview, null);

像这样更改您的代码。您没有将值设置为v变量。

答案 1 :(得分:0)

onClick来电button的{​​p> list.notifyDataSetChanged()。它会刷新你的清单。

如果不起作用,请检查适配器的getCount()返回的值。如果它返回0,即使您拨打list.notifyDataSetChanged(),您的列表也不会显示任何数据。

arrayList中设置adapter,然后在其中添加新项目。并且getCount()的返回大小为arrayList