当我将新项目添加到列表视图时,我之前的项目消失了。它只显示最新项目。
我有两个editview,名称和电子邮件。一个按钮创建播放器。单击按钮时,名称和电子邮件应在列表视图中添加为一个项目(2行)。
请帮忙。
此致
CreateTeamActivity.java
package com.recscores.android;
import java.util.ArrayList;
import android.app.Activity;
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;
public class CreateTeamActivity extends Activity implements OnClickListener {
Button addPlayer;
Button createPlayer;
LinearLayout view_createPlayer;
TableLayout numberPad;
EditText Email;
EditText Name;
EditText teamname;
ListView Roster;
private CustomAdapter adapter;
private ArrayList<PlayerInfo> fetch = new ArrayList<PlayerInfo>();
private PlayerInfo info = new PlayerInfo();
private ListView lv;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.create_team);
addPlayer = (Button)findViewById(R.id.addplayer);
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);
adapter = new CustomAdapter(CreateTeamActivity.this,
android.R.id.list,
fetch);
lv.setAdapter(adapter);
createPlayer.setOnClickListener(this);
}
public void onClick(View v) {
switch(v.getId()){
case R.id.createplayer: /** Create Player */
info.SetName(Name.getText().toString());
info.SetEmail(Email.getText().toString());
fetch.add(info);
counter++;
// lv.invalidateViews();
adapter.notifyDataSetChanged();
//Thread.sleep(2000);
break;
}
}
}
CustomAdapter.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 CustomAdapter extends ArrayAdapter<PlayerInfo>{
private ArrayList<PlayerInfo> entries;
private Activity activity;
public CustomAdapter(Activity a, int textViewResourceId, ArrayList<PlayerInfo> entries) {
super(a, textViewResourceId, entries);
this.entries = entries;
this.activity = a;
}
public static class ViewHolder{
public TextView item1;
public TextView item2;
}
public int getCount()
{
return entries.size();
}
@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);
v = vi.inflate(R.layout.add_player_listview, null);
holder = new ViewHolder();
holder.item1 = (TextView) v.findViewById(R.id.name);
holder.item2 = (TextView) v.findViewById(R.id.email);
v.setTag(holder);
}
else
holder=(ViewHolder)v.getTag();
final PlayerInfo custom = entries.get(position);
if (custom != null) {
holder.item1.setText(custom.GetName());
holder.item2.setText(custom.GetEmail());
}
return v;
}
}
答案 0 :(得分:0)
尝试添加
PlayerInfo info = new PlayerInfo();
前
info.SetName(Name.getText().toString());