I am a beginner and trying to do my first android app.
I have the following requirement.
When the user clicks on the item in the list, we should be able to navigate to a new page based on the item clicked.
Right now I have a sample code wherin I am able to navigate to only one page irrespective of the item clicked.
For exmple, When I click on football in list I should be able to navigate to a new page called by football class and when I click on basketball I should be able to navigate to a new page called by basketball class.
Also I want to add an actionbar to this listview.
I am able to create an actionbar with home, logout and settings option separately . But I am not able to add it to this list view.
Please suggest regarding how to go about this
Below is my code:
public class ImageTextListViewActivity extends Activity implements
OnItemClickListener {
public static final String[] titles = new String[] { "Football",
"Basketball" };
public static final Integer[] images = { R.drawable.football,
R.drawable.basketball };
ListView listView;
List<RowItem> rowItems;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
rowItems = new ArrayList<RowItem>();
for (int i = 0; i < titles.length; i++) {
RowItem item = new RowItem(images[i], titles[i]);
rowItems.add(item);
}
listView = (ListView) findViewById(R.id.list);
CustomListViewAdapter adapter = new CustomListViewAdapter(this,
R.layout.list_item, rowItems);
listView.setAdapter(adapter);
listView.setOnItemClickListener(this);
}
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
//Navigation to next page
Intent myIntent = new Intent(view.getContext(), Football.class);
myIntent.putExtra("Game",listView.getItemAtPosition(position).toString());
startActivityForResult(myIntent, 0);
//Navigation ends
}
}
Why don't you use the position param of onItemClick method?
Related
I have my ListView set up and populated with an arraylist of strings, my question is how can I get another listview to dropdown when the "Liquid Type" in the ListView is clicked by the user
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView mainList = (ListView) findViewById(R.id.add_drink);
ListView drinkType = (ListView) findViewById(R.id.drink_types);
ArrayList<String> mainListArray = new ArrayList<String>();
mainListArray.add("Liquid Type");
mainListArray.add("How Many?");
mainListArray.add("Amount");
mainListArray.add("Extra Calories");
ArrayAdapter<String> ad = new ArrayAdapter<String>(this, R.layout.list_adapter,R.id.text, mainListArray);
mainList.setAdapter(ad);
mainList.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
// argument position gives the index of item which is clicked
public void onItemClick(AdapterView<?> arg0, View v,int position, long arg3)
{
}
});
}
Have you looked at ExpandableListView
http://developer.android.com/reference/android/widget/ExpandableListView.html
What are you trying to do (use case)? The names you have in the list of main items kind of looks like an input form.
Maybe touching a single list item could display a context menu - see Using contextmenu with listview in android
I want to implement an ActionBar in a listview.
As given in developer.android.com I tried using extends ActionBar activity in my code.
My code runs fine when I give extends Activity, But I won't get an ActionBar.
The code crashes when I use extends ActionBarActivity with java.lang.exceptionininitializererror.
I have imported support library. The screenshot is attached.
The imported libraries are android-support-v4.jar
android-support-v7-appcompat.jar.
Below is my code.
Please guide me on this.
Also please let me know if I can do it using any other way.
public class ImageTextListViewActivity extends Activity implements OnItemClickListener {
public static final String[] titles = new String[] { "Football",
"Basketball" };
public static final Integer[] images = { R.drawable.football,
R.drawable.basketball };
ListView listView;
List<RowItem> rowItems;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//ActionBar actionBar = getSupportActionBar();
// actionBar.setDisplayShowTitleEnabled(false);
//android.graphics.drawable.ColorDrawable.setColor(0xff9ACC00);
//actionBar.setBackgroundDrawable(colorDrawable);
//actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
rowItems = new ArrayList<RowItem>();
for (int i = 0; i < titles.length; i++) {
RowItem item = new RowItem(images[i], titles[i]);
rowItems.add(item);
}
listView = (ListView) findViewById(R.id.list);
CustomListViewAdapter adapter = new CustomListViewAdapter(this,
R.layout.list_item, rowItems);
listView.setAdapter(adapter);
listView.setOnItemClickListener(this);
}
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
//Navigation to next page
Intent myIntent = new Intent(view.getContext(), Football.class);
myIntent.putExtra("Game",listView.getItemAtPosition(position).toString());
startActivityForResult(myIntent, 0);
//Navigation ends
}
}
You have to add appcompat library as a library project not just import the jar.
Import appcompat into your IDE from android_sdk_dir/extras/android/support/v7/appcompat.
Add it to your project as a library project. (Project -> Properties -> Android -> Library -> Add.. and select appcompat).
Don't forget to change the application theme to Theme.AppCompat.
In my activity i have created seven listviews and am using viewpager to swipe between them in the same activity. I then have a sqlite database populating each listview. My problem is the onitemclicklistener is not working, there are no errors and the code executes fine but nothing happens on list item clicks. I tested it out by adding toast display messages etc but nothing happens on list item clicks.
I suspect the problem is i have not gotten the listviews from xml layouts like the conventional method so the itemclicklistener method is slightly different, however i don't know what method to use when utilising listviews this way and no other OS threads that i've seen creates listviews this way.
If anyone could please shed some light i'd be very grateful. Thanks in advanced.
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mContext = this;
setContentView(R.layout.activity_schedule);
ListView listview1 = new ListView(mContext);
ListView listview2 = new ListView(mContext);
ListView listview3 = new ListView(mContext);
ListView listview4 = new ListView(mContext);
ListView listview5 = new ListView(mContext);
ListView listview6 = new ListView(mContext);
ListView listview7 = new ListView(mContext);
Vector<View> pages = new Vector<View>();
pages.add(listview1);
pages.add(listview2);
pages.add(listview3);
pages.add(listview4);
pages.add(listview5);
pages.add(listview6);
pages.add(listview7);
ViewPager vp = (ViewPager) findViewById(R.id.viewpager);
PageAdapter adapter = new PageAdapter(mContext,pages);
vp.setAdapter(adapter);
db.open();
scheduleAdapter = new ScheduleAdapter(ScheduleActivity.this, db.getMonday(),CursorAdapter.NO_SELECTION );
listview1.setAdapter(scheduleAdapter);
db.close();
db.open();
scheduleAdapter = new ScheduleAdapter(ScheduleActivity.this, db.getTuesday(),CursorAdapter.NO_SELECTION );
listview2.setAdapter(scheduleAdapter);
db.close();
db.open();
scheduleAdapter = new ScheduleAdapter(ScheduleActivity.this, db.getWednesday(),CursorAdapter.NO_SELECTION );
listview3.setAdapter(scheduleAdapter);
db.close();
db.open();
scheduleAdapter = new ScheduleAdapter(ScheduleActivity.this, db.getThursday(),CursorAdapter.NO_SELECTION );
listview4.setAdapter(scheduleAdapter);
db.close();
db.open();
scheduleAdapter = new ScheduleAdapter(ScheduleActivity.this, db.getFriday(),CursorAdapter.NO_SELECTION );
listview5.setAdapter(scheduleAdapter);
db.close();
db.open();
scheduleAdapter = new ScheduleAdapter(ScheduleActivity.this, db.getSaturday(),CursorAdapter.NO_SELECTION );
listview6.setAdapter(scheduleAdapter);
db.close();
db.open();
scheduleAdapter = new ScheduleAdapter(ScheduleActivity.this, db.getSunday(),CursorAdapter.NO_SELECTION );
listview7.setAdapter(scheduleAdapter);
db.close();
listview1.setOnItemClickListener(new ListView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
}
});
From your describe I understand that the lists are showed up when you start the application.
The position parameter, holds where the user clicked on the (screen) - list.
Did you try to use it with switch case?
I mean like this:
list.setOnItemClickListener(new OnItemClickListener(){
public void onItemClick( AdapterView <?> parent, View view, int
position,long id){
switch(position){
case 0:
// write what you need here when the user clicks on the first list item
break;
case 1:
// write what you need here when the user clicks on the 2nd list item
break;
}
}
};
hope this will help you
I'm not quite sure why the itemClicklistener isn't being called when you press something in listview1, but I don't think that's the biggest problem.
You're off on a terrible start with adding 7 listviews in 1 activity, and with opening and closing your database 7 times straight after each other.
I'd suggest you start out with the app Android UI Patterns: https://play.google.com/store/apps/details?id=com.groidify.uipatterns
There you will find a page full of viewpagers and tabs examples. I'd suggest you take the Jake Wharton one.
You want to be doing this with Fragments.
In your ListFragment you should extends ListFragment then use onListItemClick, like this:
public class ArrayListFragment extends ListFragment {
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
setListAdapter(new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_list_item_1, Listnames.TITLES));
}
#Override
public void onListItemClick(ListView l, View v, int position, long id) {
Log.i("FragmentList2", "Item clicked: " + id);
String item = (String) getListAdapter().getItem(position);
Intent intent = new Intent(getActivity(), SearchableActivity.class);
intent.putExtra("item", item);
Toast.makeText(getActivity(), item, Toast.LENGTH_LONG).show();
// startActivity(intent);
}
}
I have the following in one of my tabs:
ListView list=(ListView)findViewById(R.id.countries);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, COUNTRIES);
list.setAdapter(adapter);
static final String[] COUNTRIES = new String[] {
"List Item 1", "List Item 2", "List Item 3" };
This list is within a tab. I want to change it so when one of the items is clicked (say we call it London Big Ben, I want to somehow attach co-ordinates to that) it diverts to Google Maps either via WebView (easiest) or MapView appears over the tab (but the tab bar is still visible).
Can anyone provide links to tutorials or assistance?
This is the code where i am put my data inside arraylist which i am fetching from Web Service.
public static ArrayList<HashMap<String, String>> mylist;
for (int i = 0; i < nodes.getLength(); i++) {
HashMap<String, String> map = new HashMap<String, String>();
map.put("Name",name);
map.put("Vicinity", vicinity);
map.put("Latitude", lat);
map.put("Longitude", lng);
mylist.add(map);
}
After this below is the code of ListView Activity class. Where i have implemented on click listner and on click of listview calling another activity and passing the position of the list which is clicked.
ListViewActivity Class code
public class ListViewActivity extends ListActivity implements OnItemClickListener{
private ListView lv;
#Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.listingatms);
ListAdapter adapter = new SimpleAdapter(this, mylist , R.layout.listdata,
new String[] {"Name", "Vicinity"},
new int[] { R.id.item_title, R.id.item_subtitle});
setListAdapter(adapter);
lv = getListView();
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(this);
}
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long id) {
// TODO Auto-generated method stub
Intent showMapMenusIntent = new Intent(ListViewActivity.this, MapMenus.class);
showMapMenusIntent.putExtra("Position", position);
startActivity(showMapMenusIntent);
}
If you want to show the map in the same activity then use the postion variable and on the basis of that you can get all the values from mylist(ArrayList).
For e.g position = mylist.get(position).get("Name"));
This way you will be able to get all the details from ArrayList and use it as per your requirement.
Hope this will help you...
I currently have a custom listview where each item on the list contains two rows of text. What I would like to do is each time a user clicks on the button, it creates a new item on the list with the text that the user inputted. While I know how to get the text, I am having trouble adding a new item to the list view as I simply don't know where to begin.
Here is my code:
public class MainFeedActivity extends ListActivity implements OnClickListener {
View sendButton;
EditText postTextField;
TextView currentTimeTextView, mainPostTextView;
ListView feedListView;
String[] test;
ArrayList<HashMap<String, String>> list;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_feed);
//create button and implement on click listener
sendButton = this.findViewById(R.id.sendPostButton);
sendButton.setOnClickListener(this);
list = new ArrayList<HashMap<String, String>>();
//create the adapter for the list view
SimpleAdapter adapter = new SimpleAdapter(
this,
list,
R.layout.post_layout,
new String[]{"time", "post"},
new int[]{R.id.postTimeTextView, R.id.postTextView});
//fill the list view with something - TEST
fillData();
//set list adapter
setListAdapter(adapter);
}
public void fillData() {
//long timeTest = System.currentTimeMillis();
HashMap<String, String> temp = new HashMap<String, String>();
temp.put("time", "current time");
temp.put("post", "USER TEXT GOES HERE");
list.add(temp);
}
#Override
public void onClick(View button) {
switch (button.getId()) {
case R.id.sendPostButton:
break;
}
}
}
It's as simple as adding a new element to your ArrayList (like you do in fillData), then calling adapter.notifyDataSetChanged().
After calling fillData(), just make a call on adapter.notifyDataSetChanged().
NotifyDataSetChanged() - Notifies the attached View that the underlying data has been changed and it should refresh itself.