長押しすると、このリストビューが各行のsqlite db _idを返すようにしようとしています。
ただし、この行
onItemLongClick(AdapterView<?> parent, View view, int position, long id)
IDの代わりにリスト内の「位置」を返しています。
私はアイデアを使い果たしました...誰かがAndroidの初心者を正しい方向に向けることができますか?
public class MySavedPlaces extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_saved_places);
//Creating a List View
ArrayList<String> listItems = new ArrayList<String>();
ArrayAdapter<String> adapter = new ArrayAdapter<String>(MySavedPlaces.this, android.R.layout.simple_list_item_1, listItems);
final ListView mylist=(ListView) findViewById(android.R.id.list);
mylist.setAdapter(adapter);
String URL = "content://com.db.place/locations";
Uri locations = Uri.parse(URL);
Cursor c = getContentResolver().query(locations, null, null, null, "name");
c.moveToFirst();
if(c!=null){
//If there are contents in the database
do{
String id=c.getString(c.getColumnIndex("_id"));
String name=c.getString(c.getColumnIndex("name"));
listItems.add(name);
}while(c.moveToNext());
}
mylist.setOnItemLongClickListener(new OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
ArrayAdapter<String> adapter = new ArrayAdapter<String>(MySavedPlaces.this, android.R.layout.simple_list_item_1);
adapter.add("Navigate...");
adapter.add("Delete");
AlertDialog.Builder builder = new AlertDialog.Builder(MySavedPlaces.this);
builder.setTitle("whatever title "+id+ " "+position+" "+view.getId());
builder.setAdapter(adapter, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
Log.d( "AlertDialog", "Positive"+item );
// item is the list item number - Navigate == 0, Delete == 1
}
});
AlertDialog alert = builder.create();
alert.show();
// Return true to consume the click event
return true;
}
});
}