Set selection at beginning of spinner with simplecursoradapter - android

I have an activity A with a listView. I've got a few items in there. If I click in one of those items, I navigate with an Intent to another page. In that other poage/Activity, I have a spinner, which contains the items of the listView ( which is populated with database). The problem is, every time, i go to that activity B, the item displayed in the spinner is always the first in my database. How can I set the item to be displayed depending on the item I clicked in the listView ?
Here's how I populate my spinner (dropdown mode) :
spinner = (Spinner)findViewById(R.id.spinner_branches);
final Cursor cursor = dbhelper.getAllCours();
String[] from = { "branche_cours" }; int[] to = { android.R.id.text1 };
adapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_1, cursor, from, to, 0);
spinner.setAdapter(adapter);
adapter.notifyDataSetChanged();
When I navigate, i pass the selected item with the intent. I remind you that my listView contains the same items of the spinner..
String selected_brancheold= intent.getExtras().getString("branche");
Thank you guys !

You can do one thing if your ListView list and Spinner List both are same.
Activity A
Intent intent = new Intent(YOUR_CONTEXT, ActivityB.class);
intent.putExtra("position",PASS_CURRENT_POSITION_LISTVIEW_ITEM_CLICKED);
startActivity(intent);
Activity B (After setting up the spinner adapter)
int currentPosition = getIntent().getIntExtra("position",0);
spinner.setSelection(currentPosition);
Hope this works for you!!

Related

Android Refresh Listview issue.

I am having an issue with a listview refresh, I have a refresh function that works on load and when I click a button tied to it. What it will not do is refresh after I add info to the SQLite database and call the refresh function.
//Add item to order Table.
public void addItemToOrder()
{
itemNumberValue = qoItemNumber.getText().toString();
orderQtyValue = qoOrderQty.getText().toString();
itemDescValue = qoItemDesc.getText().toString();
Intent searchItem = new Intent(getApplicationContext(),inputOrder.class);
searchItem.putExtra("itemnumb", itemNumberValue);
searchItem.putExtra("orderqty", orderQtyValue);
searchItem.putExtra("qoitemdesc", itemDescValue);
startActivity(searchItem);
fillData();
}
//Load item table to listview
public void fillData() {
// Get all of the rows from the database and create the item list
mNotesCursor = mDbHelper.fetchAllOrders();
startManagingCursor(mNotesCursor);
// Create an array to specify the fields we want to display in the list (only TITLE)
String[] from = new String[]{HDWDBHelper.KEY_ITEMNUM, HDWDBHelper.KEY_DESC, HDWDBHelper.KEY_QTY};
// and an array of the fields we want to bind those fields to (in this case just text1)
//int[] to = new int[]{R.id.text1};
int[] to = new int[]{R.id.tvItemNum, R.id.tvItemDesc, R.id.tvQty};
// Now create a simple cursor adapter and set it to display
SimpleCursorAdapter notes =
new SimpleCursorAdapter(this, R.layout.itemlist, mNotesCursor, from, to);
notes.notifyDataSetChanged();
setListAdapter(notes);
notes.notifyDataSetChanged();
Log.v("fillData", "In fillData");
}
When I call fillData() at onCreate() or on button click, it works but when I call it in the addItemToOrder() function, nothing happens. The listview still shows previous items listed until I click the refresh button then it shows the new item added.

How to move multiple selected items from listview to another listview

I have two listviews SAMPLE IMAGE HERE.i want to move items from one listview to another.
i have two buttons,"move to right" and "move to left".
Its a multi selection listview.after select items we need to move this items to another listview.
First listview data from database.code shown below
public void fillcategory() {
Cursor cursor = dataBase
.select("SELECT * FROM t_Category ORDER BY CategoryName");
lacategory = new list(this,
android.R.layout.simple_list_item_activated_1, cursor,
new String[] { "CategoryName" }, new int[] { android.R.id.text1 },1);
lvcategory.setAdapter(lacategory);
lvcategory.setOnItemClickListener(new listclick());
lvcategory.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
}
There are twio options you can do this, first by adding checkbox in each listitem and then populate the other listview with selected items. for this here is good tutorial Multiple Check
Second, you can set you listview property to multiple_select in adapter and then populated those selected item through position in other listview.
I hope atleast one will work.

Last item from database not appearing in listview after adding the item

I have a dialogfragment which adds an item to the database and immediately shows it in the listview .
The item added shows in the listview immediately but i don't know why the last item from the database doesn't appear .
It appears after executing the activity showing listview again , but not just after adding the item through my dialog fragment .
for example , if i have 3 items "a" , "b" and "d" in my listview and database , and when i add "c" , "d" doesn't show in the listview . it only shows after executing the fragment showing listview again .
Your adapter is using two different lists, the first is the superclass' list populated as object:
super(activity, R.layout.laundry_list_item, objects);
The other is your own:
private List my;
When you add an item to my the superclass, which provides the data for your ListView, does not know of this change until your restart the Activity.
Try changing your Save button to this:
nam = name.getText().toString();
pri = price.getText().toString();
i = new MyItem(nam, Double.parseDouble(pri));
db.addMyItem(i);
MyActivity f = (MyActivity) getActivity().getFragmentManager().findFragmentByTag("my");
f.getMyListAdapter().add(i);
// You might need this, but I believe the adapter will update itself in add()
//f.getMyListAdapter().notifyDataSetChanged();
getDialog().dismiss();
This will use the superclass' list and avoid your problem.
Alternatively with the code you have posted, I believe a SimpleCursorAdapter is a better choice than your own adapter.
In your Fragment:
public void onCreate() {
...
// Retrieve a Cursor of every row in the database, "SELECT _id, name, quantity FROM Table"
mCursor = db.getAllMyItemsCursor();
madapter = new SimpleCursorAdapter(this, R.layout.laundry_list_item, cursor,
new String[] { "name", "quantity" },
new int[] { R.id.item_name, R.id.item_qty });
}
public void updateListView() {
mCursor.requery();
madapter.notifyDataSetChanged();
}
In the Save Button:
nam = name.getText().toString();
pri = price.getText().toString();
db.addMyItem(new MyItem(nam, Double.parseDouble(pri)));
MyActivity f = (MyActivity) getActivity().getFragmentManager().findFragmentByTag("my");
f.updateListView();
Hope one of these helps!

Change items of a Spinner on item click of another Spinner in android

I have a question that, I want to load items in a spinner on ItemClick of another Spinner. Actually I two spinners, data is loaded into first spinner from json_parsing and I have to load data in second Spinner after selecting an item from first spinner, So, I don't know how it will implemented? Please suggest me the right solution.
Thanks in advance.
You can do it like this,
First time you data will be loaded in First and Second Spinner.
On Selection of item from First Spinner do this.
1.) Clear the previous ArrayList or Array whateven you have passed the
Second Spinner.
2.) Fill the ArrayList or Array of new data & Update the Second Spinnner using
adapter.notifyDataSetChanged();
second_spinner.setSelection(0);
First Set an OnItemClickListner for your First Spinner. In the OnItemClickListner Method first parse your XML. After completing XML parsing, set parsed data to the adapter and set that adapter with your second spinner
Set an OnItemClickListener on your first spinner that would prepare and set the adapter to the second spinner.
Here is a more complete code example:
Spinner spinner1 = (Spinner) findViewById(R.id.spinner1);
Cursor c1 = (some code for getting a cursor from an data source, for example, a sqlite database)
SimpleCursorAdapter adapter1 = new SimpleCursorAdapter(this, android.R.layout.simple_spinner_item, c1, new String[]{"column_name"}, new int[]{android.R.id.text1});
spinner1.setAdapter(adapter1);
Spinner spinner2 = (Spinner) findViewById(R.id.spinner2);
Cursor c2 = (some code for getting a cursor from an data source, for example, a sqlite database)
SimpleCursorAdapter adapter2 = new SimpleCursorAdapter(this, android.R.layout.simple_spinner_item, c2, new String[]{"column_name"}, new int[]{android.R.id.text1});
spinner2.setAdapter(adapter2);
spinner1.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Cursor c_new = (create a new cursor);
adapter2.changeCursor(c_new);
adapter2.notifyDataSetChanged(); // this is important for notifying the UI
spinner2.setAdapter(adapter2);
}
});
What you do is set a listener to the first Spinner, there change the Cursor of the second Adapter to a new one, notify the UI and reset the Adapter of the second Spinner.

Using a Spinner with a SimpleCursorAdapter

I have an activity which has a Spinner widget to display categories. Initially I was using an ArrayAdapter to populate the the spinner as in the following code
private static final String[] arrayCategories = {
"Business",
"Personal"
};
mCatSpinner = (Spinner) findViewById(R.id.thecategory);
ArrayAdapter<String> catAdapter = new ArrayAdapter<String>(this, R.layout.track_category_item, arrayCategories);
catAdapter.setDropDownViewResource(R.layout.track_category_dropdown_item);
mCatSpinner.setAdapter(catAdapter);
This works fine, and the spinner displays the first array item by default if no selection is made. It does show the selected item when an item is actually selected
But now I want to use a SimpleCursorAdapter to pull the list contents from a db. So I changed it to
SimpleCursorAdapter scaCategories = new SimpleCursorAdapter(this, R.layout.track_category_item,cCategories,new String[] {DBAdapter.KEY_CATEGORIES_NAME},new int[]{R.id.text1});
scaCategories.setDropDownViewResource(R.layout.track_category_dropdown_item);
mCatSpinner = (Spinner) findViewById(R.id.thecategory);
mCatSpinner.setAdapter(scaCategories);
This populates the dropdown, but it does not display the first item in the spinner. Even if selected, it does not show the selected item.
I tried to setSlection to the first item using
if(mCatSpinner.isSelected() != true) {
mCatSpinner.setSelection(0);
}
but it didn't work
What is wrong?
Ok, it would help if I specified the widget id in the layout xml. <:(

Categories

Resources