I have a typical menu and I'm wanting to set a onLongClickListener for one of the items. In other words, I want this item to perform it's normal onOptionsItemSelected function, as well as, a long press function.
MenuItem item;
item = menu.findItem(android.R.id.home);
item.setOnLongClickListener(new OnLongClickListener() {
public boolean onLongClick(View v) {
Context context = getApplicationContext();
CharSequence text = "Long Press";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
return true;
}
});
I was able to do this by simply using setActionView on menuItem. You can follow this procedure if it helps.
for(int i = 0; i < menu.size(); i++){
View v = new View(this);
v.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
//Your longclick listener callback logic goes here
return false;
}
});
menu.getItem(i).setActionView(v);
}
Use the findItem method on Menu to get your views, and set your long click listener on each view.
This approach is not right, it disturbs entire flow, but here you go:
private interface OnMenuItemLongClickListener{
boolean onMenuItemLongClik(MenuItem m);
}
private void getMenuItemsView(Activity a, final Menu m, final OnMenuItemLongClickListener listener) throws NoSuchFieldException, IllegalArgumentException, IllegalAccessException{
View homeButton = a.findViewById(android.R.id.home);
ViewParent parentOfHome = homeButton.getParent().getParent(); //ActionBarView is parent of home ImageView, see layout file in sources
if (!parentOfHome.getClass().getName().contains("ActionBarView")) {
parentOfHome = parentOfHome.getParent();
Class absAbv = parentOfHome.getClass().getSuperclass(); //ActionBarView -> AbsActionBarView class
Field actionMenuPresenterField = absAbv.getDeclaredField("mActionMenuPresenter");
actionMenuPresenterField.setAccessible(true);
Object actionMenuPresenter = actionMenuPresenterField.get(parentOfHome);
Field actionMenuViewField = actionMenuPresenter.getClass().getSuperclass().getDeclaredField("mMenuView");
actionMenuViewField.setAccessible(true);
Object actionMenuView = actionMenuViewField.get(actionMenuPresenter);
Field childrenField= actionMenuView.getClass().getSuperclass().getSuperclass().getDeclaredField("mChildren");
childrenField.setAccessible(true);
Field menuField =actionMenuPresenter.getClass().getSuperclass().getDeclaredField("mMenu");
menuField.setAccessible(true);
Object menu = menuField.get(actionMenuPresenter);
Object[] menuItemsAsViews = (Object[])childrenField.get(actionMenuView);
View.OnLongClickListener longListener = new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
return listener.onMenuItemLongClik(m.findItem(v.getId()));
}
};
for(Object menuView:menuItemsAsViews ){
View v = (View)menuView;
v.setOnLongClickListener(longListener);
}
}
}
Usage:
#Override
public boolean onPrepareOptionsMenu(final Menu menu) {
new Handler().postDelayed(new Runnable(){
#Override
public void run() {
getMenuItemsViews(MainActivity.this, menu);
}}, 100); //must be called after the menu views are generated, otherwise NPE.
return super.onPrepareOptionsMenu(menu);
}
MenuItem's in the overflow WILL NOT be considered.
Related
I am using the ItemTouchHelper class to support drag and drop in my RecyclerView. While I am dragging an item around it visually updates (swaps rows) as expected. Once I drop the item, another **visual** drag occurs. For example (see diagram below) if I drag item "a" from index 0 to index 3, the correct list shows that item "b" is now at index 0. They recycler view repeats the operation and takes the new item at index 0 ("b") and drags it to index 3! This repeated drag happens no matter what index I drag from or to.
I called it a **visual** drag because the list I am submitting to my RecyclerView's ListAdapter is correctly ordered (verified by logs). And if I restart my app the list is in the correct order. Or if I call notifyDataSetChanged(), after the unwanted animation, it will order itself properly. What could be causing this second animation?
EDIT: According to the documentation, if you use equals() method in your areContentsTheSame() method (DiffUtil), "Incorrectly returning false here will result in too many animations." As far as I can tell, I am properly overriding this method in my POJO file below. I am stumped...
MainActivity.java
private void setListObserver() {
viewModel.getAllItems().observe(this, new Observer<List<ListItem>>() {
#Override
// I have verified newList has the correct order through log statements
public void onChanged(List<ListItem> newList) {
adapterMain.submitList(newList);
}
});
}
...
// This method is called when item starts dragging
public void onSelectedChanged(#Nullable RecyclerView.ViewHolder viewHolder, int actionState) {
...
if (actionState == ItemTouchHelper.ACTION_STATE_DRAG) {
currentList = new ArrayList<>(adapterMain.getCurrentList()); // get current list from adapter
}
...
}
// This method is called when item is dropped
public void clearView(#NonNull RecyclerView recyclerView,
#NonNull RecyclerView.ViewHolder viewHolder) {
...
// I have verified that all code in this method is returning correct values through log statements.
// If I restart the app, everything is in the correct order
// this is position of the where the item was dragged to, gets its value from the onMoved method.
// it's the last "toPos" value in onMoved() after the item is dropped
int position = toPosition;
// Skip this code if item was deleted (indicated by -1). Otherwise, update the moved item
if(position != -1) {
ListItem movedItem = currentList.get(position);
// If dragged to the beginning of the list, subtract 1 from the previously lowest
// positionInList value (the item below it) and assign it the moved item. This will ensure
// that it now has the lowest positionInList value and will be ordered first.
if(position == 0) {
itemAfterPos = currentList.get(position + 1).getPositionInList();
movedItemNewPos = itemAfterPos - 1;
// If dragged to the end of list, add 1 to the positionInList value of the previously
// largest value and assign to the moved item so it will be ordered last.
} else if (position == (currentList.size() - 1)) {
itemBeforePos = currentList.get(position - 1).getPositionInList();
movedItemNewPos = itemBeforePos + 1;
// If dragged somewhere in the middle of list, get the positionInList variable value of
// the items before and after it. They are used to compute the moved item's new
// positionInList value.
} else {
itemBeforePos = currentList.get(position - 1).getPositionInList();
itemAfterPos = currentList.get(position + 1).getPositionInList();
// Calculates the moved item's positionInList variable to be half way between the
// item above it and item below it
movedItemNewPos = itemBeforePos + ((itemAfterPos - itemBeforePos) / 2.0);
}
updateItemPosInDb(movedItem, movedItemNewPos);
}
private void updateItemPosInDb(ListItem movedItem, double movedItemNewPos) {
movedItem.setPositionInList(movedItemNewPos);
viewModel.update(movedItem); // this updates the database which triggers the onChanged method
}
public void onMoved(#NonNull RecyclerView recyclerView,
#NonNull RecyclerView.ViewHolder source, int fromPos,
#NonNull RecyclerView.ViewHolder target, int toPos, int x, int y) {
Collections.swap(currentList, toPos, fromPos);
toPosition = toPos; // used in clearView()
adapterMain.notifyItemMoved(fromPos, toPos);
}
}).attachToRecyclerView(recyclerMain);
RecyclerAdapterMain.java
public class RecyclerAdapterMain extends ListAdapter<ListItem, RecyclerAdapterMain.ListItemHolder> {
// Registers MainActivity as a listener to checkbox clicks. Main will update database accordingly.
private CheckBoxListener checkBoxListener;
public interface CheckBoxListener {
void onCheckBoxClicked(ListItem item); // Method implemented in MainActivity
}
public void setCheckBoxListener(CheckBoxListener checkBoxListener) {
this.checkBoxListener = checkBoxListener;
}
public RecyclerAdapterMain() {
super(DIFF_CALLBACK);
}
// Static keyword makes DIFF_CALLBACK variable available to the constructor when it is called
// DiffUtil will compare two objects to determine if updates are needed
private static final DiffUtil.ItemCallback<ListItem> DIFF_CALLBACK =
new DiffUtil.ItemCallback<ListItem>() {
#Override
public boolean areItemsTheSame(#NonNull ListItem oldItem, #NonNull ListItem newItem) {
return oldItem.getId() == newItem.getId();
}
// Documentation - NOTE: if you use equals, your object must properly override Object#equals().
// Incorrectly returning false here will result in too many animations.
// As far as I can tell I am overriding the equals() properly in my POJO below
#Override
public boolean areContentsTheSame(#NonNull ListItem oldItem, #NonNull ListItem newItem) {
return oldItem.equals(newItem);
}
};
#NonNull
#Override
public ListItemHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.recycler_item_layout_main, parent, false);
return new ListItemHolder(itemView);
}
#Override
public void onBindViewHolder(#NonNull ListItemHolder holder, int position) {
ListItem item = getItem(position);
Resources resources = holder.itemView.getContext().getResources();
holder.txtItemName.setText(item.getItemName());
holder.checkBox.setChecked(item.getIsChecked());
// Set the item to "greyed out" if checkbox is checked, normal color otherwise
if(item.getIsChecked()) {
holder.txtItemName.setTextColor(Color.LTGRAY);
holder.checkBox.setButtonTintList(ColorStateList
.valueOf(resources.getColor(R.color.checkedColor, null)));
} else {
holder.txtItemName.setTextColor(Color.BLACK);
holder.checkBox.setButtonTintList(ColorStateList
.valueOf(resources.getColor(R.color.uncheckedColor, null)));
}
}
public class ListItemHolder extends RecyclerView.ViewHolder {
private TextView txtItemName;
private CheckBox checkBox;
public ListItemHolder(#NonNull View itemView) {
super(itemView);
txtItemName = itemView.findViewById(R.id.txt_item_name);
// Toggle checkbox state
checkBox = itemView.findViewById(R.id.checkBox);
checkBox.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
checkBoxListener.onCheckBoxClicked(getItem(getAdapterPosition()));
}
});
}
}
public ListItem getItemAt(int position) {
return getItem(position);
}
}
ListItem.java (POJO)
#Entity(tableName = "list_item_table")
public class ListItem {
#PrimaryKey(autoGenerate = true)
private long id;
private String itemName;
private boolean isChecked;
private double positionInList;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getItemName() {
return itemName;
}
public void setItemName(String itemName) {
this.itemName = itemName;
}
public void setChecked(boolean isChecked) {
this.isChecked = isChecked;
}
public boolean getIsChecked() {
return isChecked;
}
public void setPositionInList(double positionInList) {
this.positionInList = positionInList;
}
public double getPositionInList() {
return positionInList;
}
#Override
public boolean equals(#Nullable Object obj) {
ListItem item = new ListItem();
if(obj instanceof ListItem) {
item = (ListItem) obj;
}
return this.getItemName().equals(item.getItemName()) &&
this.getIsChecked() == item.getIsChecked() &&
this.getPositionInList() == item.getPositionInList();
}
}
My problem is how to sum all checked items values, and how to manage their changed at check/uncheck events?
This is my method for check box toggle (check/uncheck) event.
holder.itemCheckBox.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
if (((CheckBox) v).isChecked())
{
double price = Double.parseDouble(catalogDatabases.get(position).getPriceItem());
holder.totalPrice = holder.totalPrice + price;
holder.listener.respond(holder.totalPrice);
}
else {
}
}
});
So first of all don't rely on the view as data.
I'll explain in your code
if (((CheckBox) v).isChecked())
You relying on view's isChecked method.
This may cause a bug because of android list recycling mechanism.
You can read about it in the link below
How ListView's recycling mechanism works
TL;DR views are being recycled so when you scroll your list.
So for example the user checked item number 3 and then scrolled the list down item number 13 for example may also be shown as checked even tho it isn't .
So when onClick triggers we need to save the checked state in some list
After the theoretical intro i'll show it in code.
//Here you'll need to create some boolean array or list to store
//checked not checked positions lets call it checked
boolean[] checkedArr = new boolean[catalogDatabases.size()];
// catalogDatabases.size represents your data set size
// note that all items will be false initially
#Override
public void onBindViewHolder(#NonNull final MyViewHolder holder, final int position) {
/**
* Other views binding.......
*/
holder.itemCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
checkedArr[position] = isChecked;
sumAllCheckedAndNotify();
}
}
);
}
(I decided to do the calculation on every check it more straight forward from making an update on the event)
private void sumAllCheckedAndNotify() {
double sum = 0;
for (int i = 0; i < checkedArr.length; i++) {
if(checkedArr[i]) {
sum += Double.parseDouble(catalogDatabases.get(i).getPriceItem());
}
}
// pudate the listener
listener.respond(sum, selectedCount);
}
Store totalPrice inside adapter and add/subtract value based on CheckBox state like below:
class YourAdapter extends ... {
double totalPrice = 0;
...
#Override
public void onBindViewHolder(#NonNull RecyclerHolder holder, int position) {
holder.itemCheckBox.setChecked(catalogDatabases.get(position).isSelected());
holder.itemCheckBox.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
double price = Double.parseDouble(catalogDatabases.get(position).getPriceItem());
boolean isSelected = !catalogDatabases.get(position).isSelected();
catalogDatabases.get(position).setSelected(isSelected);
if (isSelected) {
totalPrice += price;
} else {
totalPrice -= price;
}
holder.listener.respond(totalPrice);
notifyDataSetChanged();
}
});
}
}
Update your Model to hold checked/ unchecked state to persist data
class CatalogDatabase { //Assume this is your model class
private boolean isSelected
....
public boolean isSelected() {
return isSelected;
}
public void setSelected(boolean selected) {
isSelected = selected;
}
....
}
hey how can i know that the below checkbox when clicked is from which row of the list.
is there any way of knowing that. Every time yo is returning false which is its default value.
checkBox.setOnClickListener( new View.OnClickListener() {
public void onClick(View v) {
CheckBox cb = (CheckBox) v ;
callBlockOptions callBlockOptions = (callBlockOptions) cb.getTag();
callBlockOptions.setChecked( cb.isChecked() );
String yo;
if(callBlockOptions.getPosition()=="0")
{
callBlockOptions.setAllCalls();
}
if(callBlockOptions.getAllCalls() ==true)
yo="true";
else
yo="false";
Toast.makeText(getContext(), callBlockOptions.getPosition(), Toast.LENGTH_LONG).show();
}
});
}
After seeing your code, one thing you might want to try is setting listeners for the individual children within the ListView. You could do something like this:
ListView listView = (ListView)findViewById(R.id.listView);
int count = listView.getChildCount();
for (int x = 0; x < count; x++)
{
Class<? extends View> c = listView.getChildAt(x).getClass();
if (c == CheckBox.class)
{
CheckBox checkBox = (CheckBox)(listView.getChildAt(x));
checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener(){
#Override
public void onCheckedChanged(CompoundButton compoutButton, boolean isChecked) {
// TODO Auto-generated method stub
callBlockOptions.isChecked = isChecked;
}
});
}
else if (c == TextView.class)
{
TextView textView = (TextView)(listView.getChildAt(x));
textView.setOnEditorActionListener(new OnEditorActionListener(){
#Override
public boolean onEditorAction(TextView tView, int arg1, KeyEvent arg2) {
// TODO Auto-generated method stub
callBlockOptions.name = tView.getText().toString();
return true;
}
});
}
You probably want your other class to be like this:
public class callBlockOptions {
public static String name;
public static Boolean isChecked;
}
Then you can get and set name and isChecked like this:
callBlockOptions.isChecked = false;
Boolean boolVal = callBlockOptions.isChecked;
I have a gridView and I'd like to launch a different intent depending on the position of the item clicked.
I've instantiated the following onClick listener which includes the value for position:
gridview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
Toast.makeText(HelloGridView.this, "" + position, Toast.LENGTH_SHORT).show();
}
});
But I believe I'll need to add an if statement within it to determine which intent to launch depending on this position. So far I've come up with the following but I do not believe it is exactly what I'm looking for.
if position = 1 { Intent intent = new Intent(this, Activity1.class); }else if position = 2 { Intent intent = new Intent(this, Activity1.class); }
Any suggestions are greatly appreciated.
/**
* This activity presents a screen with a grid on which images can be added and
* moved around. It also defines areas on the screen where the dragged views can
* be dropped. Feedback is provided to the user as the objects are dragged over
* these drop zones.
*
*
* Like the DragActivity in the previous version of the DragView example
* application, the code here is derived from the Android Launcher code.
*
*
* The original Launcher code required a long click (press) to initiate a
* drag-drop sequence. If you want to see that behavior, set the variable
* mLongClickStartsDrag to true. It is set to false below, which means that any
* touch event starts a drag-drop.
*
*/
public class DragActivity extends Activity implements View.OnLongClickListener,
View.OnClickListener, View.OnTouchListener // ,
// AdapterView.OnItemClickListener
{
/**
*/
// Constants
private static final int HIDE_TRASHCAN_MENU_ID = Menu.FIRST;
private static final int SHOW_TRASHCAN_MENU_ID = Menu.FIRST + 1;
private static final int ADD_OBJECT_MENU_ID = Menu.FIRST + 2;
private static final int CHANGE_TOUCH_MODE_MENU_ID = Menu.FIRST + 3;
private boolean isErase = true;
private EditText et;
private TextView tx;
/**
*/
// Variables
private DragController mDragController; // Object that handles a drag-drop
// sequence. It intersacts with
// DragSource and DropTarget
// objects.
private DragLayer mDragLayer; // The ViewGroup within which an object can be
// dragged.
private DeleteZone mDeleteZone; // A drop target that is used to remove
// objects from the screen.
private int mImageCount = 0; // The number of images that have been added to
// screen.
private ImageCell mLastNewCell = null; // The last ImageCell added to the
// screen when Add Image is clicked.
private boolean mLongClickStartsDrag = true; // If true, it takes a long
// click to start the drag
// operation.
// Otherwise, any touch
// event starts a drag.
public static final boolean Debugging = false; // Use this to see extra
// toast messages.
/**
*/
// Methods
/**
* Add a new image so the user can move it around. It shows up in the
* image_source_frame part of the screen.
*
* #param resourceId
* int - the resource id of the image to be added
*/
public void addNewImageToScreen(int resourceId) {
if (mLastNewCell != null)
mLastNewCell.setVisibility(View.GONE);
FrameLayout imageHolder = (FrameLayout) findViewById(R.id.image_source_frame);
if (imageHolder != null) {
FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT,
Gravity.CENTER);
ImageCell newView = new ImageCell(this);
newView.setImageResource(resourceId);
imageHolder.addView(newView, lp);
newView.mEmpty = false;
newView.mCellNumber = -1;
mLastNewCell = newView;
mImageCount++;
// Have this activity listen to touch and click events for the view.
newView.setOnClickListener(this);
newView.setOnLongClickListener(this);
newView.setOnTouchListener(this);
}
}
/**
* Add one of the images to the screen so the user has a new image to move
* around. See addImageToScreen.
*
*/
public void addNewImageToScreen() {
int resourceId = R.drawable.sqwhite;
addNewTextToScreen();
int m = mImageCount % 3;
if (m == 1)
resourceId = R.drawable.sqdrk;
else if (m == 2)
resourceId = R.drawable.sqwhite;
addNewImageToScreen(resourceId);
}
private void addNewTextToScreen() {
// TODO Auto-generated method stub
// et.setVisibility(View.VISIBLE);
if (isErase) {
tx.setText(et.getText().toString());
} else {
tx.setText("");
et.setVisibility(View.GONE);
}
isErase = !isErase;
}
/**
* Handle a click on a view.
*
*/
public void onClick(View v) {
if (mLongClickStartsDrag) {
// Tell the user that it takes a long click to start dragging.
//toast("Press and hold to drag an image.");
}
}
/**
* Handle a click of the Add Image button
*
*/
public void onClickAddImage(View v) {
addNewImageToScreen();
}
/**
* onCreate - called when the activity is first created.
*
* Creates a drag controller and sets up three views so click and long click
* on the views are sent to this activity. The onLongClick method starts a
* drag sequence.
*
*/
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.demo);
et = (EditText) findViewById(R.id.editText1);
et.setVisibility(View.INVISIBLE);
tx = (TextView) findViewById(R.id.textView1);
GridView gridView = (GridView) findViewById(R.id.image_grid_view);
if (gridView == null)
toast("Unable to find GridView");
else {
gridView.setAdapter(new ImageCellAdapter(this));
// gridView.setOnItemClickListener (this);
}
mDragController = new DragController(this);
mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
mDragLayer.setDragController(mDragController);
mDragLayer.setGridView(gridView);
mDragController.setDragListener(mDragLayer);
// mDragController.addDropTarget (mDragLayer);
mDeleteZone = (DeleteZone) findViewById(R.id.delete_zone_view);
// Give the user a little guidance.
Toast.makeText(getApplicationContext(),
getResources().getString(R.string.instructions),
Toast.LENGTH_LONG).show();
}
/**
* Build a menu for the activity.
*
*/
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
menu.add(0, HIDE_TRASHCAN_MENU_ID, 0, "Hide Trashcan").setShortcut('1',
'c');
menu.add(0, SHOW_TRASHCAN_MENU_ID, 0, "Show Trashcan").setShortcut('2',
'c');
menu.add(0, ADD_OBJECT_MENU_ID, 0, "Add View").setShortcut('9', 'z');
menu.add(0, CHANGE_TOUCH_MODE_MENU_ID, 0, "Change Touch Mode");
return true;
}
/**
* Handle a click of an item in the grid of cells.
*
*/
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
ImageCell i = (ImageCell) v;
trace("onItemClick in view: " + i.mCellNumber);
}
/**
* Handle a long click. If mLongClick only is true, this will be the only
* way to start a drag operation.
*
* #param v
* View
* #return boolean - true indicates that the event was handled
*/
public boolean onLongClick(View v) {
if (mLongClickStartsDrag) {
// trace ("onLongClick in view: " + v + " touchMode: " +
// v.isInTouchMode ());
// Make sure the drag was started by a long press as opposed to a
// long click.
// (Note: I got this from the Workspace object in the Android
// Launcher code.
// I think it is here to ensure that the device is still in touch
// mode as we start the drag operation.)
if (!v.isInTouchMode()) {
toast("isInTouchMode returned false. Try touching the view again.");
return false;
}
return startDrag(v);
}
// If we get here, return false to indicate that we have not taken care
// of the event.
return false;
}
/**
* Perform an action in response to a menu item being clicked.
*
*/
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case HIDE_TRASHCAN_MENU_ID:
if (mDeleteZone != null)
mDeleteZone.setVisibility(View.INVISIBLE);
return true;
case SHOW_TRASHCAN_MENU_ID:
if (mDeleteZone != null)
mDeleteZone.setVisibility(View.VISIBLE);
return true;
case ADD_OBJECT_MENU_ID:
// Add a new object to the screen;
addNewImageToScreen();
return true;
case CHANGE_TOUCH_MODE_MENU_ID:
mLongClickStartsDrag = !mLongClickStartsDrag;
String message = mLongClickStartsDrag ? "Changed touch mode. Drag now starts on long touch (click)."
: "Changed touch mode. Drag now starts on touch (click).";
Toast.makeText(getApplicationContext(), message, Toast.LENGTH_LONG)
.show();
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* This is the starting point for a drag operation if mLongClickStartsDrag
* is false. It looks for the down event that gets generated when a user
* touches the screen. Only that initiates the drag-drop sequence.
*
*/
public boolean onTouch(View v, MotionEvent ev) {
// If we are configured to start only on a long click, we are not going
// to handle any events here.
if (mLongClickStartsDrag)
return false;
boolean handledHere = false;
final int action = ev.getAction();
// In the situation where a long click is not needed to initiate a drag,
// simply start on the down event.
if (action == MotionEvent.ACTION_DOWN) {
handledHere = startDrag(v);
}
return handledHere;
}
/**
* Start dragging a view.
*
*/
public boolean startDrag(View v) {
DragSource dragSource = (DragSource) v;
// We are starting a drag. Let the DragController handle it.
mDragController.startDrag(v, dragSource, dragSource,
DragController.DRAG_ACTION_MOVE);
return true;
}
/**
* Show a string on the screen via Toast.
*
* #param msg
* String
* #return void
*/
public void toast(String msg) {
Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
} // end toast
/**
* Send a message to the debug log. Also display it using Toast if Debugging
* is true.
*/
public void trace(String msg) {
Log.d("DragActivity", msg);
if (!Debugging)
return;
toast(msg);
}
} // end class
from the android documentation for GridView:
gridview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
Toast.makeText(HelloGridView.this, "" + position, Toast.LENGTH_SHORT).show();
}
});
edit:
It looks like the addNewImageToScreen() is where you are adding the ImageCells, so assuming that you can generate the intent in that scope..
Intent intent = new Intent(this, Activity1.class); // or whatever you want to run
ImageCell newView = ...
newView.setTag( intent );
then in your onItemClick:
public void onItemClick(AdapterView parent, View v, int position, long id) {
Toast.makeText(HelloGridView.this, "" + position, Toast.LENGTH_SHORT).show();
Intent intent = (Intent) v.getTag();
// now you can startActivity with your intent..
}
Make a string array with your classes, the in the onItemClick () create another string declared with the position (item) clicked.
public class MainActivity extends Activity {
String [] classes = {"act1", "act2"}; // activity files
GridView gridView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
gridView = (GridView) findViewById(R.id.gridView);
gridView.setAdapter(new ImageAdapter(this));
gridView.setOnItemClickListener(new OnItemClickListener() {
#SuppressWarnings("rawtypes")
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
String pos = classes[position];
try {
Class start = Class.forName("com.company.app." + pos); //Your package name
Intent i = new Intent(MainActivity.this, start);
startActivity(i);
} catch(ClassNotFoundException e){
e.printStackTrace();
}
Toast.makeText(
getApplicationContext(),
((TextView) v.findViewById(R.id.label))
.getText(), Toast.LENGTH_SHORT).show();
}
});
}
}
I am trying to populate data from my main activity using the adapter below. When i run the activity the screen remains blanked. I believe it has to do with the ArrayList which is null perhaps. Can someone tell me why my data is not being displayed. am on this bug for three days now :/
public class CopyOfSecondWheelAdapter extends AbstractWheelTextAdapter {
ArrayList<convertor_pst> PostList = new ArrayList<convertor_pst>();
public ImageLoader imageLoader;
Convertor main;
public CopyOfSecondWheelAdapter(Context context) {
super(context, R.layout.count_layout, NO_RESOURCE);
setItemTextResource(R.id.country_name);
}
#Override
public View getItem(int index, View cachedView, ViewGroup parent) {
View view = super.getItem(index, cachedView, parent);
ImageView img = (ImageView) view.findViewById(R.id.flag);
imageLoader.DisplayImage(PostList.get(index).getDevise(), img);
System.out.println("get item count:"+getItemsCount() );
TextView text = (TextView)view.findViewById(R.id.lib);
text.setText(PostList.get(index).getQuotite());
return view;
}
#Override
public int getItemsCount() {
return PostList.toArray().length;
}
#Override
protected CharSequence getItemText(int index) {
return PostList.get(index).getDevise().toString();
}
}
UPDATE:
In my Main class i have already an
ArrayList<convertor_pst> PostList = new ArrayList<convertor_pst>();
which is populated.
Here is my main class that is my convertor.class
ArrayList<convertor_pst> PostList = new ArrayList<convertor_pst>();
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.convertor);
context = this;
text_devise_two = (TextView)findViewById(R.id.text_spacetwo);
final WheelView country = (WheelView) findViewById(R.id.country);
country.setVisibleItems(10);
country.setViewAdapter(new FirstWheelAdapter(this));
edt_validate = (EditText)findViewById(R.id.edt_validate);
current_type_loc = (TextView)findViewById(R.id.current_type_loc);
refresh_header= (TextView)findViewById(R.id.refresh_header);
//set current time
Calendar c = Calendar.getInstance();
SimpleDateFormat df = new SimpleDateFormat("dd/MMM/yyyy");
String formattedDate = df.format(c.getTime());
refresh_header.setText(getResources().getString(R.string.mise_a_jour)+" "+formattedDate);
image_one = (ImageView)findViewById(R.id.image_one);
image_two = (ImageView)findViewById(R.id.image_two);
final WheelView currency = (WheelView) findViewById(R.id.currency);
currency.setVisibleItems(10);
currency.setViewAdapter(new CopyOfSecondWheelAdapter(this));
country.addChangingListener(new OnWheelChangedListener() {
#Override
public void onChanged(WheelView wheel, int oldValue, int newValue) {
if (!scrolling) {
}
}
});
country.addScrollingListener( new OnWheelScrollListener() {
#Override
public void onScrollingStarted(WheelView wheel) {
scrolling = true;
}
#Override
public void onScrollingFinished(WheelView wheel) {
scrolling = false;
//1.
wheelSelector(country.getCurrentItem());
}
});
currency.addScrollingListener( new OnWheelScrollListener() {
#Override
public void onScrollingStarted(WheelView wheel) {
scrolling = true;
}
#Override
public void onScrollingFinished(WheelView wheel) {
scrolling = false;
//1.
secondWheel(currency.getCurrentItem());
}
});
country.setCurrentItem(1);
currency.setCurrentItem(3);
new loadingTask().execute();
}
/*1. Change currency */
public void wheelSelector (int id){
if (id==0){
current_type_loc.setText("EUR");
image_one.setBackgroundResource(R.drawable.eur);
}else {
current_type_loc.setText("USD");
image_one.setBackgroundResource(R.drawable.usd);
}
}
class loadingTask extends AsyncTask<Void, Void,Void> {
#Override
protected void onPreExecute() {
// TODO Auto-generated method stub
pd = ProgressDialog.show(Convertor.this, "", "Chargement en cours..", true);
super.onPreExecute();
}
#Override
protected void onPostExecute(Void result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
pd.dismiss();
doc = Jsoup.parse(getxml,"", Parser.xmlParser());
taux = doc.select("taux");
for (int i = 0; i < taux.size(); i++) {
PostList.add(new convertor_pst(taux.get(i).getElementsByTag("devise").text().toString(),
taux.get(i).getElementsByTag("dateCours").text().toString(),
taux.get(i).getElementsByTag("libelle").text().toString(),
taux.get(i).getElementsByTag("quotite").text().toString(),
taux.get(i).getElementsByTag("fixing").text().toString()));
}
}
#Override
protected Void doInBackground(Void... params) {
// TODO Auto-generated method stub
envelope =
"soap content"
String requestEnvelope=String.format(envelope, "28-03-2013","true");
getxml = Util.CallWebService(URL,SOAP_ACTION,requestEnvelope);
System.out.println(getxml);
return null;
}
}
public void secondWheel(int index){
text_devise_two.setText(PostList.get(index).getDevise());
edt_validate.setText(" "+PostList.get(index).getFixing());
}
/*
*
* (non-Javadoc)
* #see android.app.Activity#onPause()
* check if activity go to background
*/
#Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
if (Util.isApplicationBroughtToBackground(Convertor.this)==true){
startActivity(new Intent(Convertor.this,Compte.class));
}
}
}
This is the original wheel adapter class
public class CopyOfSecondWheelAdapter extends AbstractWheelTextAdapter {
ArrayList<convertor_pst> PostList;
public ImageLoader imageLoader;
// Countries names
private String countries[] =
new String[] {"EUR", "USD","EUR", "USD","EUR", "USD","EUR", "USD","EUR", "USD","EUR", "USD"};
// Countries flags
private int flags[] = new int[] {R.drawable.eur, R.drawable.usd,R.drawable.eur, R.drawable.usd,R.drawable.eur, R.drawable.usd,R.drawable.eur, R.drawable.usd,R.drawable.eur, R.drawable.usd,R.drawable.eur, R.drawable.usd};
/**
* Constructor
*/
Convertor main;
public CopyOfSecondWheelAdapter(Context context) {
super(context, R.layout.count_layout, NO_RESOURCE);
setItemTextResource(R.id.country_name);
}
#Override
public View getItem(int index, View cachedView, ViewGroup parent) {
View view = super.getItem(index, cachedView, parent);
ImageView img = (ImageView) view.findViewById(R.id.flag);
img.setImageResource(flags[index]);
TextView text = (TextView)view.findViewById(R.id.lib);
text.setText("code");
return view;
}
#Override
public int getItemsCount() {
return countries.length;
}
#Override
protected CharSequence getItemText(int index) {
return countries[index];
}
}
As far as I understand
currency.setViewAdapter(new CopyOfSecondWheelAdapter(this));
this line creates the adapter, but you fill it up at this line :
new loadingTask().execute();
which is after, so you must call
yourAdapter.notifyDataSetChanged();
on your adapter to update the data.
Android Developer Help says
notifyDataSetChanged()
Notifies the attached observers that the
underlying data has been changed and any View reflecting the data set
should refresh itself.
So in your case you must
create an adapter (yourAdapter = new CopyOfSecondWheelAdapter ....)
assign it with the setViewAdater (WheelView.setViewAdapter(yourAdapter))
in the "postExecute" of your async task, do a call with yourAdapter.notifyDataSetChanged();
By the way, I am not sure to understand what you are doing, but in case you need to have a set of data displayed at two different locations, you don't need to duplicate (create a copy). The two list display can share the same adapter.
UPDATE
You have done an update to your question and I answer to that update :
In the original adapter the countries are not loaded in the async task. So when you assign the adapter, the display show the correct values because they are present in the adapter at the moment you assign it.
In your case, you load the values in the async task. When you create the adapter it is empty and you assign it empty, so the display shows an empty list. You should notify your display of the data change.
So in the original, no need to notify as the data is the correct one at the time of assignment. In your case you have to implement a notifyDataSetChanged(). Or change the type of adapter you are extending.
If I see it correctly, you have 2 times a variable name PostList which confuses you. One is created in your activity and one in your adapter and ass you call add() to the variable of your activity, the list in your adapter never gets the items.
Create a setter for the list in your adapter and set the list in your onPostExecute().