Android ListView with custom view - get elements - android

I create a list view with this tutorial - http://sunil-android.blogspot.com/2013/04/android-listview-checkbox-example.html
Now I have a questions:
1. How to create a button like "Check All"/"Uncheck All" ?
2. How to have access to some view element (like edittext) in a single row ?

your answer is in the tutorial as well...
Check the tutorial link again. There is a private class MyCustomAdapter extends ArrayAdapter<country>{} which is your answer.
When you bind your data to listview using adapter (in this case MyCustomAdapter is your adapter), for each element of your data array the #Override public View getView(int position, View convertView, ViewGroup parent) { } method will fire.
View convertView parameter in the #Override public View getView(int position, View convertView, ViewGroup parent) { } method is a view for each single row of your listview. If you have define xml template for your listview row with id for each element in a row, you can find element using...
TextView code = (TextView) convertView.findViewById(R.id.code);
CheckBox name = (CheckBox) convertView.findViewById(R.id.checkBox);
Here code and name are TextBox & CheckBox of the view from specific row.
So run through the example tutorial again and I think you probably would get your answer.

Related

Add EditText dynamically to ListView row

after a long click on a list view item (a list view row contains of only one TextView), a dynamically created EditText is supposed to appear in the corresponding list view row. How do I manage to do do this?
Thanks
You'll have to extend an ArrayAdapter (if you haven't done yet), and do that in the getView() method.
The second of this method's parameters is a View (usually called convertView, but not necessarily). Speaking very vaguely, this View represents actually the current row's layout, so you'll need to add the EditText into this method.
#Override
public View getView(final int position, View convertView, final ViewGroup parent) {
LinearLayout rootLinearLayout = (LinearLayout) convertView.findViewById(R.id.root_ll);
EditText et = new EditText(this);
// Set additional parameters to et
...
rootLinearLayout.addView(et);
...
return convertView;
}

Change row height in listActivity when pressed

I have created a ListActivity class, with a custom Adapter.
Rows are simple: TextView and a Button.
Implemented getView method is as below:
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = getLayoutInflater().inflate(R.layout.row, null);
}
TextView tV = (TextView) convertView.findViewById(R.id.tv);
tV.setText(MY_LIST[position]);
return convertView;
}
Now I want that each row, when pressed, dynamically add another button to itself, below the others components, and that consequently, the row height is increased.
How can I perform this steps?
New button should be hidden by default. When user clicked on row your handler makes something like mHiddenButton.setVisibility(View.VISIBLE).

How to setTag Button in ListView

I generated Button in row.xml file so it will create number of buttons as same as list item
so I'd like to setTag all of Button in ListView to getTag and retrieve the data from database.
Assuming that you are using the Custom Adapter for your ListView, you will be able to set Tag for each Button inside your Adapter's getView(),
#Override
public View getView(int pos, View convertView, ViewGroup parent) {
convertView = inflaterObject.inflate(R.layout.row,parent,false);
Button tagButton=(Button)convertView.findviewbyId(R.id.buttonID);
tagButton.setTag(pos);
//To fetch the button Tag
Log.i("Button Tab at Pos "+pos,tagButton.getTag()+"");
}

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.

GridView with customs view

I don't know if it is possible, but actually I wouldn't see why not.
Can we do a grid view not just with ImageView but with a custom view.
I am trying to make a grid view of a view composed of an ImageView and a TextView.
I know that everything happens in my Adapter getView's function but I can't figure out how to do it.
public View getView(int position, View convertView, ViewGroup parent) {
View cases = findViewById(R.id.fileUnitLayout);
if (convertView == null) {
convertView = new View(mContext);
} else {
cases = convertView;
}
return cases;
}
My view has an id of R.id.fileUnitLayout. Let's say my inner TextView has an id of A and my inner ImageView has an id of B. How can I fill them ?
Thank you,
You should not need to override getView to accomplish this, necessarily. GridView is an AdapterView so you can provide an adapter that will display what you want via setAdapter
You could, for example, use SimpleAdapter to provide an xml file that is used for each grid view item.

Categories

Resources