How do I set the same attributes for multiple AutoCompleteTextViews? - android

Given that I have the following AutoCompleteTextViews
ArrayAdapter<String> adapter121 = new ArrayAdapter<String>
(this,android.R.layout.simple_dropdown_item_1line,androidBooks);
AutoCompleteTextView acTextView = (AutoCompleteTextView)findViewById(R.id.AndroidBooks);
AutoCompleteTextView acTextView0 = (AutoCompleteTextView)findViewById(R.id.AndroidBooks1);
AutoCompleteTextView acTextView1 = (AutoCompleteTextView)findViewById(R.id.et3);
AutoCompleteTextView acTextView2 = (AutoCompleteTextView)findViewById(R.id.et4);
AutoCompleteTextView acTextView3 = (AutoCompleteTextView)findViewById(R.id.et5);
AutoCompleteTextView acTextView4 = (AutoCompleteTextView)findViewById(R.id.et6);
AutoCompleteTextView acTextView5 = (AutoCompleteTextView)findViewById(R.id.et7);
AutoCompleteTextView acTextView6 = (AutoCompleteTextView)findViewById(R.id.et8);
AutoCompleteTextView acTextView7 = (AutoCompleteTextView)findViewById(R.id.et9);
How would I call:
acTextView.setThreshold(1);
acTextView.setAdapter(adapter121);
on each of my AutoCompleteTextViews?

To set each of the AutoCompleteTextViews you can add them an array mAutoCompleteTextViews and set the attributes by cycling through each one.
ArrayAdapter<String> adapter121 = new ArrayAdapter<String>
(this,android.R.layout.simple_dropdown_item_1line,androidBooks);
AutoCompleteTextView[] mAutoCompleteTextViews = new AutoCompleteTextView[{
(AutoCompleteTextView)findViewById(R.id.AndroidBooks),
(AutoCompleteTextView)findViewById(R.id.AndroidBooks1),
(AutoCompleteTextView)findViewById(R.id.et3),
(AutoCompleteTextView)findViewById(R.id.et4),
(AutoCompleteTextView)findViewById(R.id.et5),
(AutoCompleteTextView)findViewById(R.id.et6),
(AutoCompleteTextView)findViewById(R.id.et7),
(AutoCompleteTextView)findViewById(R.id.et8),
(AutoCompleteTextView)findViewById(R.id.et9)}];
// for each loop to set parameters on each AutoCompleteTextView in mAutoCompleteTextViews
for (AutoCompleteTextView acTextView : mAutoCompleteTextViews ){
acTextView.setThreshold(1);
acTextView.setAdapter(adapter121);
}
If you want to learn about arrays in Java then check out this section from the Java wikibook

You can actually create a loop that does all of this for you
int numberOfViews = 9;
for(int i = 0; i < numberOfViews; i++){
AutoComplete acTextView;
if(i == 0)
acTextView = (AutoCompleteTextView)findViewById(R.id.AndroidBooks);
if(i == 1)
acTextView = (AutoCompleteTextView)findViewById(R.id.AndroidBooks1);
else{
int id = getResources().getIdentifier("et" + (i+1), "id", getPackageName());
acTextView = (AutoCompleteTextView)findViewById(id);
}
acTextView.setThreshold(1);
acTextView.setAdapter(adapter121);
}

Related

Listview not updating based on value on Array Adapter

I call the method below to update my listview
ListView list = (ListView) listView.findViewById(R.id.plan_list);
itemsList = sortAndAddSections(getItems_search(name));
ListAdapter adapter = new ListAdapter(getActivity(), itemsList);
list.setAdapter(adapter);
but that code is associated with a value that changes and also this is the other code
private ArrayList<plan_model> getItems_search(String param_cusname) {
Cursor data = myDb.get_search_plan(pattern_email, param_name);
int i = 0;
while (data.moveToNext()) {
String date = data.getString(3);
String remarks = data.getString(4);
items.add(new plan_model(cusname, remarks);
}
return items;
}
and this is my sorter
private ArrayList sortAndAddSections(ArrayList<plan_model> itemList) {
Collections.sort(itemList);
plan_model sectionCell;
tempList.clear();
tmpHeaderPositions.clear();
String header = "";
int addedRow = 0;
int bgColor = R.color.alt_gray;
for (int i = 0; i < itemList.size(); i++) {
String remarks = itemList.get(i).getRemarks();
String date = itemList.get(i).getDate();
if (!(header.equals(itemList.get(i).getDate()))) {
sectionCell = new plan_model(remarks, date);
sectionCell.setToSectionHeader();
tmpHeaderPositions.add(i + addedRow);
addedRow++;
tempList.add(sectionCell);
header = itemList.get(i).getDate();
bgColor = R.color.alt_gray;
}
sectionCell = itemList.get(i);
sectionCell.setBgColor(bgColor);
tempList.add(sectionCell);
if (bgColor == R.color.alt_gray) bgColor = R.color.alt_white;
else bgColor = R.color.alt_gray;
}
tmpHeaderPositions.add(tempList.size());
for (int i = 0; i < tmpHeaderPositions.size() - 1; i++) {
sectionCell = tempList.get(tmpHeaderPositions.get(i));
sectionCell.setDate(sectionCell.getDate() + " (" +
(tmpHeaderPositions.get(i + 1) - tmpHeaderPositions.get(i) - 1) + ")");
}
return tempList;
}
my question is the value name changes but my listview is not how can I update my listview? because i need to update it based on search parameter
If your itemList is being updated properly, you don't need to create another instance of the adapter, just use notifyDataSetChanged():
private void createList() {
ListView list = (ListView) listView.findViewById(R.id.plan_list);
itemsList = sortAndAddSections(getItems_search(name));
adapter = new ListAdapter(getActivity(), itemsList);
list.setAdapter(adapter);
}
private void updateList() {
sortAndAddSections(getItems_search(name)); // Update itemList without re-assign its value, otherwise the adapter will loose reference
adapter.notifyDataSetChanged()
}
In getItems_search() add this line at the beginning:
items.clear();
Every time the value of name changes you have to do the following:
itemsList.clear();
itemsList = sortAndAddSections(getItems_search(name));
list.setAdapter(new ListAdapter(getActivity(), itemsList));

RecyclerView Android in loop

How to get data from ArrayList to RecyclerView?
ModelCoba modelCoba = response.body();
for (int i = 0; i < modelCoba.getAcara_daftar().size(); i++){
judul[i] = response.body().getAcara_daftar().get(i).getJudul();
pemateri[i] = response.body().getAcara_daftar().get(i).getPemateri();
tanggal[i] = response.body().getAcara_daftar().get(i).getTgl();
}
ArrayAdapter arrayAdapter = new ArrayAdapter<String>(AcaraAdapter.this, android.R.layout.activity_list_item, );
Try custom adapter here
Try above given example and in your case adapter could be used as below
MyRecyclerViewAdapter adapter = new MyRecyclerViewAdapter(MainActivity.this, modelcoba);
mRecyclerView.setAdapter (adapter);

Extract substring from a string for spinner entry

I need to extract part of the string and display it in the spinner
I need that when the spinner display data example
For array entries like the following
"Equipo-001"
"Equipo-002"
Should show only:
"001"
"002"
Here's my code
private void rellenarSpinnerConFoliosDeMaquinasDelPunto(List<String> folios) {
maquinas = dbOn.getMaquinasDePunto(idPunto);
for (int i = 0; i < maquinas.size(); i++) {
foliosDeMaquinas.add(maquinas.get(i).getcFolioMaquina());
}
adaptadorFoliosMaquina = new ArrayAdapter<>(this, android.R.layout.simple_spinner_dropdown_item, folios);
adaptadorFoliosMaquina.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spn_folioMaquina.setAdapter(adaptadorFoliosMaquina);
spn_folioMaquina.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
ArrayList<String> numberFolios = new ArrayList<>();
for(int j =0; j < folios.size(); j++){
numberFolios.add(folios.get(j).substring(8, 10));
}
adaptadorFoliosMaquina = new ArrayAdapter<>(this, android.R.layout.simple_spinner_dropdown_item, numberFolios);
Check link for better reference on how to use substring.
You can use SPLIT function
ArrayList<String> data = new ArrayList();
foreach(String get:folios){
data.add(folios.split("-")[1]);
}
adapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_dropdown_item, data);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
If your result will be dynamic(The string length would vary in future) the below solution may work,
private void rellenarSpinnerConFoliosDeMaquinasDelPunto(List<String> folios) {
try{
maquinas = dbOn.getMaquinasDePunto(idPunto);
for (int i = 0; i < maquinas.size(); i++) {
foliosDeMaquinas.add(maquinas.get(i).getcFolioMaquina().toString().split("-")[1]);
}
} catch(ArrayIndexOutOfBoundsException e){
e.printStackTrace();
}
adaptadorFoliosMaquina = new ArrayAdapter<>(this, android.R.layout.simple_spinner_dropdown_item, folios);
adaptadorFoliosMaquina.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spn_folioMaquina.setAdapter(adaptadorFoliosMaquina);
spn_folioMaquina.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

How to add data in array on button click?

int size = mcq.size();
String arr[] = null;
int i;
{
for (i = 0; i < size; i++) {
if (op1.isPressed()) {
arr[i] = tv1.getText().toString();
// Log.e("Array",arr[i]);
} else if (op2.isPressed()) {
arr[i] = tv2.getText().toString();
//Log.e("Array",arr[i]);
} else if (op3.isPressed()) {
arr[i] = tv3.getText().toString();
// Log.e("Array",arr[i]);
} else if (op4.isPressed()) {
arr[i] = tv4.getText().toString();
//Log.e("Array",arr[i]);
}
I am trying to store the data in an array when the button is pressed,but it always shows null.And when the for loop is over I want to display my array.
here , Arrays in Java have a size so u cannot do like this. Instead of this
use list,
ArrayList<String> arr = new ArrayList<String>();
Inorder to do using String array :
String[] arr = new String[SIZEDEFNE HERE];
For ur answer :
ArrayList<String> arr = new ArrayList<String>();
int i;
{
for (i = 0; i < size; i++) {
if (op1.isPressed()) {
arr.add(tv1.getText().toString());
} else if (op2.isPressed()) {
arr.add(tv2.getText().toString());
} else if (op3.isPressed()) {
arr.add(tv3.getText().toString());
} else if (op4.isPressed()) {
arr.add(tv4.getText().toString());
}
Retrive value using
String s = arr.get(0);
This is because your string array is null. Declare it with the size as
String[] arr = new String[size];
Try Array List. use the following code in your main java
final ArrayList<String> list = new ArrayList<String>();
Button button= (Button) findViewById(R.id.buttonId);
final EditText editText = (EditText) findViewById(R.id.editTextId)
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
list.add(editText.getText().toString());
}
});
To avoid duplication in arraylist .You can use arraylist for faster then String array
ArrayList<String> list = new ArrayList<String>();
list.add("Krishna");
list.add("Krishna");
list.add("Kishan");
list.add("Krishn");
list.add("Aryan");
list.add("Harm");
System.out.println("List"+list);
HashSet hs = new HashSet();
hs.addAll(list);
list.clear();
list.addAll(hs);

Android Age Spinner Item

Is it possible to build a for loop that goes to 100 to populate an Android spinner? As opposed to manually adding to the ArrayList.
Something like the below:
List<Integer> age = new ArrayList<>();
for (int i = 1; i < 101; i++) {
age.add(i);
}
It's perfectly fine to programmatically populate your ArrayList; you just need to use an ArrayAdapter like this:
List age = new ArrayList<Integer>();
for (int i = 1; i <= 100; i++) {
age.add(Integer.toString(i));
}
ArrayAdapter<Integer> spinnerArrayAdapter = new ArrayAdapter<Integer>(
this, android.R.layout.simple_spinner_item, age);
spinnerArrayAdapter.setDropDownViewResource( android.R.layout.simple_spinner_dropdown_item );
Spinner spinner = (Spinner)findViewById(R.id.spinner);
spinner.setAdapter(spinnerArrayAdapter);
Hope this helps!
yes of coures you can use...
Like this --
List age = new ArrayList<>();
for (int i = 1; i < 101; i++) {
age.add(i);
}]
now you have to use Array\adapter-
ArrayAdapter<Integer> sa= new ArrayAdapter<Integer>(
this, android.R.layout.simple_spinner_item, age);
sa.setDropDownViewResource(
android.R.layout.simple_spinner_dropdown_item );
Spinner s= (Spinner)findViewById(R.id.spinner);
s.setAdapter(sa);

Categories

Resources