2013-06-19 50 views
2

我有一個FavoriteList.Class,在那裏我有一個列表視圖,我從本地數據庫填充數據。但是,列表中的onClickItem出現問題始終顯示相同的ID。假設我有項目A - >名稱:John,ID:10 和項目B - >名稱:Poul,ID:25 等等。數據庫行錯誤ID

每當我點擊列表中的項目時,它會顯示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); 
    } 
    } 
} 
+1

似乎你需要使用位置參數,即'arg2' – ozbek

+0

我不想在列表中的位置,我想要列表中該項目的「location_id」。 – Tirolel

+0

使用customAdapter到你的listView – Houcine

回答

0

解決 好吧,我解決了這種方式:

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

加入到同一個班級,在onItemClick。

0

嘗試在這個適配器視圖的項目已被點擊時要調用

public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) {   
arg0.getItemAtPosition(arg2); 
0
public abstract void onItemClick (AdapterView<?> parent, View view, int position, long id) 

回調方法。

如果需要訪問與選定項目相關的數據,實施者可以調用getItemAtPosition(position)。

參數

父:

在點擊發生的適配器視圖。

視圖:

被點擊的適配器視圖內的視圖(這將是由適配器提供的視圖)

位置:

在適配器的視圖的位置。

ID

被點擊的項目的行ID。

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