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;
}
}
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
}
});
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
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()));
I'm trying to set an OnItemClickListener for my ListView in Android, but i can't get it to work.
This is what i have so far:
public class MenuFragment extends SherlockFragment
{
ArrayList<Item> items = new ArrayList<Item>();
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
ListView list = (ListView)view.findViewById(R.id.list_mainmenu);
// some code here where i add items to an ArrayList...
// Then i add the ArrayList to an EntryAdapter
EntryAdapter adapter = new EntryAdapter(this.getActivity().getBaseContext(), items);
list.setAdapter(adapter);
list.setClickable(true);
list.setOnItemClickListener(AdapterView.OnItemClickListener()) {
// ...
}
}
But this gives me an error on OnItemClickListener():
The method OnItemClickListener() is undefined for the type AdapterView.
So my qyestion is, how can i set an OnItemClickListener on my ListView??
check this code
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
// When clicked, show a toast with the TextView text
Toast.makeText(getApplicationContext(),
((TextView) view).getText(), Toast.LENGTH_SHORT).show();
}
});
make sure you have imported correct packages:
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
try this
list.setOnItemClickListener(new AdapterView.OnItemClickListener()) {
// ...
}
You should implement a customAdapter for having more control on your listView, Here is the link after visiting this you should be able to do what is required. Or you can have this code to quickly do what you need.
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View view, int arg2,long itemID) {
}
});
Try this:
list.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> a, View v, int position, long id) {
// ...
}
});
try this one
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
}
});
I know there is a lot of information out there about using the onItemClickListener and list view but I am new to android development and cannot seem to get it working.
I am not quite sure where I should add the listener so I would really appreciate some help and guidance.
I have two files, the main activity:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.menu);
ArrayList<GroceryList> menuitems = getItems();
ListView listView = (ListView) findViewById(R.id.Menu);
listView.setAdapter(new GroceryListAdapter(this, R.layout.categorymenu, menuitems));
}
and the ListAdapter File:
public class GroceryListAdapter extends ArrayAdapter<GroceryList> {
private ArrayList<GroceryList> grocerylists;
private Activity activity;
public ImageManager imageManager;
public GroceryListAdapter(Activity a, int textViewResourceId, ArrayList<GroceryList> grocerylists) {
super(a, textViewResourceId, grocerylists);
this.grocerylists = grocerylists;
activity = a;
}
public static class ViewHolder{
public TextView name;
public TextView message;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
ViewHolder holder;
if (v == null) {
LayoutInflater vi =
(LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.categorymenu, null);
holder = new ViewHolder();
holder.name = (TextView) v.findViewById(R.id.categoryname);
holder.message = (TextView) v.findViewById(R.id.message);
v.setTag(holder);
}
else
holder=(ViewHolder)v.getTag();
final GroceryList grocerylist = grocerylists.get(position);
if (alcohollist != null) {
holder.name.setText(grocerylist.name);
holder.message.setText(grocerylist.message);
}
return v;
}
I am sorry if I am asking a question that has already been answered but I spent a lot of time trying to figure it out for myself but with no success.
I hope some one with more experience than myself will be able to tell me where and how I should add the onItemClickListen method.
Thanks!
By the looks of it you are using a regular Activity, so you should add this:
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getApplicationContext(), "You clicked item at position"+position,
Toast.LENGTH_SHORT).show();
}
});
BEFORE the .setAdapter in your main activity. That should work.
In your Main activity:
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// When clicked, show a toast with the TextView text
Toast.makeText(getApplicationContext(), ((TextView) view).getText(),
Toast.LENGTH_SHORT).show();
}
});
#Override
protected void onListItemClick(ListView l, View v, int position, long id){
// startActivity( new Intent());
Intent i = new Intent(this,"Next_Activity_Name".class);
i.putExtra("selected",(int)selected_position);
final int resultCode = 2;
startActivityForResult(i,resultCode);
}
You have to add this piece of code into ur main activity after the onCreate() method..
Write down the following code after setting the adapter.
listView.setOnItemClickListener(new OnItemClickListener(){
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
}});
Handle the functionality that you want to do on click, under the onItemClick method.
you have use custom adapter so you can set touchListner at perticular widget which you define in categorymenu layout. it is quit easy to do this but
if you use simple listview then
Add parameter in listview :
lview.setOnItemClickListener(this);
public void onListItemClick(ListView parent, View v, int position, long id) {
// do with list-view item Position
}
For more ....
Simple Listview
http://www.androidpeople.com/android-listview-onclick
http://developer.android.com/resources/tutorials/views/hello-listview.html
http://mobile.tutsplus.com/tutorials/android/android-listview/
Paresh Mayani
Custome Listview
http://saigeethamn.blogspot.com/2010/04/custom-listview-android-developer.html
how to customize listview row android
http://www.josecgomez.com/2010/05/03/android-putting-custom-objects-in-listview/
Here so many answers, But it seem to be you didn't understood what they are telling. I will also try to give answer.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.menu);
ArrayList<GroceryList> menuitems = getItems();
ListView listView = (ListView) findViewById(R.id.Menu);
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
// do whatever you want on clicking any list itm
}
});
listView.setAdapter(new GroceryListAdapter(this, R.layout.categorymenu, menuitems));
}
I hope you will understand it.