数据库行ID错误

时间:2013-06-19 15:52:44

标签: android database toast

我有一个FavoriteList.Class,我有一个listview,我从本地数据库填充数据。但是onClickItem从列表中出现的问题是始终显示相同的ID。

假设我有项目A - >姓名:John,身份证号码:10 和项目B - >姓名:Poul,身份证号码:25 等等。

每次我点击列表中的项目,它会显示ID 10,在toast信息中,问题可能是什么?

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);
    }
  }
}

3 个答案:

答案 0 :(得分:0)

尝试

public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3)  {         
arg0.getItemAtPosition(arg2);

答案 1 :(得分:0)

public abstract void onItemClick (AdapterView<?> parent, View view, int position, long id)

单击此AdapterView中的项目时要调用的回调方法。

如果需要访问与所选项目相关联的数据,实施者可以调用getItemAtPosition(position)。

参数

父:

发生点击的AdapterView。

视图:

单击AdapterView中的视图(这将是适配器提供的视图)

位置:

适配器中视图的位置。

ID

单击的项目的行ID。

http://developer.android.com/reference/android/widget/AdapterView.OnItemClickListener.html

答案 2 :(得分:0)

<强>解决 好吧,我这样解决了:

Cursor theCursor = ((SimpleCursorAdapter)((ListView)fortrListView).getAdapter()).getCursor();
String selection = theCursor.getString(theCursor.getColumnIndex("location_id"));

在onItemClick中添加到同一个类。