How do I change a list view item dynamically? - android

I basically want something similar to the Twitter or Pocket app where you long click on an item in the list View and it gives you a set of options to perform on that item by changing the layout of that item, to reveal a set of buttons.
ListView lv= getListView();
lv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> av, View v,
int pos, long id) {
// TODO Auto-generated method stub
Context ctx = getApplicationContext();
mDbHelper.deleteNotes(id);
fillData();
Toast.makeText(ctx, "Long Clicked at" + id, Toast.LENGTH_SHORT).show();
return false;
}
});
My long click code is here. Right know I just directly delete the item that is long clicked, but I want the user to see 3-4 buttons when an item is long clicked in place of the said item.
Anyone knows how to do it?
EDIT
Here is the twitter image:

What you could do, is when the user long presses, simply within your adapter, hide or show a, for example, LinearLayout containing all the buttons and so on within it.
Make sure this element you are showing fits itself below or above all other usual elements.
Do you get what I'm trying to say? Holla if you need more info.
Update 1
Okay so in reply to your comment, no you won't need to create a separate xml file etc. See this for example,
Your custom row xml with two layouts:
RelativeLayout mainView
RelativeLayout extraView
In your row xml have an extra layout (my preference is relative, it's upto you anyway), and set this layout to be below the main stuff (make sure they both fit on the row). So in this scenario, mainView is the layout with the stuff you always want shown and extraView is the stuff you want only when long pressed.
Make sure the default visibility setting for extraView is set as GONE
Then, simply when long pressed, that specific rows extraView.setVisibility(View.VISIBLE);
And obviously vice versa when no longer long pressed.
Hit me up if you're confused, and apologies for the non formatted reply as I'm currently commuting and replying via my mobile, will format the answer when at a pc.

Related

TextView doesn't pass click to parent

I have an ExpandableListView with a group header that shall expand and collapse when clicked. This works for clicks on an ImageView that is inside the list item and it works on directly clicking the item and so on but it doesn't work for a TextView in the list item.
I have to give the TextView a OnClickListener and catch the click separately. This is not nice especially because I don't have all data that I normally get in the OnGroupClickListener.
I read about making the TextView clickable=true and focusable=false but this didn't help. What else will I have to set or change to make the click go "through" the TextView to the base view?
Edit
see my answer for the reason.
Anyway I show how the GroupClickListener is set.
And of course clickable is set to true, not false.
In my activity that displays this list I do this
ExpandableListView.OnGroupClickListener groupClickListener = new ...() {
#Override
public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
if (parent.isGroupExpanded(groupPosition))
parent.collapseGroup(groupPosition);
else
parent.expandGroup(groupPosition, true);
return true;
}
};
then assign this to the expandable listview.
listView.setOnGroupClickListener(groupClickListener);
The solution is pretty clear now as I found it.
I did set android:inputType="textNoSuggestions" what I found as advice because I sometimes experienced the TextView to show underlines as an indicator of spelling mistakes.
Probably this sets the view in focusable mode which captures the click.
I have to find out what's the deal with those underlines. Why only sometimes? Why at all? They are pretty stupid for an element that shall not accept any input but simply display static text.
But at least I found it and I hope this can help others to save some time.

add buttons at the end of selected listview item in android dynamically?

I have a listview in which my list contain textview,means I have a list of text where user can select the text, read it and set margin note at the end of that text and a button is created over there. So when next time the user read that text then the button/buttons is there and when click on that button a popup is open where he can see his margin note.
Now the problem is that he can create more than one margin note at the end. In this case I have to create one or more button dynamically at the end of that selected textview. So please help me I am unable to create more than one button dynamically.
There are a couple of ways to do it.
1) Create the buttons (max the user will require) already in the xml file of the list item element and set their visibility to invisible or gone. When the user selects it you can set the visibility to visible. The visibility can be set dynamically.
2) The other way is to add buttons programatically. List views do reuse views for performance purposes. this could explain as to why you are having trouble with the button moving positions. in this case you have identify the id of the list item and add and remove buttons every time the view is created in your getView method of your list adapter.
Remember to chk the condition on your getView method and set visibility or add/remove button.
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> list, View v, int pos, long id) {
System.out.println("I clicked row item");
button1= (Button)v.findViewById(R.id.button1);
button1.setVisibility(button1.isShown() ? View.GONE : View.VISIBLE);}

Android - OnItemClickListener listview

Here's what I want to do:
I have this list of tasks. When I click on one of the items I want this to appear:
Assuming that menubar can be found with
findViewById(R.id.menubar);
Can someone please tell what code I have to put onItemClickListener
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
NOTE: The menubar visibility its set as GONE.
Thanks!
Without looking at the code, a precise answer is not possible. However, when I first wanted to implement a "Quick Action Bar" similar to the one you have shown, I had followed this example here:
http://code.google.com/p/simple-quickactions/
And this with some styling put in: https://github.com/lorensiuswlt/NewQuickAction3D
Hope this helps....
It is a bit difficult without seeing any code, but I can give you some logic ideas.
Based on your description, I'm assuming that those quick bar controls are hidden on EACH listview item so, it goes:
Fotos (visible)
Action Controls (hidden)
[End of Item]
PROJECTOS (visible)
Action Controls (hidden)
...
I think the OnItemClick method gives you the view (item: Fotos, Projectos, etc) that was clicked as the 2nd argument. If you cast that back to whatever layout you used to create the listview items, you should be able to use findViewByID to get access to the hidden controls that are on each listview and make them visible.
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
//Assuming each item is a linear layout
LinearLayout itemAsLL = (LinearLayout)view;
//find the action bar controls
LinearLayout actionControlsLayout = itemAsLL.findViewById(R.name.of.actioncontrols.id);
//Make it visible
actionControlsLayout.setVisibility(VIEW.Visible);
}
You would need to find a way to hide those controls when another item is clicked? Maybe save the view (or position in the adapterView ) as a class variable and when another item is clicked, go to that view/find that view and hide the controls.
I finally found out the answer. Apparently, when you fill a view with lots of tasks, the adapter does not fill everything when you load the layout, so everytime you scroll down, hes putting more items on the listview.
What you have to do is make sure you put a field "loaded" and, in case is false, it does not show the menubar below that item, otherwise, when you click in one of the items of that list, when you scroll down, since they will have the same relative position (if you click on the first item, the position = 0, when you scroll down, there will be another position = 0) the menubar will appear on both items.
Hope it helps ;)

Listview changes image on number of lines when i'm trying to update one line

I have a list of items, which i'd like to "check" with filled star when user clicks on one item in the list.
I have a ListView with text & image, represented in XML layout, and using simple StringAdapter.
I've implemented the above by doing this:
this.listViewSub.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(final AdapterView parent, final View view, final int position,
final long id) {
ImageView img = (ImageView) view.findViewById(R.id.unchkImg);
img.setImageResource(R.drawable.starchk);
I guess its not the right way to do it, but i don't have any idea how else to code it.
This code for some reason changes few random items in the list, and after one click i have about 4-5 items with icon changed.
Any idea how to solve it correctly?
Thanks
It's hard to tell without code, but my first guess is that as the views get recycled, if you don't reset the ImageView to use the default "unchecked" image in the getView method, each time you star an item the view will remain starred, even it is used for a different item. So your adapter should just reset the ImageView's image resource ; though in that case, you'll have to remember which items you starred in order to reset the ImageView correctly : set it to do the default drawable or to the starred one.
At last, after long search - i've found the answer in an article:
http://www.codegod.biz/WebAppCodeGod/Android-ListView-with-dynamic-Images-AID588.aspx
That was a tough one.

Unknown ListView Behavior

I'm currently making a SMS Application in Android, the following is a code snippet from Inbox Listactivity, I have requested a cursor from the contentresolver and used a custom adapter to add custom views into the list.
Now, in the custom view i've got 2 TextViews (tvFullBody,tvBody)...
tvFullBody contains the Full SMS Text while tvBody contains a short preview (35 characters)
The tvFullBody Visibility is by default set to GONE.
My idea is, when the user clicks on a list item, the tvBody should dissappear(GONE) and the tvFullBody should become visible (VISIBLE). On Clicking again, it should revert back to its original state.
//isExpanded is a BitSet of the size = no of list items...keeps track of which items are expanded and which are not
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
if(isExpanded.get(position))
{
v.findViewById(R.id.tvFullBody).setVisibility(View.GONE);
v.findViewById(R.id.tvBody).setVisibility(View.VISIBLE);
}else
{
v.findViewById(R.id.tvFullBody).setVisibility(View.VISIBLE);
v.findViewById(R.id.tvBody).setVisibility(View.GONE);
}
isExpanded.flip(position);
super.onListItemClick(l, v, position, id);
}
The Code works as it is supposed to :) except for an undesired sideeffect....
Every 10th (or so) List Item also gets "toggled".
eg. If i Expand the 1st, then the 11th, 21th list items are also expanded...Although they remain off screen, but on scrolling you get to see the undesired "expansion".
By my novice analysis, i'm guessing Listview keeps track of 10 list items that are currently visible, upon scrolling, it "reuses" those same variables, which is causing this problem...(i didn't check the android source code yet.)
I'd be gratefull for any suggestion, on how i should tackle this! :)
I'm open to alternative methods aswell....Thanks in advance! :)
Your diagnosis of the problem almost correct. What's happening is that Android is reusing the Views it creates to display the list to save memory. Instead of creating a new View for every item in your list, Android creates just enough to fill the screen, updating them to show the relevant data for the items which are currently visible.
So when you show tvFullBody when the user clicks on an item, when that View is re-used later tvFullBody is still visible.
You may have to write you own ListAdapter to make sure the Views are displayed how you want. It should be as simple as extending the ListAdapter you are currently using and overriding the getView() method to ensure that tvFullBody is hidden.

Categories

Resources