我有两项活动。第一个包含将从数据库中的行填充的字符串列表,第二个活动显示您从中选择的数据库中的所有行。
我们的想法是从数据库中的玩家那里为运动队挑选15名球员。
我想要做的是在点击后删除第二个活动中从列表中挑选的玩家,即我不能选择同一个玩家两次。
以下是我的活动:
public class PickTeamActivity extends Activity {
ListView listView;
String[] values;
String context;
Bundle extras;
public static final String PREFS = "examplePrefs";
Button finish;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pickteam);
SharedPreferences exmaple = getSharedPreferences(PREFS, 0);
context = exmaple.getString("teamName", "cant find team");
extras = getIntent().getExtras();
// Get ListView object from xml
listView = (ListView) findViewById(R.id.listViewPlayers);
// Defined Array values to show in ListView
values = new String[] { "No.1 ", "No.2 ", "No.3 ", "No.4 ", "No.5 ",
"No.6 ", "No.7 ", "No.8 ", "No.9 ", "No.10 ", "No.11 ",
"No.12 ", "No.13 ", "No.14 ", "No.15 "
};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, android.R.id.text1, values);
// Assign adapter to ListView
View v = getLayoutInflater().inflate(R.layout.footer_view, null);
listView.addFooterView(v);
listView.setAdapter(adapter);
finish = (Button) findViewById(R.id.btn_finishPicking);
finish.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent i = new Intent(PickTeamActivity.this, OptionsActivity.class);
startActivity(i);
}
});
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// ListView Clicked item index
int itemPosition = position;
// ListView Clicked item value
String itemValue = (String) listView
.getItemAtPosition(position);
// Show Alert
Toast.makeText(
getApplicationContext(),
"Position :" + itemPosition + " ListItem : "
+ itemValue, Toast.LENGTH_LONG).show();
// .................................................................................
Bundle passdata = new Bundle();
passdata.putInt("position", itemPosition);
passdata.putString("value", itemValue);
Intent passIntent = new Intent(PickTeamActivity.this,
ViewMatchPlayersActivity.class);
passIntent.putExtras(passdata);
startActivityForResult(passIntent, 0);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
finish.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent i = new Intent(PickTeamActivity.this, OptionsActivity.class);
startActivity(i);
}
});
//TODO handle here.
Bundle b = data.getExtras();
// ArrayList<Integer> removeList = new ArrayList<Integer>();
int i = b.getInt("row");
int p = b.getInt("previous_pos");
String v = b.getString("previous_val");
MySQLiteAdapters regadapter = new MySQLiteAdapters(this);
Cursor c = regadapter.queryRecord(i);
if (c.moveToFirst()) {
do {
values[p] = v + ": " + c.getString(0) + " " + c.getString(1);
} while (c.moveToNext());
}
ArrayList<Integer> removeList = new ArrayList<Integer>();
removeList.add(i);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, android.R.id.text1, values);
View view = getLayoutInflater().inflate(R.layout.footer_view, null);
listView.addFooterView(view);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// ListView Clicked item index
int itemPosition = position;
// ListView Clicked item value
String itemValue = (String) listView
.getItemAtPosition(position);
// Show Alert
Toast.makeText(
getApplicationContext(),
"Position :" + itemPosition + " ListItem : "
+ itemValue, Toast.LENGTH_LONG).show();
// .................................................................................
Bundle passdata = new Bundle();
passdata.putInt("position", itemPosition);
passdata.putString("value", itemValue);
Intent passIntent = new Intent(PickTeamActivity.this,
ViewMatchPlayersActivity.class);
passIntent.putExtras(passdata);
startActivityForResult(passIntent, 0);
}
});
}
public void onBackPressed()
{
Intent i = new Intent(PickTeamActivity.this, OptionsActivity.class);
startActivity(i);
}
}
公共类ViewMatchPlayersActivity扩展了Activity {
ListView nameList;
MySQLiteAdapters adapter_ob;
MySQLiteHelper helper_ob;
SQLiteDatabase db_ob;
Button registerBtn;
Cursor cursor, cursor2, cursor3;
String val, team;
int pos;
String[] removeList;
public static final String PREFS = "examplePrefs";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_match_players);
nameList = (ListView) findViewById(R.id.listView2);
adapter_ob = new MySQLiteAdapters(this);
SharedPreferences exmaple = getSharedPreferences(PREFS, 0);
team = exmaple.getString("teamName", "cant find team");
Bundle extras = getIntent().getExtras();
pos = extras.getInt("position");
val = extras.getString("value");
String[] from = { helper_ob.KEY_FNAME, helper_ob.KEY_LNAME,
helper_ob.KEY_POSITION, helper_ob.KEY_TEAMNAME };
int[] to = { R.id.tv_fname, R.id.tv_lname, R.id.tv_position,
R.id.tv_teamName };
cursor = adapter_ob.queryName(team);
SimpleCursorAdapter cursorAdapter = new SimpleCursorAdapter(this,
R.layout.row, cursor, from, to);
nameList.setAdapter(cursorAdapter);
nameList.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView arg0, View arg1, int arg2,
long arg3) {
//ArrayList<Integer> removeList = new ArrayList<Integer>();
Cursor listCursor = (Cursor) arg0.getItemAtPosition(arg2);
int playerRecord = listCursor.getInt(listCursor
.getColumnIndex("p_id"));
//removeList.add(playerRecord);
adapter_ob.playerPicked(playerRecord, "YES", pos + 1);
Bundle passdata = new Bundle();
passdata.putInt("row", playerRecord);
passdata.putInt("previous_pos", pos);
passdata.putString("previous_val", val);
Intent passIntent = getIntent();
passIntent.putExtras(passdata);
setResult(RESULT_OK, passIntent);
finish();
}
});
}
}
正如您所看到的,我将从数据库中获取玩家列表:
cursor = adapter_ob.queryName(team);
我想以某种方式编辑该列表并删除之前使用我在此处获取的行ID选择的记录:
int playerRecord = listCursor.getInt(listCursor
.getColumnIndex("p_id"));
谢谢!
答案 0 :(得分:0)
我准备去睡觉,没有时间充分研究这个问题,但也许只是为了给你一些想法。所有适配器都有一个remove
方法,它接受一个对象参数。
因此,如果您可以使用playerRecord
获取要删除的对象,则可以使用remove
方法从适配器中删除,并在适配器上调用notifyDataSetChanged
以更新您的结果。
否则,您可能希望编写自定义适配器并实现自己的remove方法,该方法会从自定义适配器数据源中删除该项。
答案 1 :(得分:0)
管理来解决这个问题会更加深思熟虑。
我所做的是更改cursor = adapter_ob.queryName(team);
以获取字符串
即。cursor = adapter_ob.queryName(team, string);
并更改了queryName();不从字符串返回记录而不显示它们的方法,即
public Cursor queryName(String team, String filter2) {
String[] context = {team};
opnToWrite();
Cursor mCursor = database_ob.rawQuery("SELECT PLAYER_Local._id AS p_id, * FROM PLAYER_Local, TEAM_local "
+ "WHERE PLAYER_Local.team_id = TEAM_local._id "
+ "AND team_name = ? "
+ "AND p_id not in ("+filter2+")"
+ "ORDER BY first_name ", context);
return mCursor;
}
这条线; "AND p_id not in ("+filter2+")"
,不在。
我通过使用 SharedPreferences 类并创建List&lt;来填充初始字符串。字符串&gt;并在每次单击某个项目时将记录的ID添加到列表中,然后将列表转换为字符串以由 SharedPreferences 存储。然后,当我需要时,我拉下了弦。
完整课程如下所示。
public class PickTeamActivity extends Activity {
ListView listView;
String[] values= new String [15];
String context;
Bundle extras;
Editor editor;
SharedPreferences exmaple;
String previousContext;
public static final String PREFS = "examplePrefs";
public Button finish;
@Override
protected void onSaveInstanceState(Bundle changedState) {
changedState.putStringArray("array", values);
//common bug fix
changedState.putString("WORKAROUND_FOR_BUG_19917_KEY", "WORKAROUND_FOR_BUG_19917_VALUE");
super.onSaveInstanceState(changedState);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pickteam);
exmaple = getSharedPreferences(PREFS, 0);
context = exmaple.getString("teamName", "cant find team");
//extras = getIntent().getExtras();
// Get ListView object from xml
listView = (ListView) findViewById(R.id.listViewPlayers);
// Defined Array values to show in ListView
if(savedInstanceState != null){
values = savedInstanceState.getStringArray("array");
}
else{
int size = exmaple.getInt("array_size", 15);
for(int i1=0; i1<size; i1++)
values[i1] = exmaple.getString("array_" + i1, "No." + (i1 + 1));
}
//add values to sharedPrefs
editor = exmaple.edit();
editor.putInt("array_size", values.length);
for(int x=0;x<values.length; x++)
editor.putString("array_" + x, values[x]);
editor.commit();
//call custom method
onListItemClick();
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode ==RESULT_OK){
Bundle b = data.getExtras();
int i = b.getInt("row");
int p = b.getInt("previous_pos");
MySQLiteAdapters regadapter = new MySQLiteAdapters(this);
Cursor c = regadapter.queryRecord(i);
if (c.moveToFirst()) {
do {
values[p] = "No." + (p+1) + ": " + c.getString(0) + " " + c.getString(1);
} while (c.moveToNext());
}
editor.remove("array_" + p);
editor.putString("array_" + p, values[p]);
editor.commit();
onListItemClick();
System.out.println("resultcode = " + "RESULT OK");
}else{
System.out.println("resultcode = " +"RESULT NOT OK");
}
}
public void onListItemClick() {
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, android.R.id.text1, values);
// Assign adapter to ListView
View v = getLayoutInflater().inflate(R.layout.footer_view, null);
listView.addFooterView(v);
listView.setAdapter(adapter);
finish = (Button) findViewById(R.id.btn_finishPicking);
finish.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent i = new Intent(PickTeamActivity.this, OptionsActivity.class);
startActivity(i);
}
});
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// ListView Clicked item index
int itemPosition = position;
// ListView Clicked item value
String itemValue = (String) listView
.getItemAtPosition(position);
// Show Alert
Toast.makeText(
getApplicationContext(),
"Position :" + itemPosition + " ListItem : "
+ itemValue, Toast.LENGTH_LONG).show();
// .................................................................................
Bundle passdata = new Bundle();
passdata.putInt("position", itemPosition);
passdata.putString("value", itemValue);
passdata.putString("teamContext", context);
Intent passIntent = new Intent(PickTeamActivity.this,
ViewMatchPlayersActivity.class);
passIntent.putExtras(passdata);
startActivityForResult(passIntent, 0);
}
});
}
//public void onBackPressed()
//{
// Intent i = new Intent(PickTeamActivity.this, OptionsActivity.class);
// startActivity(i);
//}
}
public class ViewMatchPlayersActivity extends Activity {
ListView nameList;
MySQLiteAdapters adapter_ob;
MySQLiteHelper helper_ob;
SQLiteDatabase db_ob;
Button registerBtn;
Cursor cursor, cursor2, cursor3;
String val, team;
Editor editor;
int pos;
String filter;
List<String> removeList;
public static final String PREFS = "examplePrefs";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_match_players);
nameList = (ListView) findViewById(R.id.listView2);
adapter_ob = new MySQLiteAdapters(this);
SharedPreferences exmaple = getSharedPreferences(PREFS, 0);
team = exmaple.getString("teamName", "cant find team");
Bundle extras = getIntent().getExtras();
editor = exmaple.edit();
pos = extras.getInt("position");
val = extras.getString("value");
String[] from = { helper_ob.KEY_FNAME, helper_ob.KEY_LNAME,
helper_ob.KEY_POSITION, helper_ob.KEY_TEAMNAME };
int[] to = { R.id.tv_fname, R.id.tv_lname, R.id.tv_position,
R.id.tv_teamName };
String test = exmaple.getString("removePlayers", "notFound");
if (test.equals("notFound")){
removeList = new ArrayList<String>();
filter = removeList.toString().replace("[", "").replace("]", "").replace(", ", ",");
editor.putString("removePlayers",filter );
editor.commit();
}else{
filter = exmaple.getString("removePlayers", "ERROR");
}
if (filter.length()== 2 || filter.length()== 3){
filter = filter.substring(1);
}
cursor = adapter_ob.queryName(team, filter);
SimpleCursorAdapter cursorAdapter = new SimpleCursorAdapter(this,
R.layout.row, cursor, from, to);
nameList.setAdapter(cursorAdapter);
nameList.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView arg0, View arg1, int arg2,
long arg3) {
//ArrayList<Integer> removeList = new ArrayList<Integer>();
Cursor listCursor = (Cursor) arg0.getItemAtPosition(arg2);
int playerRecord = listCursor.getInt(listCursor
.getColumnIndex("p_id"));
//removeList.add(playerRecord);
adapter_ob.playerPicked(playerRecord, "YES", pos + 1);
removeList = new ArrayList<String>();
removeList.add(filter);
removeList.add(Integer.toString(playerRecord));
filter = removeList.toString().replace("[", "").replace("]", "").replace(", ", ",");
editor.putString("removePlayers",filter );
editor.commit();
Bundle passdata = new Bundle();
passdata.putInt("row", playerRecord);
passdata.putInt("previous_pos", pos);
passdata.putString("previous_val", val);
Intent passIntent = getIntent();
passIntent.putExtras(passdata);
setResult(RESULT_OK, passIntent);
finish();
}
});
}
}