ExpandableListView with an EditText - android

I have an ExpandableListView where each parentView has an EditText (and a textview) in it. When i added the EditText to the Layout it would no longer let me click or expand the ExpandableListView, all focus just goes to the EditText. Is there a way to make both the EditText and the ExpandableListView itself be clickable? I want to be able to click the expandableListView to expand the list, and the EditText to be able to enter text.
Thanks
EDIT: added code of my group view
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) myContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.group_row, null);
}
TextView playerName = (TextView) convertView.findViewById(R.id.playerName);
playerName.setText(groupElements[groupPosition]);
final EditText holeScore = (EditText) convertView.findViewById(R.id.score);
holeScore.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(scoreCard.this, "On CLick", Toast.LENGTH_SHORT).show();
holeScore.setFocusable(true);
}
});
return convertView;
}

The problem is because of EditText's default focusablitiy. Since they are capable of getting focused by default this will make your parent view to be unfocused. So what you have to do is remove the focus capability of your EditText by using,
andorid:focusable="false".
And similarly if you are having any button in your parent, setting focusable false to it also will help. This is the only way you can make your parent view to get clicked.

Related

ExpandableListAdapter with buttons changing an TextView

I have an expandableList Adapter working pretty well, but something in baffling me. On each child lists I have 2 buttons and a textview. I would like to make it some that when I click one button (+) that the textview number goes up and when I click the other button (-) that the textview number goes down.
public View getChildView(final int groupPosition, final int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
final String childText = (String) getChild(groupPosition, childPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.list_item, null);
}
final TextView txtListChild = (TextView) convertView
.findViewById(R.id.lblListItem);
Button btnMinus = (Button) convertView
.findViewById(R.id.btnMinus);
btnMinus.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
LinearLayout ll = (LinearLayout) v.getParent();
TextView tv1 = (TextView) ll.getChildAt(1);
String Count = tv1.getText().toString();
int num = Integer.parseInt(Count);
num = num - 1;
tv1.setText(Integer.toString(num));
}
So the above code works as I would expect it, the button is clicked the textview goes down by one and we all is good.
But here is where I am getting confused. If I open another part of the list and look at another list of children one of the TextViews in that list has also changed! So in short clicking a button in one child is not only affecting the text view right next to it, but is also affecting a text view in a completely different header.
I think you are unaware of the view recycling mechanism. You can read about it here:
How ListView's recycling mechanism works
Basically, when you scroll a ListView (or ExpandableListView), the view that just got off the screen on the top will be reused to create a view that needs to be shown on the bottom.

Is possible to put a button on a row of a listview?

I'm trying to put a button to delete a row of a listview. I've modified the layout of the single rows like this:
With lst.setOnItemClickListener... I manage the click on the row, but I don't know how to click the button inside the list.
It can be done?
Search on google "custom adapter"
Then the 5 first links :
Custom Adapter for List View
http://www.javacodegeeks.com/2013/06/android-listview-custom-adapter-with-imageview.html
http://www.learn-android-easily.com/2013/06/listview-with-custom-adapter.html
http://www.vogella.com/tutorials/AndroidListView/article.html
http://developer.xamarin.com/recipes/android/data/adapters/create_a_custom_adapter_for_contacts/
Override your adapter getview method to handle the button click.
public View getView(final int position, View convertView,
ViewGroup parent) {
LayoutInflater inflater = getLayoutInflater();
View row = inflater.inflate(R.layout.vehicals_details_row, parent,
false);
Button deleteImageView = (Button) row.findViewById(R.id.DeleteImageView);
deleteImageView.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
});
}
}
In your listItem xml layout, set the button to have the following attribute and it will cause the list item to be clickable as well:
android:focusable="false"

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).

scrolling listview causes buttons to be invisible

I have a list view which contains a button in each row of the list.
Based upon a field, I want to make this button invisible.
My getView method inside adapter is shown below.
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
Activity activity = (Activity) getContext();
View view = convertView;
if (convertView == null) {
LayoutInflater inflater = activity.getLayoutInflater();
view = inflater.inflate(R.layout.listrow, null);
}
final Details details = getItem(position);
Button btn = (Button) view.findViewById(R.id.btn);
if(details.check()) {
btn.setVisibility(View.INVISIBLE);
}
}
When I load this page the data comes correctly. But when simply scroll through this list, then this button is getting invisible. Whats the reason for this? When I remove that if section, then i will get buttons for all rows, even if i scrolls. Is there any problem giving invisible inside getView(). Please reply. Thanks in advance.
Add the following:
if(details.check()) {
btn.setVisibility(View.INVISIBLE);
}
else {
btn.setVisibility(View.VISIBLE);
}
and...it had better use
LayoutInflater.from(getContext())
instead of activity.getLayoutInflater()
set the clickable property of listview false in xml or like:
getListView().setClickable(false);

custom views not updating with OnGroupClickListener on ExpandableListView

I have custom groupViews that need to change state when they are expanded and collapsed.
If the same group view is expanded it toggles between two states.
The problem i'm having is that the expand method seems to be pulling up some cached version of the views because my updates aren't visible after calling expandGroup.
If my listener returns true (handles the whole event itself) without calling expandGroup the update does happen. So something is happening with expandGroup that only allows cached view to be drawn.
I have tried invalidating() just about everything. I've tried firing off data update events on the List View. i've tried all this other stuff as well:
expandableList.setGroupIndicator(null);
expandableList.setAlwaysDrawnWithCacheEnabled(false);
expandableList.setWillNotCacheDrawing(true);
expandableList.setItemsCanFocus(false);
no luck on any of those :(
Here's my onClick code:
expandableList.setOnGroupClickListener(new OnGroupClickListener() {
public boolean onGroupClick(ExpandableListView parent, View v,
int groupPosition, long id) {
MusicTrackRow mt = (MusicTrackRow) v;
if (mt.isPlaying == true) {
mt.setPaused();
} else {
mt.setPlaying();
}
mt.invalidate();
parent.invalidate();
trackAdapter.notifyDataSetInvalidated();
//need to call expandGroup if the listener returns true.. if returning false expandGroup is //returned automatically
expandableList.expandGroup(groupPosition); //no view refresh
return true;
Found a solution finally!
When the expandable list is expanded a call to getGroupview in the adapter is made for every group on the list.
That is the place where you want to effect changes.
the isExpanded argument lets you determine which group view is expanded.
Then you can do stuff that looks like this:
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
View v;
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) getBaseContext()
.getSystemService(LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.expandablelistitem, null);
} else {
v = convertView;
}
int id = (!isExpanded) ? R.drawable.list_plus_selector
: R.drawable.list_minus_selector;
TextView textView = (TextView) v.findViewById(R.id.list_item_text);
textView.setText(getGroup(groupPosition).toString());
ImageView icon = (ImageView) v.findViewById(R.id.list_item_icon);
icon.setImageResource(id);
return v;
}

Categories

Resources