Spinner Item re-selected - java

I am implementing a spinner with auto fill text view and adding same list in adapter of Spinner and auto fill text view,
If I select a item from spinner it will be set in autoFillTextView,
But the problem is if I select a item from spinner and then make a change in autofillTextView, and then again select same item from spinner then controller is not going back to spinner onSelected method,
Thanks in advance.
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
switch (parent.getId()) {
case R.id.spinnerGroupName:
actvMatchGroup.setText("");
//Your Another Action Here.
for (int i = 0; i < groupList.size(); i++) {
if (groupName[position] == groupList.get(i).getGroupName()) {
groupId = Integer.parseInt(groupList.get(i).getGroupId());
group_id = groupId;
}
}
tvMatchGroup.setText(groupName[position]);
actvMatchGroup.setText(groupName[position]);
break;
}

Related

Dependent dropdown using Exposed Drop-Down Menu and AutoCompleteTextView

#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
//Define city spinner but we will populate the option through the selected state
policeStationSelectDropdown = findViewById(R.id.psAutoCompleteText);
// Obtain the selected district
selectedDistrict = districtSelectDropdown.getSelectedItem().toString();
Toast.makeText(Complaint_page.this,selectedDistrict, Toast.LENGTH_SHORT).show();
// int parentId = parent.getId();
if (parent.getId() == R.id.districtAutoCompleteText)
getSelectedItem() is showing error??
How to use this with autoCompleteTextView. I want dependent dropdown using Exposed Drop-Down Menu and autoCompleteTextView using JAVA.

without selecting how to pass the spinner value

Hi in the below code I have a edit button .If I am click on edit button setting the spinner values to the spinner .
click on edit button spinner value is setting the adapter.
String speclizations = getArguments().getString("speclization_name");
ArrayAdapter<String> dataAdapter1 = new ArrayAdapter<>(getContext(), android.R.layout.simple_spinner_item, Specializationnames);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerSpecialization.setAdapter(dataAdapter1);
if(spinnerSpecialization.getSelectedItem().equals(0)){
speclizations = getArguments().getString("speclizations");
Log.d("salutation_names",salutation_names);
}else {
speclizations=spinnerSpecialization.getSelectedItem().toString();
}
Same value I am passing after click on save button I am passing the spinner value without selecting want to pass the same value.but it is throwing an error.
spinner.getslecteditem().toString();//null
Can any one help me how to resolve the issue.
Try this
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
if(spinner.getslecteditem().equals(0))
{
//Display a toast message to select any values from the Spinner
}
else {
str = spinner.getslecteditem().toString();
}
}
#Override
public void onNothingSelected(AdapterView<?> parentView) {
}
});

Deselect current list view item if selected again

I currently have a list view in which you can select an item and it will highlight the currently selected item. But I would like to set it sot that so that if the user clicks on the same item currently selected in the list again it will unhighlight the list item and have no list item currently selected/
currentUsersCameras.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(final AdapterView<?> parent, View view, final int position, long id) {
for (int i = 0; i < parent.getChildCount(); i++) {
parent.getChildAt(i).setBackgroundColor(Color.TRANSPARENT);
view.setBackgroundColor(Color.rgb(3, 169, 244));
}
}
});
Listview/Recyclerview items requires you to handle if and else cases always. So if you want to do something for one item and not for another item, and similarly if you want to do and undo the same thing to the same item, handle both IF and ELSE

Android : get id of listview row when button clicked

i have a listview that contains relative views on each list item that are populated by a database and contains two buttons.
when one of the buttons in the list view item is pressed i need to get the id of that row in the list view. in other words i am trying to find the same thing as the last argument in the onItemClick methos for the onItemClickListener(), which would return a click for the entire item. however i am just listening for the button in the listview item so i am unable to use onItemClickListener().
i have this method being called when i click the button
public void plusClick(View v)
{
//get the row the clicked button is in
RelativeLayout vwParentRow = (RelativeLayout)v.getParent();
//i need to get the id of this RelativeLayout item in the list view
TextView child = (TextView)vwParentRow.getChildAt(2);
Button btnChild = (Button)vwParentRow.getChildAt(1);
int c = Color.CYAN;
vwParentRow.setBackgroundColor(c);
vwParentRow.refreshDrawableState();
}
it need to find the id of the relative layout which i can pass ass arg for
public Cursor getRow(long rowId) {
String where = KEY_ROWID + "=" + rowId;
Cursor c = db.query(true, DATABASE_TABLE, ALL_KEYS,
where, null, null, null, null, null);
if (c != null) {
c.moveToFirst();
}
return c;
}
to create a new cursor contiain the data in this listview item
try this code:
private OnItemClickListener itemClickListener = new OnItemClickListener() {
public void onItemClick(AdapterView<?> av, View v, int position,
long id) {
// write your logic here
// position starts from 0
// id starts from 1
}
};
when you set onclicklistener for your button you can set tag for it(tag can be id of row), and then when you click on button get it's tag. (of course if you use custom adapter)
You need some kind of adapter, in getView method you have to declare your event
There is an example:
holder.hMention.setOnClickListener(new VerTweet(mention));
and...
public class VerTweet implements View.OnClickListener
{
Mention mention;
public VerTweet ( Mention mention) {
this.mention = mention;
}
public void onClick( View view ){
Bundle bundle = new Bundle();
//Le ponemos la variable parametro con el contenido test
bundle.putInt("idTrack", mention.getIdTrack());
bundle.putString("Gifo", mention.getGifo());
bundle.putString("From", mention.getFromUser());
bundle.putString("Date", mention.getDateMentions());
bundle.putString("Text", mention.getText());
bundle.putLong("Status", mention.getLastId());
Intent i = new Intent(getContext(), Tweet.class);
i.putExtras(bundle);
getContext().startActivity(i);
}
}
https://github.com/m-alcu/SkoltiAlert/blob/master/src/com/alcubier/skoltialert/mentions/GDMentionsList.java

Android get Textview text on the same row of listview

I have this code inside my BaseAdapter Adapter:
public View getView(final int position, View convertView, ViewGroup parent) {
View vi=convertView;
if(convertView==null)
vi = inflater.inflate(R.layout.facebook_friends_layout, null);
TextView friendsName = (TextView)vi.findViewById(R.id.tvFacebookName); // Friends thumb
friend_image=(ImageView)vi.findViewById(R.id.ivFacebookPicture); // thumb image
song= new HashMap<String, String>();
song = data.get(position);
friendsName.setText(song.get(MyFriends.KEY_FBNAME));
//Log.i("Facebook Pangalan", ""+MyFriends.KEY_FBNAME);
FriendName = (String) friendsName.getText().toString();
FBID = song.get(MyFriends.KEY_FBID);
String fbAvatarUrl = "http://graph.facebook.com/"+ FBID +"/picture";
//Log.i("FBID", FBID);
BitmapManager.INSTANCE.loadBitmap(fbAvatarUrl, friend_image, 100,100);
Button btn=(Button)vi.findViewById(R.id.btnAdd);
if (FBID.equals(""))
{
btn.setVisibility(View.GONE);
}
else
{
btn.setVisibility(View.VISIBLE);
}
btn.setOnClickListener(new OnClickListener(){
public void onClick(View arg0) {
LOCK_BASEDATA = MyFriends.LOCK_BASEDATA;
if (LOCK_BASEDATA.equals("0"))
{
//FACEBOOK FRIENDS
Log.i("", ""+FriendName);
//AddfromFacebookFriends();
}
else if (LOCK_BASEDATA.equals("1"))
{
//MY REQUEST
//((MyFriends)context).SetMyRequest();
//Log.i("LangYa", "Langya");
}
else if (LOCK_BASEDATA.equals("2"))
{
//FRIEND REQUEST
//((MyFriends)context).SetFriendRequest();
//d2papasok
AddFriendRequest();
}
}
});
return vi;
}
I am logging my friend's name whenever I click on the button. Now my problem starts when I click the button; I am not getting my desired string. It is getting the string from another row of my listview.
EDIT
Thanks, actually ive just aaded this code
public void onClick(View arg0) {
LOCK_BASEDATA = MyFriends.LOCK_BASEDATA;
if (LOCK_BASEDATA.equals("0"))
{
//Object x =btn.getTag();
//String sa = x.toString();
View parentView = (View) arg0.getParent();
String textviewtext = ((TextView) parentView.findViewById(R.id.hiddentv)).getText().toString();
//FACEBOOK FRIENDS
AddfromFacebookFriends(textviewtext);
}
else if (LOCK_BASEDATA.equals("1"))
{
I am just getting my parents View to get my desired textview and now it works. Thanks anyway
You are trying to get the FriendName using this code:
TextView friendsName = (TextView)vi.findViewById(R.id.tvFacebookName);
FriendName = (String) friendsName.getText().toString();
This is not The FriendName of the ListView item you clicked. Its the FriendName of the newly created ListView item. Because in getView(), the ListView item you get is the newly created one.
One way to solve this is:
Use tags like KunalK suggested. I am not sure if it works...i never tried it.
Or the other way is:
onButtonClicked get the Position of the ListView item you clicked, and get the respective Name from the ArrayList you are using.
Here the Position is not the position parameter in the getView(). That gives the position of the newly created ListView item again.
Use this to get the actual position of the clicked item:
int clickedItemPosition = yourListView.getPositionForView((View) yourButton.getParent());
you should go with this by setting the "FriendName" in your button's setTag property. and whenever you handle your button click event fetch your FriendName String by using getTag property of button. for e.g. something like this:
....
btn.setTag(FriendName);
btn.setOnClickListener(new OnClickListener(){
public void onClick(View arg0) {
LOCK_BASEDATA = MyFriends.LOCK_BASEDATA;
if (LOCK_BASEDATA.equals("0"))
{
//FACEBOOK FRIENDS
Log.i("", ""+btn.getTag().toString());
//AddfromFacebookFriends();
}
else if (LOCK_BASEDATA.equals("1"))
{
//MY REQUEST
//((MyFriends)context).SetMyRequest();
//Log.i("LangYa", "Langya");
}
else if (LOCK_BASEDATA.equals("2"))
{
//FRIEND REQUEST
//((MyFriends)context).SetFriendRequest();
//d2papasok
AddFriendRequest();
}
}
});
....
Getting the name from the list can be accompished by using an onListItemClick method as follows (not in your adapter, but in the class with your list):
#Override
public void onListItemClick(ListView l, View v, int position, long id) {
TextView friendsName = (TextView)v.findViewById(R.id.tvFacebookName);
FriendName = (String) friendsName.getText().toString();
}
Since this code is the onListItemClick method, the View v parameter is the view of the row you clicked. Once you have that, you search that particular row view for the TextView you want and then pull the string from that.
You had the right idea, you just put it in the wrong place.

Categories