setOnItemClickListener not working - android

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);
}
}

Related

Clickable custom array list

How can I make this custom array list clickable to go to the others activities because I tried the intents but it doesn't work
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayList<Names> namesArrayList = new ArrayList<Names>();
namesArrayList.add(new Names(R.drawable.call_centre, "Call Centre"));
namesArrayList.add(new Names(R.drawable.soco_academy_icon, "Academy"));
NamesAdapter NamesListAdapter = new NamesAdapter(this, namesArrayList);
ListView list = (ListView) findViewById(R.id.List_View);
list.setAdapter(NamesListAdapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
}
});
}
}
In onItemClick() you can determine which item was clicked by doing this:
Name selectedName = NamesListAdapter.getItem(position);
Then you can do whatever you want with that.

ListView, OnItemClick not being called?

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

Listview item click issue

I have added leadbolt ad(entry ad). The advertising works correctly but listview.click doesn't work when I close ad from close sign. (Listview.click does not do anything, It works when I remove AdController)
public class SoundList extends ListActivity {
int [] soundfile;
MediaPlayer mediaPlayer;
private AdController myController;
final Activity act = this;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
myController = new AdController(act, "111111111");
myController.loadAd();
soundfile= new int[] {R.raw.sound1,R.raw.sound2.....};
String[] sounds= getResources().getStringArray(R.array.sounds);
// Binding Array to ListAdapter
this.setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, R.id.label, sounds));
ListView lv = getListView();
lv.setDescendantFocusability(ListView.FOCUS_BLOCK_DESCENDANTS);
// listening to single list item on click
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Intent intent = new Intent(getApplicationContext(), SingleListItem.class);
intent.putExtra("position", position);
startActivity(intent);
}
});
}
Having a focusable item in a row of a ListView causes the OnItemClickListener NOT to be invoked.
To fix this issue add following code to row view.
XML:
android:descendantFocusability="blocksDescendants"
Java:
listItem.setDescendantFocusability(FOCUS_BLOCK_DESCENDANTS);

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()));

ListView update

I have two activities with ListView. This two activitises have individual layout-file. First have myListView01, second have myListView02. How to update listView's adapters?
I do so:
ListView list = (ListView)findViewById(R.id.myListView01);
myAdapter = new ArrayAdapter<String>(this, R.layout.item01, R.id.label, array01);
list.setAdapter(myAdapter);
I remove some items from the array01 and I need to update my listView without this elements.
But this solution to ListActivity does not work:
myAdapter.notifyDataSetChanged();
my code:
package ...;
import ...;
public class Class01 extends Activity
{
ArrayAdapter<String> mAdapter;
ArrayList<String> array01 = new ArrayList<String>();
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.layout01);
ListView list = (ListView)findViewById(R.id.my_list01);
mAdapter = new ArrayAdapter<String>(this, R.layout.item01, R.id.label, array01);
list.setAdapter(mAdapter);
}
public void onListItemClick (ListView parent, View v, int position, long id)
{
array01 = deleteElementByPosition(array01, position);
mAdapter.notifyDataSetChanged(); //does not work
}
public void deleteElementByPosition()
{
...
}
}
You have not set onItemClickListener for your ListView.
Extend your Class01 from ListActivity. Activity class doesn't have onListItemClick() method so it is never invoked. Or set your listener with setOnItemClickListener() for your ListView.

Categories

Resources