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;
}
Related
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?
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.
I have listview with hundred of items. Every item had a couple of LinearLayouts but ONE of them is Visibility.GONE! Every item has textviews and an image. On Image Click i want to set the LinearLayout with visibility.Gone to View.VISIBLE. It works fine until you scroll down the listview, then every 4th item has the same layout set to VISIBLE but i only need the Clicked one! Here is the getView method:
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
final ImageView imgForClick;
View vi = convertView;
if (convertView == null)
vi = inflater.inflate(R.layout.custom_row, null);
final LinearLayout hiddenLayout = (LinearLayout)vi.findViewById(R.id.hiddenLayout);
imgForClick = (ImageView)vi.findViewById(R.id.imageView3);
imgForClick.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
hiddenLayout.setVisibility(View.VISIBLE);
}
});
return vi;
}
That's because you are recycling the views, so the layout gets changed on a click and then that layout is used in your other rows to save memory.
You need to remember the state of each of the rows to know whether or not the layout should be visible or not
Have something like this:
public View getView(final int position, View convertView, ViewGroup parent) {
...
if (shouldBeVisible.get(position)) {
hiddenLayout.setVisibility(View.VISIBLE);
} else {
hiddenLayout.setVisibility(View.GONE);
}
That way the layout will always be set one way or another.
shouldBeVisible is a List of something that lets you know which rows should have that layout visible or not.
EDIT--
An alternative is to remove view recycling, however this will dramatically hurt performance and should NOT be done, but I'm just explaining to list all your options.
You would remove the line
if (convertView == null)
Making Android always inflate a new view, instead of using the recycled one when possible.
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).
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()+"");
}