リスト ビューに新しい項目を追加すると、以前の項目が消えます。最新のアイテムのみを表示します。
名前とメールの2つの編集ビューがあります。そして、ボタン作成プレーヤー。ボタンをクリックすると、名前と電子メールが 1 つの項目 (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;
}
}