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("");
}
}
});
Related
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.
My target is to show part of selected spinner item string, as selected item. In Spinner options I want show full string, but when selected only first part of it. Example:
Selected: "Racing Car" > show: "Racing",
Selected: "Fast car" > show: "Fast"
Does that possible? My code:
private void defineRacingCarsArray(){
racingCarsArray = Arrays.asList(
"Racing Car",
"Fast Car"
);
}
private void defineRacingCarsCodeSpinner(){
racingCarsCodeSpinner = (Spinner) view.findViewById(R.id.cars_code_spinner);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getContext(), android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
for (int i = 0; i < racingCarsArray.size(); ++i) {
adapter.add(racingCarsArray.get(i));
}
racingCarsCodeSpinner.setAdapter(adapter);
racingCarsCodeSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
parent.getItemAtPosition(position);
String car = racingCarsArray.get(position);
;
}
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
Try this,
racingCarsCodeSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
// here you have to update
TextView selectedView = (TextView) view;
selectedView.setText(racingCarsArray.get(position).split(" ",2)[0]);
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
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);
}
});
I am working on an Android project in which I have a list of items shown to the user along-with its images. The problem right now is because of the modifications I had to do to set the list, I don't know how I can change the code to include the id of the object which is being clicked by the user. I would not like the position, but the id which is received from the server. Any help would be nice. Thanks a lot. :-)
Please note, relevant code is more in the first class below, I have just put the LazyAdapter if there are modifications requried in it. Thanks a lot.
GroupCanvasActivity.java :
public class GroupCanvasActivity extends Activity {
private static volatile Long groupAccountid = (long) 0;
List<RestCanvas> restCanvasList = new ArrayList<>();
private CanvasServiceImpl canvasService = new CanvasServiceImpl();
LazyAdapter lazyAdapter;
ListView listView;
static final String mcanvasid = "canvasid";
static final String mcanvastitle = "canvastitle";
static final String mcanvasname = "mcanvasname";
static final String mcanvasimage = "mcanvasimage";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.canvaslayout);
Bundle extras = getIntent().getExtras();
if (extras != null) {
groupAccountid = extras.getLong("groupid");
}
restCanvasList = this.canvasService.getGroupCanvasForGroupAccount(groupAccountid);
ArrayList<HashMap<String, String>> canvaslist = new ArrayList<HashMap<String, String>>();
for (RestCanvas restCanvas : restCanvasList) {
HashMap<String, String> canvasDisplay = new HashMap<>();
// The below mcanvasid is what I would like to get when clicked
canvasDisplay.put("mcanvasid", String.valueOf(restCanvas.getMcanvasid()));
canvasDisplay.put("mcanvastitle", restCanvas.getMcanvastitle());
canvasDisplay.put("mcanvasname", restCanvas.getMcanvasname());
canvasDisplay.put("mcanvasimage", restCanvas.getMcanvasimage());
canvaslist.add(canvasDisplay);
}
listView = (ListView) findViewById(R.id.canlist);
lazyAdapter = new LazyAdapter(this, canvaslist);
listView.setAdapter(lazyAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// In the for loop above, you can see the mcanvasid, which I would like to use here.
}
});
}
}
LazyAdapter.java :
public class LazyAdapter extends BaseAdapter {
private Activity activity;
private ArrayList<HashMap<String, String>> data;
private static LayoutInflater inflater=null;
public LazyAdapter(Activity a, ArrayList<HashMap<String, String>> d) {
activity = a;
data=d;
inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public int getCount() {
return data.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
View vi=convertView;
if(convertView==null)
vi = inflater.inflate(R.layout.activity_group_canvas, null);
TextView canvasName = (TextView)vi.findViewById(R.id.canvasname); // id
TextView canvasTitle = (TextView)vi.findViewById(R.id.canvastitle); // title
ImageView canvasImage=(ImageView)vi.findViewById(R.id.canvasimage); // image
HashMap<String, String> canvasList = new HashMap<String, String>();
canvasList = data.get(position);
canvasName.setText(canvasList.get(GroupCanvasActivity.mcanvasid));
canvasTitle.setText(canvasList.get(GroupCanvasActivity.mcanvasname));
canvasImage.setImageBitmap(convertByteArrayToBitmap(canvasList.get(GroupCanvasActivity.mcanvasimage)));
return vi;
}
private Bitmap convertByteArrayToBitmap(String string){
byte [] encodeByte= Base64.decode(string, Base64.DEFAULT);
return BitmapFactory.decodeByteArray(encodeByte, 0, encodeByte.length);
}
}
Any help would be nice. Thank you. :-)
Do like this :
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
String mcanvasid = restCanvasList.get(position).get("mcanvasid"); // Here you get the mcanvasid
}
});
try this. In your adapter:
public Object getItem(int position) {
return d.get(position);
}
, in your activity:
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
String output = (HashMap<String, String>)(parent.getAdapter.getItem(position)).get("mcanvasid");
//do what you want with output
}
});
I am trying to implement something similar to facebook's search system, where if a user starts typing in a name it brings autocomplete suggestions based on the letters typed, and with an additional option to search for more results. Each result is an object and not a string, and I have tried adding an extra result for search but every time I click on search or one of the objects a replace text occurs with the object as oppose to the name and I know it is a method of the autocomplete widget. Is there another way to go about it?
Here is my code:
private AutoCompleteTextView sx;
sx = (AutoCompleteTextView) findViewById(R.id.sx);
if(sadapter == null) {
sadapter = new Sadapter(PostActivity.this, usersFound);
sx.setAdapter(sadapter);
}
sx.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (sx.getText().toString().length() <= 3 && sadapter != null) {
usersFound.clear();
sadapter.notifyDataSetChanged();
}
if (sx.getText().toString().length() > 3) {
usersFound.clear();
sadapter.notifyDataSetChanged();
Log.d(Constants.DEBUG, "Changing text " + s);
sxname = s.toString();
testCreate();
sadapter.notifyDataSetChanged();
}
}
#Override
public void afterTextChanged(Editable s) {
}
});
sx.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
DatabaseUser newAdd = usersFound.get(position);
if(position == searchServerIndex) {
sx.setText(sxname);
usersFound.clear();
sadapter.notifyDataSetChanged();
apiGetPossibleCandidates();
} else {
sx.setText("");
}
}
});
private void testCreate() {
DatabaseUser nuser1 = new DatabaseUser("userid", "pictureid", "Jon");
DatabaseUser nuser2 = new DatabaseUser("userid", "pictureid", "Jonny");
DatabaseUser nuser3 = new DatabaseUser("userid", "pictureid", "Jong");
DatabaseUser nuser4 = new DatabaseUser("userid", "pictureid", "Joan");
DatabaseUser searchServer = new DatabaseUser("SearchId", "pictureid", "Search " + sxname);
usersFound.add(nuser1);
usersFound.add(nuser2);
usersFound.add(nuser3);
usersFound.add(nuser4);
searchServerIndex = usersFound.size();
usersFound.add(searchServer);
if(sadapter != null) {
sadapter.notifyDataSetChanged();
}
}
This is the adapter:
public class Sadapter extends ArrayAdapter<DatabaseUser> {
private Context mContext;
private List<DatabaseUser> usersSearch;
private List<DatabaseUser> usersFiltered;
public Sadapter(Context context, List<DatabaseUser> usersAdded) {
super(context, 0, usersAdded);
mContext = context;
usersSearch = usersAdded;
}
#Override
public int getCount() {
return usersSearch.size();
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater inflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.user_autosearch_item, null);
}
//helps for recycling
final ViewHolder holder = new ViewHolder();
holder.userTxt = (TextView) v.findViewById(R.id.userTxt);
v.setTag(holder);
String name = usersSearch.get(position).getName();
holder.userTxt.setText(name);
return v;
}
static class ViewHolder {
TextView userTxt;
}
}
you can override getItem() method in your adapater and return the object of DataBaseUser of particular position from the searchlist.. like
#Override public DatabaseUser getItem(int position) {
return usersSearch.get(position);
}
So from your onClick method you can call this method and it will give you DatabaseUser object from which you can retrive your text. I hope it helps you ..