MultiChoiceModeListener on listView not working as expected - android-listview

I have a listView and setting multiChoiceModeListener on it. It works fine. Now to play an audio item inside the listView I have written
ViewHolder.AuidoXmlLayoutItem.setOnClickListener({...playAudioCode...});
inside the getView of listView adapter class.
because of this now the multiChoiceModeSelection dosent not show selection of listItem when I longPress on AuidoXmlLayoutItem and hence does not show ContextualActionBar.
How can I keep the onClick of the audio item layout and still allow ContextualActionBar to appear on long click of audio item layout

Try to use OnItemClickListener rather than OnClickListener. Follow this way,
view.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// playAudioCode
// change the checkbox state
ViewToChecked checkedTextView = ((ViewToChecked)view);
checkedTextView.setChecked(!checkedTextView.isChecked());
}
});
You might get a concept.

Related

How do I prevent a CellTable RowElement from being redrawn after a SelectionChangehander fires?

I'm probably doing something else wrong but I've followed examples given here:
How to remove a row from the Cell Table
and
GWT get CellTable contents for printing or export
to accomplish my goal and the result is close but not quite right.
I have a page with two widgets. The first wiget contains a CellTable that uses an aSync ListDataProvider to pull results and populate a table. The table has a selection change event handler associated with it that loads further details about the selected item into the second widget below it.
public OrderAdminTable() {
selectionModel.addSelectionChangeHandler(new SelectionChangeEvent.Handler() {
#Override
public void onSelectionChange(SelectionChangeEvent event) {
OrderAdminListProxy selected = selectionModel.getSelectedObject();
if (selected != null && orderSnapShot != null) {
orderSnapShot.loadSnapShot(selected);
}
}
});
initTable();
this.addStyleName("order-list fixed_headers BOM");
this.setSelectionModel(selectionModel);
}
Once the second widget has loaded the details about the selected item, the user can remove the item from the table/list by clicking a button in the RootPanel that is the parent of both widgets.
searchView.getCmdReview().addClickHandler(new ClickHandler() {
#Override public void onClick(ClickEvent event) {
searchView.getOrderAdminSnapshot().reviewOrder();//this line calls a web service that deletes the item from the server data
dataProvider.getList().remove(searchView.getOrderAdminSnapshot().getSelectedOrder());
for(int i=0;i<table.getRowCount();i++){
TableRowElement row = table.getRowElement(i);
for(int j=0;j<row.getCells().getLength();j++){
if(row.getCells().getItem(j).getInnerText().contains(searchView.getOrderAdminSnapshot().getSelectedOrder().getSalesOrderNumber())){
row.setAttribute("removed", "true");
row.addClassName("hidden");
}
}
}
}
});
This all works fine until you select another item in the table. When that happens, the selection change event seems to redraw the table and remove my custom attribute and class from the previously selected item. This makes it appear in the list again.
The ultimate goal here is to avoid a round trip to the server to pull new results when you remove an item from the list. The line "searchView.getOrderAdminSnapshot().reviewOrder();" makes a web service call that removes the item from the data on the server side so it does not appear in subsequent reloads.
Is there some way to force the selection change event to maintain the state of the table row that was previously selected? Is there a better way to remove the selected item from the list? Any advice would be appreciated.
Once you remove the object from the list dataProvider.getList().remove, it should disappear from the table. There is no need to hide the row - this row should be gone. So your loop should never find it.

How to use onClick and onLongPress in ListActivity?

I want to create a listview from which we can use onclick and in which on long press a Context menu comes out. the code is
public class MainActivity extends ListActivity{
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String quizlist[]={"Normal","MCQ 2 Options","MCQ 3 options","MCQ 4 Options"};
ArrayAdapter<String> ab=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,quizlist);
setListAdapter(ab);
}
}
Thanks in advance
You can register an AdapterView.OnItemLongClickListener on the ListView. So what you need to do is find the list view:
in onCreate try
((ListView) getView).setOnItemLongKlickListener(...)
or
((ListView) findViewById(<the id of your list view>).setOnItemLongKlickListener(...)
When implementing an OnItemLongClickListener you will have to override the onItemLongClick method:
From the documentation:
public abstract boolean onItemLongClick (AdapterView<?> parent, View view, int position, long id)
Added in API level 1 Callback method to be invoked when an item in
this view has been clicked and held. Implementers can call
getItemAtPosition(position) if they need to access the data associated
with the selected item.
Parameters
parent The AbsListView where the click happened
view The view within the AbsListView that was clicked
position The position of the view in the list
id The row id of the item that was clicked
Returns true if the callback consumed the long click, false otherwise
So, parent is your ListView. view is the view of the *long clicked' list item. position is the position inside the list and thus also the position in your array. For id I am not sure whether the default implementations returns a constant or the position.

How to pass data from listview to next(details) screen

I have created one sstatic Arraylist and displayed the data in listview using the custom adapter. My requirement is when i click on the listitem, i have to show the details of particular listitem in the next screen. How can i pass the selected listitem data from listview screen to details screen.
I have tried to pass the data using the parcelable concept. But when I click on the listitem, i am getting "Null pointer exception"
Any complete example will be very helpful.
Thanks in advance.
It can be acheived in following way.I'm assuming that your list item is a text view.
Add onItemClick Listener to your listview,get Selected Item's text on click and pass it with itent extras to NextActivity.
listview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View p_view,
int p_pos, long arg3) {
TextView text= (TextView ) p_view.findViewById(R.id.textview1); //id of textview from layout file that you are inflating.
String mytext=text.getText();
Intent i = new Intent(Context context,NextActivity.class);
i.putExtra("text",mytext);
startActivity(i);
}
});
And on the next activity,retreive the text from extras.
Bundle extras = getIntent().getExtras();
String text_value= extras.getString("text");

Radio button change handler refreshing the page

I'm using UploadItem, RadioGroupItem and some other widgets. RadioButton is having onChangeHandler which will decide what all other components need to be displayed. I've uploaded some file using UploadItem. Then I changed the radio button selection. On changing the radio button, required widgets are getting displayed properly but whatever file I'd selected using UploadItem is going away. Fresh UploadItem widget is getting displayed. In other words page is getting refreshed.
My requirement is whenever I change radio button option, required widget should displayed along with that whatever file I had selected using UploadItem should remain same.
My Code is something like this:
UploadItem upload = new UploadItem();
RadioGroupItem radioGroup = new RadioGroupItem();
HashMap map = new HashMap();
map.put("option1","option1");
map.put("option2","option2");
radioGroup.setValueMap(map);
TextItem textbox = new TextItem();
radioGroup.addChangeHandler(new ChangeHandler(){
public void onChanged(ChangedEvent event) {
String radioValue =((String)event.getValue());
if(radioValue.equalsIgnoreCase("option2")){
textbox.show();
}else{
textbox.hide();
}
}
});
Add all created widgets to DynamicForm object using dynamicForm.setFields(all created widgets)
Changing the radio button should hide and show the textBox. But while doing that page is getting refreshed and whatever file we had selected using UploadItem is lost.
As per the documentation for hide() and show() of FormItem class, invocation of any of these methods, will cause the DynamicForm to be redrawn.
So it may cause the problem you're getting.
To overcome this issue, I would suggest you to put UploadItem in a separate DynamicForm.
fire an event on radio Selection change as
radioButton.addListener(Events.Change, new Listener<BaseEvent>() {
#Override
public void handleEvent(BaseEvent be) {
if(radioButton.getValue()){
//fire an event here for ur widget
}
}
});

How to add a GWT click listener to an Image?

I want to click on an image and therefore want to register (e.g.) a ClickHandler. The image I get from a ClientResource. This works so far to set the image into a table cell:
MyResources.INSTANCE.css().ensureInjected();
Image colorImage = new Image( MyResources.INSTANCE.colorImage() );
Element colorImageElement = colorImage.getElement();
colorImage.addClickHandler(new ClickHandler() {
public void onClick(ClickEvent event) {
System.out.println( event );
}
} );
TableElement table = Document.get().createTableElement();
TableRowElement headRow = table.insertRow(-1);
headRow.insertCell(-1).appendChild( colorImageElement );
RootPanel.get().getElement().appendChild( table );
How can I add a listener to the icon? I tried ClickHandler and to put the image on a PushButton and get the Element from this PushButton but all don't work.
But mind, if I add the widget (Image is a Widget) to a panel it works!
RootPanel.get().add( colorImage );
But I am not working with widgets here but with the Element. So the handler disappears and that's the point I don't get how to preserve this added handler information.
In the end I would like to build a table with different rows where I can click on the icon I get a popup menu and thereby change the colour of the row.
You should be able to just add a ClickHandler (or a MouseDownHandler if that fits your needs better).
Like this:
colorImage.addClickHandler(new ClickHandler() {
public void onClick(ClickEvent event) {
// Do something....
}
});
Don't unwrap your widget and append only the DOM elements. The Widget class allows your code to refer to both elements and events at the same time, and deals with possible memory leaks, as well as grouping your code in logical ways.
This might make sense for other frameworks, but in GWT you almost always want to work with the Widgets directly, adding them together, then appending them to the RootPanel.
If you really want to use a html table to build this up, look at the com.google.gwt.user.client.ui.HTMLTable subclasses, com.google.gwt.user.client.ui.Grid and com.google.gwt.user.client.ui.FlexTable. This probably should never be necessary, unless you are adding multiple items to the table - when trying to specify layouts, use actual layout classes.
did you tried to add image.sinkEvents( Event.ONCLICK | Event.MOUSEEVENTS )?
The image has to be inside a focus widget. I don't know why that is, but somewhere the events don't get propagated right and the DOM events don't fire.