I've two list one with custom adapter and other with array adapter. So i want onItemClickListener to work with custom adapter listview and it will automatically disable when I start using same listview for array adapter. To initiate array adapter list I'm using button.
I've already try using ListView.setClickable(false)
. but this doesn't work.
If setClickable(boolean) not working then u can use flag to work with as per your behavior.It'll work as u want.
Inside the onitem click event put in an if condition, that is, if its the button to enable the click then perform operations, otherwise do not.
In the onClick of button1 set a integer flag to 1 & of button2 set flag to 2
Then define onItemClickListener like this
#Override
protected void onListItemClick(ListView l, View v, int position, long id)
{
// TODO Auto-generated method stub
super.onListItemClick(l, v, position, id);
if(flag ==1){
Toast.makeText(getApplicationContext(),"You selected "+ arr.get(position)+"", 1).show();
Intent ints= new Intent(getApplicationContext(),Activity2.class);
ints.putExtra("pos", position);
startActivity(ints);
}
}
NOTE: if the flag is not 1 then dont implement the required code thus by providing a condition you manipulate the onClick of listview.
Related
I am trying to change image of a list item on click.I have tried using view but on using it I am getting multiple items with changed image even though I just click on a single one.I am using a simplecursor adapter and code is same as given in this question:-changing image on listview at runtime in android.
And also I don't want to use a custom adapter.It will be really helpful if someone helps me in solving this problem just through simple adapter.
Current code
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
/*
imageView=(ImageView)l.getChildAt(position).findViewById(R.id.PlayPause);
imageView.setImageResource(R.drawable.pause);
*/
}
1- Do not use get child at when u are directly getting the child view as "View v" in the event.
2- you just need to call invalidate() method on the view once u set the new background resource.
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
imageView=((ImageView)v)
imageView.setImageResource(R.drawable.pause);
imageView.invalidate();
}
While you should make your list using a simple cursor adapter with an imageview ---- :-
http://thinkandroid.wordpress.com/2010/01/09/simplecursoradapters-and-listviews/
refer this link for that.
Why not use
ListView.OnItemClick(OnItemClickListener{})
http://developer.android.com/reference/android/widget/AdapterView.OnItemClickListener.html
I'm developing an app and I have one ListActivity, which has choice mode set to choice_mode_multiple. now i want to override method, which is called when one item is checked/unchecked, and I've found out that onCheckChanged() method is implemented only for RadioGroup and compund Button. how can I override something like this in ListActivity? or do I have to implement my own Adapter? thanks
Set onlistitemclick listener to listview , then in itemclick method you can get mylistView.getCheckedItemCount() , write the code of the operation to be performed.
If this is not clear let me know what you want to implement when item is checked.
mylistView = (ListView)customView.findViewById(R.id.mylist);
mylistView.setAdapter(mFolderAdapter);
mylistView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
mylistView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View v,
int arg2, long arg3) {
// TODO Auto-generated method stub
if(mylistView.isItemChecked(arg2)){
//Dooperation
}
// or
if(mylistView.getCheckedItemCount()>1){
//Dooperation
}
}
});
Use isItemChecked (int position) or setItemChecked(int position, boolean value) if you using standart array adapter and SINGLE or MULTIPLE choise mode. http://developer.android.com/reference/android/widget/AbsListView.html#isItemChecked(int)
Also you can create your custom adapter and inflate row layout with checkbox that can hold onClickListener and CheckedChangedListener.
Use search to find example.
In the tablet layout of my app, I have three ListFragments and one regular Fragment, let's call them Make, Model, Size, and Details. Initially the Make list is populated, then based on the Make selection, the Model list is populated; when a Model is selected, the Size list is populated; when a Size is selected, Details are shown. Each of these events (list item selection) is handled through the onListItemClick handler.
On startup, I want to populate the Make list, select the first Make in the list and have it go through the onListItemClick handler to populate the Model list (and so on so that all lists are populated and the details are shown - this should also be the behavior when any selection is made in any of the lists - select the first item in the next list until we get to showing the details). Note that I have control of the DB and for every Make there will always be at least one Model, for each Make/Model at least one Size, for each Make/Model/Size exactly one Detail.
So, I want to select the first item in the list and have it trigger the onListItemClick handler. I've tried the following (with appropriate bounds checking, etc), but it doesn't work.
getListView().setItemChecked(0, true);
The only changes to an "out of the box" ListFragment are to set the CacheColorHint as such
getListView().setCacheColorHint(R.color.GhostWhite);
where GhostWhite is set in the styles.xml as
<color name="GhostWhite">#88FFFFFF</color>
Any ideas?
Thanks in advance.
To select the first item in the list, first get focus from touch, then select the first item, then trigger the onclick handler
int position = 0;
getListView().requestFocusFromTouch();
getListView().setSelection(position);
getListView().performItemClick(getListView().getAdapter().getView(position, null, null), position, position);
Try setSelection(int position)
http://developer.android.com/reference/android/widget/ListView.html#setSelection(int)
1.) In the OnActivityCreated of your list fragment create an interface like so
public interface ListItemSelectedListener {
public void onListItemSelected(int index);
}
2.) Implement the interface in your activity
3.) Create private ListItemSelectedListener selectedListener in your list fragment
4.) Set selectedListener.onListItemSelected(0) in the OnActivityCreated.
This is how it worked for me using the Android Support Package - should be similar on HC
Try adding this,, it works in my project:
#Override
public void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
super.onListItemClick(l, v, position, id);
Intent intent = new Intent();
intent.setClass(getActivity(), someActivity.class);
startActivity(intent);
}
in The Fragment class in onActivityCreated(Bundle savedInstanceState) method, you can add the following:
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
//if the parent is in the landscape, make the first item selected
if (getActivity().getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
try {
if (mItems.size() > 0) {
//notify the activity that an item is selected
mListener.onMasterItemSelected(mItems.get(0));
}
} catch (Exception e) {
Log.e("exception", e.toString());
}
}
}
I have implemented a custom adapter for my ListView, that includes a CheckBox in each row.
As I have my click listener in my custom adapter, I'm forced to implement there my business logic (that is, what happens when a CheckBox is clicked... access the database, etc).
Is that correct? Wouldn't be a better practice to implement that business logic outside the custom adapter? (I think the adapter should only care about visualization).
Try this..hope it helps you
lv_Archiveist.setOnItemClickListener(new OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
// TODO Auto-generated method stub
if(view.findViewById(R.id.chkbox).isChecked())
{
//your method//
//you can also get the position of your selected checkbox by the parameter "position"
}
I'd only mark the item on CheckBox click, and perform all the logic on different button click that would be outside of a ListView (similar to how iPhone handle editing the table). I can't remember the situation where CheckBox is used to perform some action itself.
What is the best way to add an OnClickListener to a ListActivity that's Endless?
I am using cwac-endless adapter for my endless list.
The problem I am encountering is only my first batch of data that populates the list get's the click listener. The new data that get's fetched doesn't get the onclick listener.
I've tried adding a onclick listener in the following manner.
I used ListActivity onListItemClick
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
super.onListItemClick(l, v, position, id);
String url = prodList.get(position).getProdUrl();
Log.i("URL",url);
Intent webViewIntent = new Intent(ProductListActivity.this, ProductWebView.class);
webViewIntent.putExtra("URL", url);
startActivity(webViewIntent);
}
UPDATE Rookie mistake, the above code works fine, the problem was that I was referencing the original list that was passed in instead of referencing the list in my custom ArrayAdapter.