2

我有一个 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);
    }
  }
}
4

3 回答 3

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

于 2013-06-19T16:44:08.897 回答
0

尝试

public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3)  {         
arg0.getItemAtPosition(arg2);
于 2013-06-19T16:08:58.357 回答
0

解决 了,我是这样解决的:

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

在 onItemClick 中添加到同一类。

于 2013-06-22T00:11:32.677 回答