我遇到了适配器在按钮单击时将数据添加到列表视图的问题。
我有一个名为 createteam 的活动,其中包含名称和电子邮件编辑文本以及一个 createplayer 按钮。单击 createplayer 按钮后,名称和电子邮件应添加到列表视图中。
我想使用适配器来做到这一点(这样我就可以学习)。
更新:单击按钮会添加第一个条目,但是当我 再次填写编辑文本时,列表视图确实会更新。我在更新 数据时通知适配器,但只有第一个条目被获取并保持添加到列表视图
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>
创建团队活动.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;
}
}
播放器信息.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;
}
}