简体   繁体   English

单击按钮时未添加listview项

[英]listview item is not added on button click

I have an issue with adapter adding data to listview on button click. 适配器在单击按钮时将数据添加到Listview时出现问题。

I have a activity called createteam that has name and email edittext and a createplayer button. 我有一个名为createteam的活动,该活动具有名称和电子邮件edittext和createplayer按钮。 On click createplayer button, the name and email should be added to list view. 在单击createplayer按钮时,名称和电子邮件应添加到列表视图。

I want to use an adapter for doing that(so that I can learn). 我想使用适配器来做到这一点(以便我可以学习)。

Update: Onclicking the button the the first entry gets added but when I fill in the edittexts again, the listview does get updated. 更新:单击按钮会添加第一个条目,但是当我 再次 填写 edittexts时,listview确实会更新。 I am notifying the adapter when I update the data but only first entry gets and remains added to list view 我在更新 数据 时通知适配器, 但只有第一个条目被获取并保持添加到列表视图

create_team.xml 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 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 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 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 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;
        }
}
LayoutInflater vi =(LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.add_player_listview, null);

Change your code like this. 像这样更改您的代码。 You are not setting values to v variable. 您没有将值设置为v变量。

onClick of your button call list.notifyDataSetChanged() . 您的button调用list.notifyDataSetChanged() onClick It will refresh your list. 它将刷新您的列表。

Incase if doesn't work just check getCount() of your adapter what value it is returning. 如果不起作用,只需检查适配器的getCount()会返回什么值。 If it is returning 0 your list will not show any data even you call list.notifyDataSetChanged() . 如果返回0 ,则即使您调用list.notifyDataSetChanged()列表也不会显示任何数据。

Make an arrayList in your adapter and add new items into that. adapter一个arrayList ,然后向其中添加新项。 and from getCount() return size of arrayList . getCount()返回arrayList大小。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM