Iterate through ListView navigation items - android

I'm using the Activity with Navigation Drawer project template within Android Studio, which generates the navigation menu like this:
mDrawerListView.setAdapter(new ArrayAdapter<String>(
getActionBar().getThemedContext(),
android.R.layout.simple_list_item_1,
android.R.id.text1,
new String[]{
getString(R.string.title_section1),
getString(R.string.title_section2),
getString(R.string.title_section3),
}));
I'm looking for an easy way to be able to iterate through those items and update the subtext on some of them with new information. So I attempted to change android.R.layout.simple_list_item_1 to android.R.layout.simple_list_item_2 within the setAdapter() call, and loop through them like this:
View v;
TextView tv;
for(int i = 0; i < mDrawerListView.getCount(); i++){
v = mDrawerListView.getAdapter().getView(i,null,null);
tv = (TextView) v.findViewById(android.R.id.text2);
tv.setText("Sub Item");
}
Which doesn't work, but it doesn't throw any errors. I've also tried just using getChildAt() rather than going through getAdapter(), which has resulted in a null reference error.
What's the best way to approach this particular issue?

I had the same issue, but the solution is pretty simple. You just need to override the getView method of the adapter:
public View getView(int position, View convertView, ViewGroup parent)
You can keep the normal behaviour of this method and add whatever you want to do, so for example in your case you'd want to add something like this:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view= super.getView(position, convertView, parent);
TextView tv= (TextView) view.findViewById(android.R.id.text2);
tv.setText("sub text");
}
This should work.

Related

How to change AutoCompleteTextView's EditText's view?

Not its dropdown view, but the TextView view where our choice will appear, that looks like an EditText.
I'm using custom adapter, and I've tried to set it in adapter constructor, and then throw it back to parent using super, but the EditText view is still using the style in xml.
The only view-related I override was getView:
public View getView(int position, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);
TextView tv = (TextView) view;
//I do something with TextView here, but won't affect the problem
return view;
}
Then I use the adapter like this
adapterAutoComplete = new CustomAdapter(
this, R.layout.custom_layout_item, objects);
adapterAutoComplete.setDropDownViewResource(
R.layout.custom_dropdown_layout);
autoComplete.setAdapter(adapterAutoComplete);
Resulted in just how I've said, its EditText view used a style I used in xml and totally abandoned R.layout.custom_layout_item.
What did I do wrong?

Using same custom adapter for AutoCompleteTextView and Spinner

So I use one adapter for my Spinner and AutoCompleteTextView, which extends ArrayAdapter<Object>. For View-related method, I only override getView.
public View getView(int position, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);
TextView tv = (TextView) view;
//I do something with TextView here, but won't affect the problem
return view;
}
Now, both declared like this:
adapterSpinner = new CustomAdapter(
this, R.layout.custom_layout_item, objects);
adapterService.setDropDownViewResource(
R.layout.custom_dropdown_layout);
spinner.setAdapter(adapterSpinner);
adapterAutoComplete = new CustomAdapter(
this, R.layout.custom_layout_item, objects);
adapterAutoComplete.setDropDownViewResource(
R.layout.custom_dropdown_layout);
autoComplete.setAdapter(adapterAutoComplete);
The result confusing me:
AutoCompleteTextView
Applies custom_layout_item to its dropdown View instead, and instead applies a style I gave in its element in xml to its TextView/EditText layout. It ignores custom_dropdown_layout I've set.
Spinner
Applies correctly all of them, its TextView view using custom_layout_item, and its dropdown view using custom_dropdown_layout. Though it ignores the style I gave to it in xml.
Functional, both works fine. But from UI, quite a mess... Why did it happen?

Android - Making a listview row invisible

I am attempting to make a list view row empty if it has no text. I have achieved this before, but I'm not sure how I did it. At the moment, I have managed to get it to not show anything when the text is equal to nothing, however it still shows the row border.
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_2, android.R.id.text1, MainActivity.values) {
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);
TextView text1 = (TextView) view.findViewById(android.R.id.text1);
TextView text2 = (TextView) view.findViewById(android.R.id.text2);
text1.setText(MainActivity.values[position]);
text2.setText(MainActivity.numvalues[position]);
if (MainActivity.values[position].equals("")) {
text1.setVisibility(View.INVISIBLE);
text1.setVisibility(View.GONE);
text2.setVisibility(View.INVISIBLE);
text2.setVisibility(View.GONE);
//convertView.setVisibility(View.INVISIBLE);
//convertView.setVisibility(View.GONE);
view.setVisibility(View.INVISIBLE);
view.setVisibility(View.GONE);
}
else {
text1.setVisibility(View.VISIBLE);
text2.setVisibility(View.VISIBLE);
//convertView.setVisibility(View.INVISIBLE);
//convertView.setVisibility(View.GONE);
view.setVisibility(View.VISIBLE);
}
return view;
}
};
Before init ArrayAdapter remove from MainActivity.values and MainActivity.numvalues items that don't need to show.
VIEW.GONE won't work as you want, i've tried it, but there is one thing you can do to avoid this problem at all, maybe you should remove all of your empty rows before you past your items into adapter?

How to add Image in Spinner?

I made this app called UnitConverter...
unitarray=new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item);
unitarray.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
SpinnerUnit.setAdapter(unitarray);
unitarray.add(getResources().getString(R.string.unit1));
unitarray.add(getResources().getString(R.string.unit2));
unitarray.add(getResources().getString(R.string.unit3));
unitarray.add(getResources().getString(R.string.unit4));
unitarray.add(getResources().getString(R.string.unit5));
unitarray.add(getResources().getString(R.string.unit6));
unitarray.add(getResources().getString(R.string.unit7));
unitarray.add(getResources().getString(R.string.unit8));
unitarray.setNotifyOnChange(true);
everything's awesome but I want to put images next to each Unit!
is there anyone who can help me with this please? thanks!
Probably the simplest way would be to use android's R.layout.simple_spinner_item as the spinner elem layout and internally to add an image to the TextView using setCompoundDrawablesWithIntrinsicBounds
ArrayAdapter adapter = new ArrayAdapter(context, android.R.layout.simple_spinner_item, countries);
countrySpinner.setAdapter(adapter);
And the adapter:
class ImageSpinnerAdapter extends ArrayAdapter {
public View getView(int position, View convertView, ViewGroup parent) {
TextView textView = (TextView) super.getView(position, convertView, parent);
Elem elem = elemArray.get(position);
textView.setText(elem.toString());
textView.setCompoundDrawablesWithIntrinsicBounds(elem.getImage(), null, null, null);
return textView;
}
}
See detailed example here.

How to get the spinner id or tag from getDropDownView method in android

I have several spinners that I have created a custom ArrayAdapter for so I can change the drop down menu look. I want to manipulate the view depending on what spinner the dropdown belongs to. I thought I would be able to do something like parent.getTag() but it is returning null.
The custom array adapter looks like:
class BackgroundColorAdapter extends ArrayAdapter<String> {
BackgroundColorAdapter() {
super(SettingsActivity.this, R.layout.settings_spinner_item, R.id.item_text, textColors);
}
public View getDropDownView (int position, View convertView, ViewGroup parent){
View row=super.getView(position, convertView, parent);
if(parent.getTag().equals("background"){
//Do custom stuff here
}
return(row);
}
}
and I'm setting the tag:
settingsSpinner.setTag("bg_color_spinner");
settingsSpinner.setAdapter(new BackgroundColorAdapter());
I think I'm confused how the view hierarchy works but it seems logical that the parent of the spinner drop down would be the spinner. Anyone know how I can find out what spinner the drop down belongs to in getDropDownView?
edit: made the settingsSpinner a single spinner instead of an array of spinners to make it less confusing
Eventually got this to work, here is the code for example that changes the text font for each item in the drop down.
class TextSizeAdapter extends ArrayAdapter<String> {
TextSizeAdapter() {
super(SettingsActivity.this, R.layout.settings_spinner_item, R.id.item_text, textSizes);
}
public View getDropDownView (int position, View convertView, ViewGroup parent){
View row=super.getView(position, convertView, parent);
TextView text = (TextView)row.findViewById(R.id.item_text);
text.setTextSize(TypedValue.COMPLEX_UNIT_PX,appState.FONTSIZES[position]);
RadioButton radio = (RadioButton)row.findViewById(R.id.item_radio);
if(settingsSpinners[2].getSelectedItemPosition() == position){
radio.setChecked(true);
}else{
radio.setChecked(false);
}
return(row);
}
}
I'm unfamiliar with getDropDownView(), and don't know why you use it. Documentation for getDropDownView() states the following about the parent:
parent the parent that this view will eventually be attached to
This doesn't sound like the 'parent' you are looking for...
Since the 'parent' in the getView() call is indeed a Spinner, you could use that to store an instance variable of the parent like below:
public Spinner mParent = null;
public View getView (int position, View convertView, ViewGroup parent)
{
this.mParent = parent;
return super.getView(position, convertView, parent);
}
public View getDropDownView(int position, View convertView, ViewGroup parent)
{ // Your code here -> but use 'mParent'
}
I haven't tried, but maybe it's a workaround to get what you need. Please let me know if you found the solution.

Categories

Resources