How can i display selected item in spinner A show in spinner B..?
<string-array name="type_report">
<item>Emergency</item>
<item>Sponsor</item>
<item>House</item>
</string-array>
Both of spinner used same "type_report" ..spinner A and B will show Emergency in first position. My question is when i choose "House" in spinner A in activity home , then spinner B in other activity will show "House" in first position ..
spinner A list
<item>Emergency</item>
<item>Sponsor</item>
<item>House</item>
after choose "House"
spinner B list will show
<item>House</item>
<item>Emergency</item>
<item>Sponsor</item>
spinnerA.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
/**
* Called when a new item is selected (in the Spinner)
*/
public void onItemSelected(AdapterView<?> parent, View view,
int pos, long id) {
Intent intent=new Intent(this,SecondaActivity.class);
intent.putExtra("index",pos);
startActivity(intent);
}
public void onNothingSelected(AdapterView<?> parent) {
// Do nothing, just another required interface callback
}
});
Now in your secondActivity you have to place the selected index's item on first position like this
Intent mIntent = getIntent();
int pos= mIntent.getIntExtra("index", 0);
String valueAtIndex = yourArray[pos];
for(int i = pos; i > 0; i--){
yourArray[i] = yourArray[i-1];
}
yourArray[0] = valueAtIndex;
//now set this array to second Spinner
ArrayAdapter spinnerBArrayAdapter = new ArrayAdapter(this,
android.R.layout.simple_spinner_dropdown_item,
yourArray);
spinnerB.setAdapter(spinnerArrayAdapter);
Tested and working code
Related
I have 3 string text 'A' , 'B' , 'C' and it is added in ListView . And one TexView as Title .
1 . When i click on List-view Item 'A' this item will be display in TexView and Item 'A' remove from ListView.Only 'B','C' remianing in List.
2 . When i click on List-view Item 'B' this item will be display in TexView and same Item 'B' remove from ListView and 'A','C' will be showing in ListView.
3 . When i click on List-view Item 'C' this item will be display in TexView and same Item 'C' remove from ListView and 'A','B' will be showing in ListView.
It means first i have 3 item in my ListView after adding in textView as title only 2 item will be display every time in ListView. How to work with this logic.Can someone help me .
Here is my code.
String[] values = new String[] { "A", "B", "C"};
arr = new ArrayList<String>(Arrays.asList(values));
filterPost_adapter = new ArrayAdapter<String>(getApplicationContext(),R.layout.filter_popup_list_item , R.id.text_filter_title, arr);
filter_list.setAdapter(filterPost_adapter);
filter_list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
String selectedFromList =(filter_list.getItemAtPosition(position).toString());
Log.e("selectedFromList ", " = " + selectedFromList);
removeItemFromList(position);
if(selectedFromList.equals("A"))
{
textSearch.setText("A");
populate_MyAllPostList();
}
if(selectedFromList.equals("B"))
{
textSearch.setText("B");
populateList();
}
if(selectedFromList.equals("B"))
{
textSearch.setText("B");
populate_StarPost();
}
}
});
protected void removeItemFromList(int position) {
final int deletePosition = position;
arr.remove(deletePosition);
filterPost_adapter.notifyDataSetChanged();
filterPost_adapter.notifyDataSetInvalidated();}
You only removed the item from your Activity ArrayList I supose
to update the Listview you have to call a method from the adapter and update it's arraylist before calling notifyDataSetChanged()
Exemple :
In your activity
-> here remove the item from the activity arraylist and then
filterPost_adapter.updatelist(arrayListWithRemovedItem);
In your adapter :
public void updateList(ArrayList list)
{
mLocalArrayList = list;
this.notifyDataSetChanged();
}
in on item click write below code:
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
if(filterPost_adapter.getCount()==3){
String data = filterPost_adapter.getItem(position);
textVeiw.setText(data);
filterPost_adapter.remove(data);
}else{
String temp = textView.getText().toString();
String data = filterPost_adapter.getItem(position);
textVeiw.setText(data);
filterPost_adapter.remove(data);
filterPost_adapter.add(temp);
}
}
I have a listview which is populated by an array adpater. A user starts on activity a and after clicking an item in the listview is taken to activity b where they can edit the item they clicked. The edited item from B is sent back to A through an intent after the user returns to A. I want the original item clicked in A to be replaced with the data received from the intent from B.
ArrayAdapter<Comment> adapter = new ArrayAdapter<Comment>(this,
android.R.layout.simple_list_item_1, values);
// setListAdapter(adapter);
final ListView listView = (ListView) findViewById(R.id.list);
listView.setAdapter(adapter);
//intent receiving data from Activity B; I want to add in statement to change the original item clicked to this new String, edit;
Bundle fromedit = getIntent().getExtras();
if (fromedit != null) {
String edit = fromedit.getString("BENG");
int ps = fromedit.getInt("POSITION");
}
//onclicklistener for the listview
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Intent intent = new Intent(Notes.this, EditNote.class);
intent.putExtra("KEY", values.get(position).toString());
intent.putExtra("POSITION", position);
startActivity(intent);
}
});
Thanks!
In your original activity you need to keep an int variable referring to the selected item index on the list. So your onItemClick() would look like that:
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
currentSelected = position;
Intent intent = new Intent(Notes.this, EditNote.class);
intent.putExtra("KEY", values.get(position).toString());
intent.putExtra("POSITION", position);
startActivity(intent);
}
When you return from the activity where you modify the data you need to update the item of your values list and call adapter.notifyDataSetChanged().
Have you tried to update your list by calling mList.set(index, Object); and then refreshing the Adapter to update ListView displayed data?
You can use the ArrayList#set() method, thus if your 'values' dataset is an ArrayList.
The java.util.ArrayList.set(int index, E element) replaces the element at the specified position in this list with the specified element.
modify your code as follows:
//intent receiving data from Activity B; I want to add in statement to change the original item clicked to this new String, edit;
Bundle fromedit = getIntent().getExtras();
if (fromedit != null) {
String edit = fromedit.getString("BENG");
int ps = fromedit.getInt("POSITION");
values.set(ps, edit);
adapter.notifyDataSetChanged();
}
I have already created a listview with the codes as below that displays apple, orange and banana. When I click on the item (Eg: apple) I want it to be displayed in a different activity as a textview along with the value. Eg: apple = 40 cal. For now I do not have a database to store these values.
This is the code for the listview :
public class ViewMenuList extends ListActivity {
String[] food = { "Apple", "Banana", "Orange"
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.menu_list);
setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, food));
ListView listView = getListView();
listView.setTextFilterEnabled(true);
}
public void onListItemClick(ListView parent, View v, int position, long id)
{
Toast.makeText(this, "You have selected " + food[position] , Toast.LENGTH_LONG).show();
}
}
You can do a couple different things then. You can simply pass the value using an Intent
public void onListItemClick(ListView parent, View v, int position, long id)
{
Toast.makeText(this, "You have selected " + food[position] , Toast.LENGTH_LONG).show();
String name = food[position];
Intent i = new Intent(CurrentActivityName.this, NextActivityName.class);
i.putExtra("foodName", name);
startActivity(i);
}
then retrieve it in the next Activity in onCreate()
Intent intent = getIntent();
String foodName = intent.getStringExtra("foodName");
Then call setText(foodName) on your TextView in the next Activity
You also could store the information in SharedPreferences or in a static variable in a different class that would hold associated information with the food item.
I have this code i am used to pass an array list to another page and show it as a listview. When the list shows up, i want to be able to check an item and remove it at "button click" which will modify the array.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.oppout);
final ListView lv2 = (ListView) findViewById (R.id.custom_list_view);
lv2.setClickable(true);
lv2.setAdapter(new ArrayAdapter<String>(Oppout.this,
android.R.layout.simple_list_item_checked,
Entername.playerList));
lv2.setOnItemClickListener (
new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView adapterView,
View view,int arg2, long arg3) {
int selectedPosition = adapterView.getSelectedItemPosition();
Toast.makeText(getBaseContext(), "mu"+ selectedPosition,
Toast.LENGTH_SHORT).show();
}
});
Button next = (Button) findViewById(R.id.button1);
next.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
// on click call the adapterview and delete string at selected position.
// This is my problem, am not getting how to call the adapter and deleted
// the selected item/position
int selectedPosition = adapterView.getSelectedItemPosition();
adapterView.remove(player.SelectedPosition);
Intent myIntent = new Intent (view.getContext(), Callacab.class);
startActivityForResult(myIntent, 0);
}
});
}}
contacts.remove(index); //the arraylist you gave it to your adapter
arrayadapter.remove(index); // this is your adapter that you give it to the listview
arrayadapter.notifyDataSetChanged();
//you can delete from your arraylist or your adapter and then notifyDataSetChanged(); to make the effect happen in your listview....it better to delete from arraylist and its indexs will be the arg2 in your listview item listener
I have created a ListView where I have to add different items. Now, when I click on a particular item it displays another window. On that window, I want to display the name of that item which I click on the ListView.
My code:
private ListView contactList;
private String lv_arr[]={"Android","iPhone","BlackBerry","AndroidPeople"};
#Override
public void onCreate(Bundle icicle)
{
super.onCreate(icicle);
setContentView(R.layout.contact_activity);
contactList=(ListView)findViewById(R.id.ListView01);
contactList.setAdapter(new ArrayAdapter<String (this,android.R.layout.simple_list_item_1 , lv_arr));
contactList.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
/*Intent myIntent = new Intent(view.getContext(), CallActivity.class);
startActivity(myIntent);*/
}
});
}
you have to get the name of the item on itemclick event. pass it to the activity which will be called. in in the calling activity get the name of item and display
contactList.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
String selecteditem = lv_arr[position];
Intent myIntent = new Intent(view.getContext(), CallActivity.class);
intent.putExtra("item", selecteditem);
startActivity(myIntent);
}
});
In CallActivity.java
write the following to get the selected item name
String selectedItem=getIntent().getStringExtra("item");
Instead of starting a new activity, use AlertDialog. You already have the position of the list item clicked. So displaying it on the dialog shouldn't be a problem if you follow the article linked.
EDIT :
As per your requirement, you have to launch a new activity to display a string
In the sending list activity
intent.putExtra(String key, String value)
In receiving activity,
String value = getIntent().getStringExtra(key);