I have a set of data that has been populated
final Set<String> set = prefs.getStringSet("favoritedrinks", null);
final List<String> list = new ArrayList<String>(set);
final ListView lv = (ListView) getActivity().findViewById(R.id.display_saved_drinks_list_view);
final ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(getActivity(), R.layout.list_item, R.id.product_name,
list);
lv.setAdapter(arrayAdapter);
this set is updated in an onclicklistener of a listview
list.remove(value);
arrayAdapter.notifyDataSetChanged();
Set<String> set = new HashSet<String>();
set.addAll(list);
editor.putStringSet("favoritedrinks", set);
editor.commit();
What I am trying to do is pull data from a database, based off the listview item. the listview item name is the content stored within the sqlite "name" column. What would I set the cursor to in order to do this? I have tried iterating through the length of the array only, I keep getting
android.database.CursorIndexOutOfBoundsException: Index 1 requested, with a size of 1 after the first item is selected. For reference, here is my entire method:
public class MyFavoriteCocktails extends Fragment {
protected ListView lv;
protected ListAdapter adapter;
SQLiteDatabase db;
Cursor cursor;
// The onCreateView method is called when Fragment should create its View object hierarchy,
// either dynamically or via XML layout inflation.
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
// Defines the xml file for the fragment
return inflater.inflate(R.layout.my_favorite_cocktails, parent, false);
}
// This event is triggered soon after onCreateView().
// Any view setup should occur here. E.g., view lookups and attaching view listeners.
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
SharedPreferences prefs = getActivity().getSharedPreferences("User3", Context.MODE_PRIVATE);
final SharedPreferences.Editor editor = prefs.edit();
final Set<String> set = prefs.getStringSet("favoritedrinks", null);
if (!(set ==null)) {
final List<String> list = new ArrayList<String>(set);
final ListView lv = (ListView) getActivity().findViewById(R.id.display_saved_drinks_list_view);
final ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(getActivity(), R.layout.list_item, R.id.product_name,
list);
lv.setAdapter(arrayAdapter);
db = (new DrinksDB(getActivity())).getWritableDatabase();
cursor = db.rawQuery("SELECT * FROM drinks WHERE", null);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
detail(position);
}
});
lv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
public boolean onItemLongClick(AdapterView parentView, View childView, int position, long id) {
final String value = lv.getItemAtPosition(position).toString();
AlertDialog.Builder alertDialog = new AlertDialog.Builder(getActivity());
// Setting Dialog Title
alertDialog.setTitle("Remove Item...");
// Setting Dialog Message
alertDialog.setMessage("Are you sure you want remove this item from your saved drinks?");
// Setting Positive "Yes" Button
alertDialog.setPositiveButton("YES", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getActivity().getApplicationContext(), value + " removed", Toast.LENGTH_SHORT).show();
list.remove(value);
arrayAdapter.notifyDataSetChanged();
Set<String> set = new HashSet<String>();
set.addAll(list);
editor.putStringSet("favoritedrinks", set);
editor.commit();
}
});
// Setting Negative "NO" Button
alertDialog.setNegativeButton("NO", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// Write your code here to invoke NO event
dialog.cancel();
}
});
// Showing Alert Message
alertDialog.show();
return false;
}
public void onNothingSelected(AdapterView parentView) {
}
});
}
//continued
}
public void detail(int position) {
int im = 0;
String _id = "";
String name = "";
String series = "";
String glassware = "";
String ingredients = "";
String directions = "";
if (cursor.moveToFirst()) {
cursor.moveToPosition(position);
//im = cursor.getInt(cursor.getColumnIndex("img"));
name = cursor.getString(cursor.getColumnIndex("name"));
series = cursor.getString(cursor.getColumnIndex("series"));
glassware = cursor.getString(cursor.getColumnIndex("glassware"));
ingredients = cursor.getString(cursor.getColumnIndex("ingredients"));
directions = cursor.getString(cursor.getColumnIndex("directions"));
//if column number of things -- loop new
}
Intent iIntent = new Intent(getActivity(), DrinksDB_Parse.class);
//iIntent.putExtra("dataIM", im);
iIntent.putExtra("dataName", name);
iIntent.putExtra("dataSeries", series);
iIntent.putExtra("dataGlassware", glassware);
iIntent.putExtra("dataIngredients", ingredients);
iIntent.putExtra("dataDirections", directions);
getActivity().setResult(getActivity().RESULT_OK, iIntent);
startActivityForResult(iIntent, 99);
}
}
Aucun commentaire:
Enregistrer un commentaire