exchanging items within a gridView - java

In the following method, I am trying to exchange two items within the grid view by clicking on one. I have numbers 0-9 and by clicking on one item, other than 0, the item will place itself in position on 0 and the 0 will be set in the place of clicked item. I think I am missing the line of inserting the 0 into its place. Please see the following code:
gridView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Item clickedItem = mList.get(position);
int g = clickedItem.getValue();
String c = clickedItem.getStr();
Item spare = mList.get(8);
Toast.makeText(MainActivity.this, "val is "+g+",", Toast.LENGTH_SHORT).show();
if (clickedItem.getValue() == 0) {
Toast.makeText(MainActivity.this, "unable to find pic", Toast.LENGTH_SHORT).show();
}
if (clickedItem.getValue() != 0) {
Item temp = getItem(position);
// need clicked position to get spare;
spare = temp;
}
}
public Item getItem(int position) {
return mList.get(position);
}
});

gridView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Item clickedItem = mList.get(position);
int g = clickedItem.getValue();
String c = clickedItem.getStr();
Item spare = mList.get(8);
Toast.makeText(MainActivity.this, "val is "+g+",", Toast.LENGTH_SHORT).show();
if (clickedItem.getValue()== 0) {
Toast.makeText(MainActivity.this, "unable to find pic", Toast.LENGTH_SHORT).show();
return
}
if (clickedItem.getValue()!= 0) {
Item temp = getItem(position);
Item temp0 = getItem(0);
tempPosition = position;
mList.remove(tempPosition );
mList.remove(0);
mList.add(0,temp );
if(tempPosition > mList.size() - 1){
mList.add(temp0);
}else{
mList.add(tempPosition ,temp0);
}
mAdapter.notifDataSetChanged();
// need clicked position to get spare;
spare=temp;
}
}
public Item getItem(int position) {
return mList.get(position);
}
});

Related

Android ListView with Switch buttons - only one switch active at a time

I have a list view with two switches. I want the functionality to work were only one switch may be active at a time.
--UPDATED
My Adapter:
public class NotificationsAdapter extends ArrayAdapter<String> {
private Context context;
private String mTitle[];
private boolean onOff[];
public NotificationsAdapter(Context c, String mTitle[], boolean onOff[]) {
super(c, R.layout.adapter_notifications_layout, R.id.notificationsListTv, mTitle);
this.context = c;
this.mTitle = mTitle;
this.onOff = onOff;
}
#NonNull
#Override
public View getView(int position, #Nullable View convertView, #NonNull ViewGroup parent) {
LayoutInflater layoutInflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = layoutInflater.inflate(R.layout.adapter_notifications_layout, parent, false);
Switch notificationSwitch = view.findViewById(R.id.switchNotificationsDaily);
TextView myTitle = view.findViewById(R.id.notificationsListTv);
myTitle.setText(mTitle[position]);
notificationSwitch.setChecked(onOff[position]);
view.setClickable(true);
view.setFocusable(true);
view.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int size = onOff.length;
for (int i = 0; i < size; i++) {
if (i == position) {
break;
}
onOff[i] = false;
}
if (onOff[position]) {
notificationSwitch.setChecked(false);
onOff[position] = false;
} else {
onOff[position] = true;
notificationSwitch.setChecked(true);
}
}
});
return view;
}
My Class:
private String[] mTitle = new String[]{"Once Daily", "Twice Daily"};
private Switch notificationSwitch;
private boolean[] onOff = new boolean[] {false, false};
NotificationsAdapter notificationsAdapter = new NotificationsAdapter(getActivity(), mTitle, onOff);
listView = view.findViewById(R.id.notificationsListView);
listView.setAdapter(notificationsAdapter);
listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
listView.setItemsCanFocus(true);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
}
});
}
Currently with this code I can select both switches to be active at the same time. When I select more than one switch I would like the other to deactivate. Any assistance getting this functionality to work would be greatly appreciated.
In your activity:-
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
resetValues(position);
if (onOff[position]) {
onOff[position] = false;
} else {
onOff[position] = true;
}
notifyDataSetChanged();
}
}
private void resetValues(int selectedPosition){
int size = onOff.length;
for(int i=0; i < size; i++){
if(i == selectedPosition){
continue;
}
onOff[i] = false;
}
}
And in your adapter:-
notificationSwitch.setChecked(onOff[position]);
Also, remove your click listener from adapter.
The logic is:- Making all other values as false except the selected item, then changing the state of the selected item based on its previous state.

Only clicked item visible and hide the pervious items in RecyclerView

I have ImageView inside of an item in a RecyclerView. Every time I click on an item, I want the image to become visible and the previous clicked item's image becomes invisible.
public static class MyHolder extends RecyclerView.ViewHolder {
private ImageView image_view;
private TextView text_view_name;
private TextView text_view_abilities;
private ImageView heart_image_view;
public MyHolder(#NonNull final View itemView, final OnItemClickListener listener) {
super(itemView);
image_view = itemView.findViewById(R.id.image_view);
text_view_name = itemView.findViewById(R.id.text_view_name);
text_view_abilities = itemView.findViewById(R.id.text_view_abilities);
heart_image_view = itemView.findViewById(R.id.heart_image_view);
heart_image_view.setImageResource(R.drawable.heart);
heart_image_view.setVisibility(View.GONE);
itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (listener != null) {
int position = getAdapterPosition();
if (position != RecyclerView.NO_POSITION) {
listener.onItemClick(position);
heart_image_view.setVisibility(View.VISIBLE);
}
}
});
}
}
#NonNull
#Override
public MyHolder onCreateViewHolder(#NonNull ViewGroup viewGroup, int i) {
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.single_item_layout, viewGroup, false);
MyHolder holder = new MyHolder(v, mlistener);
return holder;
}
#Override
public void onBindViewHolder(#NonNull MyHolder myHolder, int position) {
Item item = myList.get(position);
myHolder.text_view_name.setText(item.getTitle());
stringArray = item.getAbilitiesObj();
StringBuilder builder = new StringBuilder();
for (int i = 0; i < stringArray.size(); i++) {
try {
builder.append(stringArray.get(i));
builder.append(", ");
} catch (NumberFormatException ex) {
ex.printStackTrace();
}
myHolder.text_view_abilities.setText(builder);
}
if (myHolder.image_view != null) {
String photoUrl = item.getImageUrl();
Picasso.with(mContext).load(photoUrl).into(myHolder.image_view);
}
}
In MyHolder class where item.setOnClick, I set the clicked item image visible but then every time I click on another item, the image of the new item becomes visible and the image of the previous item does not get invisible.
You need to have another array of integers in your adapter to keep track of the items which is clicked. At first, initialize the array of integers with all ones.
// Define an array like the following in your adapter
private int[] selectedItems = new int[yourDataList.size()]; // Initialize the array to have the same size as your data list.
Then initialize the array with all ones. Try having a function like this in your adapter.
private void initializeSeledtedItems() {
for(int item : selectedItems) item = 1;
}
Now in your onBindViewHolder, set the visibility of the ImageView in your RecyclerView items based on the value found in the selectedItems array.
if(selectedItems[position] == 1) heart_image_view.setVisibility(View.VISIBLE);
else heart_image_view.setVisibility(View.GONE);
In the onClickListener modify the selectedItems array as such that, only the item selected has the value one and all the others have zeros. Then call notifyDataSetChanged to take your changes into effect.
private void setSelectedItem(int position) {
for(int i = 0; i < selectedItems.length(); i++) {
if(i == position) selectedItems[i] = 1;
else selectedItems[i] = 0;
}
}
itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (listener != null) {
int position = getAdapterPosition();
if (position != RecyclerView.NO_POSITION) {
setSelectedItem(position);
notifyDataSetChanged();
}
}
}
});
Hope that helps!
My code is for setting Visibility on recycler view's item , when you click on recycler views item it set VISIBLE on that particular item and INVISIBLE on other items.
this code works for me for setting visibility on clicked item and hiding previous icon.
initialize
int selecteditem =0;
onBindViewHolder
if(selecteditem==position){
holder.icSelect.setVisibility(View.VISIBLE);
}
else{
holder.icSelect.setVisibility(View.INVISIBLE);
}
holder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
selecteditem=position;
holder.icselect.setVisibility(View.VISIBLE);
notifyDataSetChanged();
}
});
i uploaded image for basic idea .
enter image description here

Count of total suggestions as per input data on autocompletetextview - android

How can I get total number of suggestions that is exactly matching the input data on autocompletetextview
placeView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Places item = (Places) parent.getItemAtPosition(position);
if(!place_id.equals("")) {
place_name = item.getPlaceName();
place_id = item.getOnlineId();
place_panchayat_id = item.getPanchayat_name();
} else {
placeView.setText("");
}
}
});
Try this,
placeView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//getCount() Method give the count from the adapter;
int count = parent.getCount();
Places item = (Places) parent.getItemAtPosition(position);
if(!place_id.equals("")) {
place_name = item.getPlaceName();
place_id = item.getOnlineId();
place_panchayat_id = item.getPanchayat_name();
} else {
placeView.setText("");
}
}
});

How to find which ListView item is selected to add it in an ArrayList

First i am sorry for my bad English. :)
I must find out which item has been clicked on to add it in an ArrayList. If items are clicked successively, I want to add them successively in the ArrayList.
final ArrayList oldPostion = new ArrayList<Integer>();
ArrayAdapter adapterONE = new ArrayAdapter(this,android.R.layout.simple_list_item_activated_1, list);
ListView one = (ListView) findViewById(R.id.listViewOne);
one.setAdapter(adapterONE);
one.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
oldPostion.add(position);
int something = 0;
if(oldPostion != null && oldPostion.get(position - 1) != null){
if( oldPostion.get(position - 1) == position){
//do something like this
Toast.makeText(getApplicationContext(), "Item First Item" + "Selected Item" + one.getAdapter().getItem(position), Toast.LENGTH_LONG).show();
something = 1 + 1;
}else if(oldPostion.get(position) == position){
//do something like this
Toast.makeText(getApplicationContext(), "Item Second Item" + "Selected Item" + one.getAdapter().getItem(position) , Toast.LENGTH_LONG).show();
something = 1 - 1;
}
//Result
Toast.makeText(getApplicationContext(), Integer.toString(something), Toast.LENGTH_LONG).show();
}
}
}
To get the text, inside public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
add
TextView tv = (TextView) view.findViewById(R.id.the_text_view_id);
String text = tv.getText().toString();

How to get integer values from a list view in android?

I have created a list view and it contains integer values. I have set them inside a textview and now from another layout I need to get those integer values and pass it to another activity! How can I do that? Please help I'm new to android .
private void viewFunction() {
Button messageButton=(Button)findViewById(R.id.btnCreate);
messageButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
;
Units units=new Units(dbHandler.getUnitsCount(),String.valueOf(myTry.getText()),String.valueOf(bulbSpn.getSelectedItem()),String.valueOf(fanSpn.getSelectedItem()));
//addUnits(0,myTry.getText().toString(),bulbSpn.getSelectedItem().toString(),fanS pn.getSelectedItem().toString());
if(!unitExsits(units)) {
dbHandler.createUnit(units);
Unit.add(units);
unitsAdapter.notifyDataSetChanged();
// populateList();
// startActivity(new Intent(getApplicationContext(),HomePage.class));
Intent intent_valueBulb = new Intent(CreateNewUnit.this,
HomePage.class);
intent_valueBulb.putExtra("key2",noOfBulb);
intent_valueBulb.putExtra("keyFan",noOfFan);
startActivity(intent_valueBulb);
Toast.makeText(getApplicationContext(),String.valueOf(myTry.getText())+"has been added to units",Toast.LENGTH_SHORT).show();
}
else {
Toast.makeText(getApplicationContext(), String.valueOf(myTry.getText()) + " already exists !!! Please use a different name!", Toast.LENGTH_SHORT).show();
}
}
});
}
#Override
public View getView(int position, View view, ViewGroup parent) {
if (view == null) {
view = getLayoutInflater().inflate(R.layout.listview_item,parent ,false);
}
Units currentUnit = Unit.get(position);
TextView name = (TextView)view.findViewById(R.id.lblLUnitName);
name.setText(currentUnit.getName());
TextView bulb=(TextView) view.findViewById(R.id.lblLNoofBulbs);
bulb.setText("Number of Bulbs :"+currentUnit.getBulbNo());
TextView fan=(TextView) view.findViewById(R.id.lblLNoOfFans);
fan.setText("Number of Fans :"+currentUnit.getFanNo());
return view;
}
If you want to store the contents of the list in a variable, you can call getCount to determine the size of the list, and then do a for loop to add each item to an ArrayList.
ArrayList<Integer> numbers = new ArrayList<Integer>();
int size = myList.getAdapter().getCount();
for(int i = 0; i < size; i++)
{
numbers.add((Integer) myList.getAdapter().getItem(i));
}
If you're trying to store the list item selected by the user, you need to implement an onItemClickListener, and then grab the selected item from the adapter using its index in the list:
myList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
//this is what happens when an item in the list is clicked
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
//the location of the selected item is stored in i
//use this location to pull the value and store it in a variable
int selectedNumber = (Integer) adapterView.getAdapter().getItem(i);
//you now have the chosen number stored in a variable
}
});

Categories

Resources