我有一个 FavoriteList.Class,我有一个列表视图,我正在从本地数据库中填充数据。但是列表中的 onClickItem 存在一个问题,始终显示相同的 ID。
假设我有 item A--> Name: John , ID:10 和 Item B --> Name: Poul , ID: 25 等等。
每次我单击列表中的项目时,它都会在 toast 消息中显示 ID 10,问题可能是什么?
public class FavoriteList extends ListActivity {
public static final String ROW_ID = "row_id";
private ListView fortrListView;
private CursorAdapter conAdapter;
String TAG = "Favorite Test : ";
DatabaseConnector dbConnector = new DatabaseConnector(FavoriteList.this);
@SuppressWarnings("deprecation")
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
fortrListView=getListView();
fortrListView.setOnItemClickListener(viewFotrukneListener);
String[] from = new String[] { "location_name", "location_id" };
int[] to = new int[] { R.id.FavoriteTextView , R.id.location_id_favotire_saver };
conAdapter = new SimpleCursorAdapter(FavoriteList.this, R.layout.fortrukne_list, null, from, to);
// set adapter
setListAdapter(conAdapter);
registerForContextMenu(getListView());
//gemme på sharedprefences
SharedPreferences prefs = getSharedPreferences("PREFERENCE", MODE_PRIVATE);
boolean firstrun = prefs.getBoolean("firstrun", true);
// bruges til at forklare hvordan man tilføjer manuelt...
if (firstrun) {
SharedPreferences.Editor editor = prefs.edit();
editor.putBoolean("firstrun", false);
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) {
editor.apply();
} else {
editor.commit();
}
// alert
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("For at tilføje en genbrugsplads, gå ind i "+"Nærmeste"+" vælg en genbrugsplads og bruge menuen til at tilføje!")
.setCancelable(false)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
//do things
}
});
AlertDialog alert = builder.create();
alert.show();
}
// gem staten
getSharedPreferences("PREFERENCE", MODE_PRIVATE)
.edit()
.putBoolean("firstrun", false)
.commit();
}
// onResume Metoden.n
@Override
protected void onResume()
{
super.onResume();
new GetFavorite().execute((Object[]) null);
}
// onStop metoden
@Override
protected void onStop()
{
Cursor cursor = conAdapter.getCursor();
if (cursor != null)
cursor.deactivate();
conAdapter.changeCursor(null);
super.onStop();
}
//A-syncTask til vise hele objekter fra DB.
private class GetFavorite extends AsyncTask<Object, Object, Cursor>
{
@Override
protected Cursor doInBackground(Object... params)
{
dbConnector.open();
return dbConnector.getAllFavorite();
}
@Override
protected void onPostExecute(Cursor result)
{
conAdapter.changeCursor(result); // set the adapter's Cursor
dbConnector.close();
}
}
// enkelt item bliver valgt og bliver sendt til en anden activity
OnItemClickListener viewFotrukneListener = new OnItemClickListener()
{
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3)
{
Intent in = new Intent(FavoriteList.this, ShowPlace.class);
in.putExtra(ROW_ID, arg3);
TextView tv = (TextView)findViewById(R.id.location_id_favotire_saver);
String genus = tv.getText().toString();
Log.d(TAG, "this is value :" +genus);
Log.d(TAG, "this is value :" +ROW_ID);
Toast.makeText(getApplicationContext(),"this is the value: " + genus, Toast.LENGTH_LONG).show();
//startActivity(in);
//overridePendingTransition(R.anim.right_to_left, R.anim.right_to_left);
}
};
//viser xml når man trykke
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.stock_item_menu, menu);
}
//menu bliver kaldt
public boolean onContextItemSelected(MenuItem item) {
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
switch(item.getItemId()) {
case R.id.remove_item:
try{
dbConnector.deletePlace(conAdapter.getItemId(info.position));
}
finally
{
Intent intent = getIntent();
finish();
startActivity(intent);
Toast.makeText(this, "Slettet",
Toast.LENGTH_SHORT).show();
}
return true;
default:
return super.onContextItemSelected(item);
}
}
}