ListView, OnItemClick not being called? - android

I have a FragmentActivity that controls a ListFragment; that ListFragment contains a generic ListView, Adapter, and draws an ArrayList from a Singleton that I have created.
When in the onCreateView method of my ListFragment I put the following code:
public View onCreateView(LayoutInflater viewInflation, ViewGroup container,
Bundle SavedInstantState) {
cycleviewfragment = viewInflation.inflate(
R.layout.cycleviewfragment_page, container, false);
context = getActivity().getApplicationContext();
Singleton mySingleton = Singleton.getInstance();
basicList = (ListView) cycleviewfragment.findViewById(android.R.id.list);
adapter = new ArrayAdapter<listControlObject>(getActivity()
.getApplicationContext(), android.R.layout.simple_list_item_1,
mySingleton.getA1());
this.setListAdapter(adapter);
addButton = (Button) cycleviewfragment.findViewById(R.id.addbutton);
addButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent myIntent = new Intent(getActivity(),
listaddactivity.class);
getActivity().startActivity(myIntent);
}
});
basicList.setOnItemClickListener(new ListView.OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
Log.d("basicListtester", "Testing onClickItem call");
Intent myIntent = new Intent(getActivity(),
listdetailactivity.class);
myIntent.putExtra("selectedObjectIndex",arg2);
getActivity().startActivity(myIntent);
}
});
adapter.notifyDataSetChanged();
return cycleviewfragment;
}
Any ideas as to why when I add items to my list they do not react and the OnItemClick is not called?
Thanks guys.
[Update]
I tried implementing it with basicList.setAdapter(adapter); which still did not work.
also tried having my ListFragment implement OnItemClickListener and added the method to the class; which did not work either.

Since you use ListFragment you shouldn't set onItemClickListener to your list. There is already a method in ListFragment that you should override.
#Override
public void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
//Do your thingy.
}

Use
basicList.setListAdapter(adapter);
instead of
this.setListAdapter(adapter);
Use same instance of ListView for setting Adapter and setOnItemClickListener.

you should override onListItemClick, since your class extends ListFragment. From the doc:
This method will be called when an item in the list is selected.
Subclasses should override.
Here the documentation

Related

Can't start activity from fragment in setOnClickListener

I want to start a new activity from list view items from a fragment. But this isn't working. Here's the code:
public class SettingsF extends Fragment {
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View root = (ViewGroup) inflater.inflate(R.layout.fragment_settings, container, false);
String[] menuitems = {"Context Setup", "Set-Up Custom Texts"};
ListView listView = (ListView) root.findViewById(R.id.listview_settings);
ArrayAdapter<String> listViewAdapter = new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_list_item_1, menuitems);
listView.setAdapter(listViewAdapter);
listView.setOnClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if (position == 1){
Intent intent;
intent = new Intent(getActivity(), contactselect.class);
startActivity(intent);
}
}
});
return root;
}
If you want to use event listener on ListView then you can use listview.setOnItemClickListener(new AdapetView). This (setOnClickListener) listener can't work on listview.
Listview contain multiple data.
Try this if you want to open an activity on item clicked:
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// your code
}
});

On Item Click Listener for List View extending ArrayAdapter

What I have tried:
public class EntryAdapter extends ArrayAdapter<Item> {
private Context context;
private ArrayList<Item> items;
private LayoutInflater vi;
public EntryAdapter(Context context,ArrayList<Item> items) {
super(context,0, items);
this.context = context;
this.items = items;
vi = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public View getView(int position, final View convertView, ViewGroup parent) {
// // // // NON-FUNCTIONING CODE BELOW
AdapterView.OnItemClickListener listener = new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(getContext(), NewsItemActivity.class);
convertView.getContext().startActivity(intent);
}
};
}
The AdapterView.onItemClickListener doesnt yield any errors but doesnt seem to function whatsoever.
What is the proper way of setting this onClick Listener?
Note: I have to set it in this adapter class, not the main class for my own reasons.
You shuldn't implement a listener inside the getView() unless you what set a listener on a particular view inside your row layout.
You should instead use setOnItemClickListener() method on your ListView:
ListView lv = (ListView) findViewById(R.id.your_listview);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(context, NewsItemActivity.class);
context.startActivity(intent);
}
});
EDIT:
If, for each action inside the onclick, you need information that resides in your Objects (Item) then you can get it in this way:
Item item = (Item)listview.getAdapter().getItem(position);
from inside of onItemClick() method
You can use:
convertView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Make what you want
}
});
Or depending on your item view, you can make the OnCLickListener on your global layout of the item or a specific item's layout
Try by this code :-
Spinner cb_tv;
ArrayList<String> list = new ArrayList<String>();
ArrayAdapter<String> dataAdapter;
list.add(phone1);
list.add(name1);
dataAdapter = new ArrayAdapter<String>(Display_Tank.this,android.R.layout.simple_spinner_item, list);
cb_tv.setAdapter(dataAdapter);
cb_tv.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(),"Clicked",Toast.LENGTH_SHORT).show();
}
});
at the place of cb_tv Spinner You can use ListView
First of all, the best way to achieve this is to use your own ViewHolders in ListView.
This is an example of a custom ViewHolder
class MyViewHolder extends ViewHolder implements View.OnItemClickListener {
public MyViewHolder(View view) {
/* Implement your views here
* like this: mTextView = (TextView) view.findViewById(R.id.myId);
*/
view.setOnItemClickListener(this);
}
#Override
public void onClick(View view) {
Intent intent = new Intent(getContext(), NewsItemActivity.class);
convertView.getContext().startActivity(intent);
}
}
And on your getView method you create this ViewHolder and populate your Views with your data.
You can try this way... it is working in my case Change your context to
Activity activity = (Activity) context;
convertView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(activity, NewsItemActivity.class);
activity.startActivity(intent);
}
});

Android - ListFragment OnClickListener is not working

I have a ListFragment in my android application, I have got it to work, but the OnClick Listener is not working, I tried just making it so that when any item on the list is selcted a Toast appears and it is not happening, there is no Error so I have no LogCat to post
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View v = inflater.inflate(R.layout.main, container, false);
ArrayList<HashMap<String, String>> menuItems = new ArrayList<HashMap<String, String>>();
//...
ListAdapter adapter = new SimpleAdapter(getActivity(), menuItems,
R.layout.list_item,
new String[] { KEY_NAME, KEY_DESC, KEY_COST }, new int[] {
R.id.name, R.id.desciption, R.id.cost });
setListAdapter(adapter);
ListView lv = (ListView)v.findViewById(android.R.id.list);
lv.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// getting values from selected ListItem
Toast.makeText(getActivity().getApplicationContext(), "Not Configured",
Toast.LENGTH_SHORT).show();
}
});
return v;
}
Thanks
if your class extends ListFragment than everything you need to do is just overriding its onListItemClick method.
#Override
public void onListItemClick(ListView l, View v, int pos, long id) {
super.onListItemClick(l, v, pos, id);
Toast.makeText(getActivity(), "Item " + pos + " was clicked", Toast.LENGTH_SHORT).show();
}
The ListFragment subclass already has it's overriden onListItemClick method.
The doc says:
This method will be called when an item in the list is selected. Subclasses should override
So there is no need to declare another listner for your listview.
Removing .getApplicationContext() should work. I have some code similar to yours from an app I made. Its from inside a fragment as well though works without problem. The db.remove is probably irrelevant to your code though because this code was written for an app with a database. Also maybe try changing new OnItemClickListener to new AdapterView.OnItemClickListener
listItem.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
db.remove( (int) l);
Toast.makeText(getActivity(), "Item Deleted", Toast.LENGTH_LONG).show();
}
});
If that doesn't work, maybe try making a Context instance variable like:
private Context ctx = getActivity();
or
private final Context ctx = getActivity();
I have never worked with ListFragments before though, so I am not sure if anything I wrote will work.
put
android:focusable="false"
android:clickable="false"
to all itens in your row
Make sure you
1.shouldn't have onclicklistener inside your Adapter
2.inside your XML layout
R.layout.main
Listview should be initialized like
<ListView
android:onClick="#id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="#+id/textHeader"
android:layout_margin="5dp"
android:divider="#color/DeepPink"
android:dividerHeight="1sp"
android:gravity="center"
android:horizontalSpacing="1dp"
android:visibility="visible" >
here id android:onClick="#id/list" important
ListView should be initialize like ListView lv = getListView if your extending your fragment by ListFragment instead Fragment

ListFragment onItemClickListener not working

I'm using the tabbed layout (with swipe).
Here I have 3 tabs with controlled by a SectionsPagerAdapter. Each tab is a ListFragment.
Now I want to get an event fired when one of the items in the list is clicked. I would like a listener for each tab.
Here's the code now (Which isn't working, event is not fired).
public class NyhederFragment extends ListFragment {
public static final String ARG_SECTION_NUMBER = "section_number";
private static final String TAG="NyhederFragment";
private List<Item> newsItems;
private ArrayList newsHeadlines;
private ArrayAdapter adapter;
private BroadcastReceiver updateReciever;
public NyhederFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
ListView newsList = new ListView(getActivity());
newsList.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
newsList.setId(R.id.list);
DatabaseHelper dbConn = new DatabaseHelper(getActivity());
newsItems = dbConn.getAllItemsFromNews();
newsHeadlines = new ArrayList();
for(Item i : newsItems){
newsHeadlines.add(i.getTitle());
}
adapter = new ArrayAdapter(getActivity(), android.R.layout.simple_list_item_1, newsHeadlines);
setListAdapter(adapter);
newsList.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Log.i("debug", "single click");
}
});
dbConn.close();
getActivity().registerReceiver(updateReciever, new IntentFilter("ArticlesUpdated"));
return newsList;
}
}
What is it, I'm doing wrong?
Thanks a lot in advance!
If you are using ListFragment then you can simply use its override method onListItemClick()
#Override
public void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
super.onListItemClick(l, v, position, id);
}
It is due to custom list item. The default focus is with custom list item (button/textview). It causes this issue.
Please add android:descendantFocusability="blocksDescendants" in root layout of list element.
Hope this will help someone.
change setListAdapter(adapter); to newsList.setAdapter(adapter);
If you wanted to create or re-use or an existing handler that implements AdapterView.OnItemClickListener, rather than implementing onListItemClick() within the ListFragment, you can do so by getting a reference to the ListView of the ListFragment and setting its listener. In your ListFragment.onResume() you could do:
#Override
public void onResume() {
super.onResume();
ListView listView = getListView();
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Log.d("TAG", "Stop touching me");
}
});
}
In my case, I can use the same listener from an Activity with a ListView, a ListActivity or a ListFragment, e.g.
listView.setOnItemClickListener(new MyListViewOnClickListener(getActivity()));

setOnItemClickListener not working

I am not able get onItemCickListener to work. All I am doing is opening up a fragment using intents. Please do let me know if I am doing anything fishy here ?
public class MyListFragment extends ListFragment {
private ArrayList<String> myList = null;
private ArrayAdapter<String> myAdapter;
#Override
public void onActivityCreated(Bundle savedInstanceState){
Log.d("Example:", "In Fragement Calss");
super.onActivityCreated(savedInstanceState);
Resources myResources = getResources();
myList = new ArrayList<String>(Arrays.asList(myResources.getStringArray(R.array.myExamArray)));
myAdapter = new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_1,myList);
setListAdapter(myAdapter);
ListView lv = (ListView) getActivity().findViewById(R.id.listView);
lv.setOnItemClickListener(new OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Intent myIntent = new Intent(view.getContext(), DetailsFragment.class);
startActivity(myIntent);
}
});
}
}
Note that the ListView blocks clicks of an item that contains at least one focusable
descendant but it doesn’t make the content focus-reachable calling
setItemsCanFocus(true). One workaround is by disabling focusability of descendants, using
android:descendantFocusability="blocksDescendants"
in the layout defining your list item. (First learned of this myself from http://cyrilmottier.com/2011/11/23/listview-tips-tricks-4-add-several-clickable-areas/, but a few other SO posts also point this out.) Does your layout contain buttons? If so, you'd fall into this case.
I don't see where you are inflating a specific XML for this Fragment... but it is possible you are referencing the wrong ListView.
Regardless you should use the existing onListItemClick method:
public class MyListFragment extends ListFragment {
private ArrayList<String> myList = null;
private ArrayAdapter<String> myAdapter;
#Override
public void onActivityCreated(Bundle savedInstanceState){
// Remove the OnItemClickListener, but keep everything else
}
#Override
public void onListItemClick (ListView l, View v, int position, long id) {
Intent myIntent = new Intent(this, DetailsFragment.class);
startActivity(myIntent);
}
}

Categories

Resources