How to get the List Item position?
I was just trying to learn fragment. I saw the example of ListFragment on developer.android.com, but it's hard for me to understand.
So I used a ListView in Fragment but when I click the items I can't get its position. How can I obtain that?
public class Fragment_listview extends Fragment implements AdapterView.OnItemClickListener {
ListView listView;
String[] days={"Sun","Mon","Tue"};
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment,container,false);
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
listView= (ListView) getActivity().findViewById(R.id.listview);
ArrayAdapter<String> adapter=new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_1,days);
listView.setAdapter(adapter);
}
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Toast.makeText(getActivity(), "Position "+i, Toast.LENGTH_SHORT).show();
}
}
You need to set listener to your ListView
listView.setOnItemClickListener(this);
To do this job, usually you need to call setOnItemClickListener on your ListView. Its callback function includes the position of the view that is clicked as a argument. Here is an example:
myListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView parent, View v, int position, long id){
// DO STUFF HERE
}
});
As your Activity already implements AdapterView, you simply have to set the OnItemClickListener of your listView to the Activity itself, as below
listView.setOnItemClickListener(this);
disclaimer: part of this answer it take from here
Related
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
}
});
I'm new to Android development, I having some problem which unable to resolve.
I tried to implement a Spinner inside Fragment, now it having problems with the spinner.setOnItemClickListener() method,
It showing an error message:
"setOnItemClickListener
(android.widget.AdapterView.OnItemClickListener) in Spinner cannot be
applied to (anonymous
android.widget.AdapterView.OnItemSelectedListener)
Here is my code:
public class CheckAvailabilityFragment extends Fragment {
Spinner spinner;
ArrayAdapter<CharSequence> adapter;
#Override
public View onCreateView(LayoutInflater inflater, final ViewGroup container,
Bundle savedInstanceState) {
final View rootView = inflater.inflate(R.layout.fragment_check_availability, container,false);
spinner = (Spinner)getView().findViewById(R.id.spinner);
adapter = ArrayAdapter.createFromResource(getActivity().getBaseContext(),R.array.venue,android.R.layout.simple_list_item_1);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemClickListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getBaseContext(), parent.getItemAtPosition(position) + " selected", Toast.LENGTH_SHORT).show();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
return rootView;
}
}
The error message tells you that setOnItemClickListener() takes a parameter of type OnItemClickListener. However, you are giving it a OnItemSelectedListener. Since Spinner doesn't support OnItemClickListener, you probably mean to call onItemSelectedListener() which takes a OnItemSelectedListener as its parameter.
setOnItemClickListener will not works with spinner as been said at Spinner SetOnItemClickListener:
A spinner does not support item click events. Calling this method will
raise an exception.
Instead use
setOnItemSelectedListener(AdapterView.OnItemSelectedListener).
Try using the following:
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getBaseContext(), parent.getItemAtPosition(position) + " selected", Toast.LENGTH_SHORT).show();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
use getActivity() instead of getBaseContext()
Is there a reason you want getBaseContext()? You should be able to just pass getActivity() as the first parameter of createFromResource()
I have the following code segment which is activity. And i don't know how to convert to fragment. I am making grid view with image and text.
public class CustomGridViewMainActivity extends Activity
{
GridView gridView;
GridViewCustomAdapter grisViewCustomeAdapter;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
gridView=(GridView)findViewById(R.id.gridViewCustom);
// Create the Custom Adapter Object
grisViewCustomeAdapter = new GridViewCustomAdapter(this);
// Set the Adapter to GridView
gridView.setAdapter(grisViewCustomeAdapter);
// Handling touch/click Event on GridView Item
gridView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View v, int position, long arg3) {
String selectedItem;
if(position%2==0)
selectedItem="Facebook";
else
selectedItem="Twitter";
Toast.makeText(getApplicationContext(),"Selected Item: "+selectedItem, Toast.LENGTH_SHORT).show();
}
});
Can u please guide me through this..
Hopefully it will help you little bit !
public class CustomGridViewMainFragment extends Fragment
{
View view_root;
GridView gridView;
GridViewCustomAdapter grisViewCustomeAdapter;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
view_root = inflater.inflate(R.layout.main, container, false);
gridView=(GridView)view_root.findViewById(R.id.gridViewCustom);
// Create the Custom Adapter Object
grisViewCustomeAdapter = new GridViewCustomAdapter(this);
// Set the Adapter to GridView
gridView.setAdapter(grisViewCustomeAdapter);
// Handling touch/click Event on GridView Item
gridView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View v, int position, long arg3) {
String selectedItem;
if(position%2==0)
selectedItem="Facebook";
else
selectedItem="Twitter";
Toast.makeText(getApplicationContext(),"Selected Item: "+selectedItem, Toast.LENGTH_SHORT).show();
}
});
return view_root;
}
}
I have the below code, but the onItemClickListener doesn't work,
can anyone help me what the problem could be?
I have also added the Override and the setClickable but the problem still exists.
public class Show extends Activity implements AdapterView.OnItemClickListener {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.show);
...//Some codes in here
adapter = new MyAdapter(this, hadithList);
list = (ListView) findViewById(R.id.list);
list.setAdapter(adapter);
list.setClickable(true);
list.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getApplicationContext(), "clickedItemString", Toast.LENGTH_SHORT).show();
});
}
In your Activity, you are first implementing AdapterView.OnItemClickListener and than you are setting AdapterView.OnItemClickListener to your ListView. You should choose one of the methods, not both.
If you choose to stick up implementing OnItemClickListener, you should add this to your Activity :
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getApplicationContext(), "clickedItemString", Toast.LENGTH_SHORT).show();
}
and add this to your ListView : list.setOnItemClickListener(this);
If you choose to not implement OnItemClickListener just do :
list.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getApplicationContext(), "clickedItemString", Toast.LENGTH_SHORT).show();
}
});
This should work properly.
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()));