我正在尝试构建一个应用程序:我们可以在listView中添加一个播放器(播放器也存放在SQL库中),我想在每个播放器名称旁边添加一个按钮来编辑或删除它(创建一个AlertDialog,有两个选择)。目前,我只想删除ListView和SQL Base的项目,所以我尝试了一下我的代码,但现在,我的列表已经消失了!我没有发现问题
这是我的代码:
public class MainActivity extends ActionBarActivity {
Button AddPlayer=null;
EditText edittextplayer = null;
ArrayList<Player> Players = new ArrayList<Player>();
ListView playerListView;
DatabaseHandler dbHandler;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
AddPlayer=(Button) findViewById(R.id.button);
edittextplayer=(EditText) findViewById(R.id.editTextplayer);
playerListView=(ListView) findViewById(R.id.listView);
dbHandler = new DatabaseHandler(getApplicationContext());
AddPlayer.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Player player = new Player(dbHandler.getPlayerCount(), String.valueOf(edittextplayer.getText()));
if(!playerExists(player)){
dbHandler.createPlayer(player);
Players.add(player);
Toast.makeText(getApplicationContext(), String.valueOf(edittextplayer.getText()) + " has been added to your Players!", Toast.LENGTH_SHORT).show();
return;
}
Toast.makeText(getApplicationContext(), String.valueOf(edittextplayer.getText()) + " already exists. Please use a different name.", Toast.LENGTH_SHORT).show();
}
});
edittextplayer.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
public void onTextChanged(CharSequence s, int start, int before, int count) {
AddPlayer.setEnabled(String.valueOf(edittextplayer.getText()).trim().length() > 0);
}
@Override
public void afterTextChanged(Editable s) {
}
});
if (dbHandler.getPlayerCount() != 0)
Players.addAll(dbHandler.getAllPlayer());
populateList();
}
private boolean playerExists(Player player) {
String name = player.getName();
int playerCount = Players.size();
for (int i = 0; i < playerCount; i++) {
if (name.compareToIgnoreCase(Players.get(i).getName()) == 0)
return true;
}
return false;
}
private void populateList() {
ArrayAdapter<Player> adapter = new PlayerListAdapter(Players);
playerListView.setAdapter(adapter);
}
private class PlayerListAdapter extends ArrayAdapter<Player> {
ArrayList<Player> list = new ArrayList<Player>();
private PlayerListAdapter(ArrayList<Player> list) {
super (MainActivity.this, R.layout.listviewitem);
this.list = list;
}
@Override
public View getView(final int position, View convertview, ViewGroup parent) {
View view = convertview;
if (view == null) {
view = getLayoutInflater().inflate(R.layout.listviewitem, parent, false);
}
Player currentPlayer = Players.get(position);
TextView name = (TextView) view.findViewById(R.id.textviewplayer);
name.setText(currentPlayer.getName());
ImageButton deleteplayer = (ImageButton) findViewById(R.id.Imagebuttoneditcontact);
deleteplayer.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
list.remove(position); //or some other task
notifyDataSetChanged();
}
});
return view;
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
这是类DataBaseHandler
public class DatabaseHandler extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "PlayerManager",
TABLE_PLAYER = "players",
KEY_ID = "id",
KEY_NAME = "name";
public DatabaseHandler(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE " + TABLE_PLAYER + "("+KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + KEY_NAME + " TEXT)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_PLAYER);
onCreate(db);
}
public void createPlayer(Player player) {
SQLiteDatabase db = getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_NAME, player.getName());
db.insert(TABLE_PLAYER, null, values);
db.close();
}
public Player getPlayer(int id) {
SQLiteDatabase db = getReadableDatabase();
Cursor cursor = db.query(TABLE_PLAYER, new String[] { KEY_ID, KEY_NAME }, KEY_ID + "=?", new String[] { String.valueOf(id) }, null, null, null, null );
if (cursor != null)
cursor.moveToFirst();
Player player = new Player(Integer.parseInt(cursor.getString(0)), cursor.getString(1));
db.close();
cursor.close();
return player;
}
public void deleteContact(Player player) {
SQLiteDatabase db = getWritableDatabase();
db.delete(TABLE_PLAYER, KEY_ID + "=?", new String[] { String.valueOf(player.getId()) });
db.close();
}
public int getPlayerCount() {
SQLiteDatabase db = getReadableDatabase();
Cursor cursor = db.rawQuery("SELECT * FROM " + TABLE_PLAYER, null);
int count = cursor.getCount();
db.close();
cursor.close();
return count;
}
public int updatePlayer(Player player) {
SQLiteDatabase db = getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_NAME, player.getName());
int rowsAffected = db.update(TABLE_PLAYER, values, KEY_ID + "=?", new String[] { String.valueOf(player.getId()) });
db.close();
return rowsAffected;
}
public List<Player> getAllPlayer() {
List<Player> players = new ArrayList<Player>();
SQLiteDatabase db = getWritableDatabase();
Cursor cursor = db.rawQuery("SELECT * FROM " + TABLE_PLAYER, null);
if (cursor.moveToFirst()) {
do {
players.add(new Player(Integer.parseInt(cursor.getString(0)), cursor.getString(1)));
}
while (cursor.moveToNext());
}
cursor.close();
db.close();
return players;
}
}
这就是clas Player:
public class Player {
protected String name;
private int id;
public Player(int id, String name) {
this.name = name;
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@Override
public String toString() {
return "Player{" +
"name='" + name + '\'' +
", id=" + id +
'}';
}
}
非常感谢你的帮助,因为我很困难,我尝试了很多东西,但没有任何作用!这可能是一个愚蠢的错误..如果你还告诉我如何从我的DatabaseHandler删除播放器它将是伟大的! 再见