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...
Related
I am using onItemClickListener() with a List. I want to pass name of the item clicked on, and an arbitrary number to next instance of the list. How can this be done?
Edit: here is the class:
public class ListViewA extends Activity{
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ListView lv= (ListView)findViewById(R.id.listview);
// create the grid item mapping
String[] from = new String[] {"col_1"};
int[] to = new int[] {R.id.item2};
// prepare the list of all records
List<HashMap<String, String>> fillMaps = new ArrayList<HashMap<String, String>>();
for(int i = 0; i < 10; i++){
HashMap<String, String> map = new HashMap<String, String>();
map.put("col_1", "col_1_item_" + i);
fillMaps.add(map);
lv.setOnItemClickListener(onListClick);
}
// fill in the grid_item layout
SimpleAdapter adapter = new SimpleAdapter(this, fillMaps, R.layout.grid_item, from, to);
lv.setAdapter(adapter);
}
private AdapterView.OnItemClickListener onListClick=new AdapterView.OnItemClickListener(){
public void onItemClick(AdapterView<?> parent,View view, int position, long id)
{
Intent i=new Intent(ListViewA.this,ListViewA.class);
startActivity(i);
}
};
}
Following example shows you how to pass data onItemClick for particular position.
ListView lv = getListView();
// listening to single list item on click
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// selected item
HERE YOU GOT POSITION FOR PERTICULAR ITEM
// Launching new Activity on selecting single List Item
Intent i = new Intent(getApplicationContext(), SingleListItem.class);
// sending data to new activity
i.putExtra("position", fillMaps.get(position));
startActivity(i);
}
});
And yes you need to declare fillMaps as globally.
public class ListViewA extends Activity{
List<HashMap<String, String>> fillMaps = new ArrayList<HashMap<String, String>>();
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);......
Intent i=new Intent(ListViewA.this,ListViewA.class);
i.putExtra("Key", fillMaps.get(position));
startActivity(i);
You can use above code in your onItemClickListner.
In this method the third agrument is position. So you have to use this eg. The Arraylist you are passing to your listView contains the data shown on listView. so use this kind of code
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
// TODO Auto-generated method stub
String data= mArrlist.get(position);
}
If you are using the arralist of String type otherwise If you are using arraylist of getter setter class then use
mArralist.get(potion).getName();
You should get item #paticular position
HashMap<String, String> item = adapterview.getAdapter().getItem(position);
check your condition according to requirement and by getting data from item and process further.
Intent i=new Intent(ListViewA.this,ListViewA.class);
i.putExtra("item", item);
startActivity(i);
For adding arbitrary number you can use the Random class of java as below
Random mRandom = new Random();
int random = mRandom.nextInt(mMoveToList.size());
random variable of int type return every time random value.
you can use getItem(int position) of SimpleAdapter
put your SimpleAdapter variable adapter as global
in your ClickListener call adapter.getItem(position)
I am having an issue with trying to retrieve the contents of the view that the the user has clicked within a ListView. My ListView is setup using a SimpleAdapter and is comprised of a "heading" and a "sub-title" for each of the items in the list. These are stored using a HashMap.
Within the activity also, is a spinner, when the user selects an item in the spinner, the ListView is updated. This is working fine and I just thought it necessary to mention what is happening within the activity as well.
What I am trying to do, is retrieve which item the user has selected so I can guide them to a new activity based on their selection (right now just trying to display the view contents with Toast). I want to retrieve the contents of what is stored under the "engExp" key for the item.
Here is my code:
// HASHMAP FOR LISTVIEW
List<HashMap<String, String>> fillMaps = new ArrayList<HashMap<String, String>>();
for(int x = 0; x < expressionListForModule.getCount(); x++) {
HashMap<String, String> map = new HashMap<String, String>();
map.put("engExp", expressionListForModule.getString(0));
map.put("japDef", expressionListForModule.getString(1));
fillMaps.add(map);
expressionListForModule.moveToNext();
}
// SETUP LISTVIEW
SimpleAdapter adapter2 = new SimpleAdapter(this, fillMaps, android.R.layout.simple_list_item_2, new String[] {"engExp","japDef"}, new int[] {android.R.id.text1, android.R.id.text2});
ListView lv = (ListView) findViewById(R.id.expressionList);
lv.setAdapter(adapter2);
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getApplicationContext(), ((TextView) view).getText(), Toast.LENGTH_LONG).show();
}
});
Specifically, the item that is causing the application to crash is this:
((TextView) view).getText()
If you need to see any more of my code, please let me know.
Any help is appreciated, thanks in advance.
try
lv.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
TextView v = (TextView) view.findViewById(android.R.id.text1);
String contentForLink = v.getText().toString();
Toast.makeText(getApplicationContext(), contentForLink, Toast.LENGTH_LONG).show();
}
});
ListViews have always been my weak point and right now I am practicing putting a Listview, within a Listview. Anyway, I first call my ListView at the start of my program and it loads it with an array saved in my strings.xml:
String[] departments = getResources().getStringArray(
R.array.departments_array);
setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item,
departments));
setContentView(R.layout.main);
ListView lv = getListView();
lv.setTextFilterEnabled(true);
What I want to do is update this ListView with a new array of values each time a list item is clicked. The reason why I am trying to do it this way is because I plan on having 27 different arrays with different values for each position, and I feel it would be lighter on my resources if instead of making a ListView for each array of items, I would update this one ListView. I know I am probably not doing this the most efficient way, but if there is another way of implementing my idea please tell me.
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// When clicked, show a toast with the TextView text
switch (position) {
case 0:
try {
//It is here that i dont know what to do, I was going to call
//the Listview the same way i did previously using my setlistadapter,
//but i kept getting errors about the code being undefined
String[] listitems1 = getResources().getStringArray(
R.array.items_array);
} catch (ClassCastException e) {
Toast.makeText(getApplicationContext(), "Error",
Toast.LENGTH_SHORT).show();
}
break;
case 1:
try {
//The listview will be changed again here
} catch (ClassCastException e) {
Toast.makeText(getApplicationContext(), "Error",
Toast.LENGTH_SHORT).show();
}
break;
}
};
});
Have you thought of using a BaseAdapter and setting it as the list adapter
http://developer.android.com/reference/android/widget/BaseAdapter.html
Your approach is wrong( if I understand what are you doing). Instead of replacing the adapter of the ListView every time the user clicks(and simply setting a new adapter should work) a element in the initial list you should start a new activity passing the clicked position and in your new activity set the adapter on a ListView with the correct array based on that position.
A small example:
Main class:
/**
* The main class with the initial 27 items array.
*/
public class Class1 extends ListActivity {
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
// start the second activity that will show our array depending on the
// position clicked
Intent i = new Intent(this, Class2.class);
// put the position in the Intent so we can know in the other activity
// what array to load.
i.putExtra("pos", position);
startActivity(i);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// I just used a simple array of 2 items, you'll load your 27 items
// array
String[] items = { "1", "2" };
setListAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, items));
}
}
Secondary activity that will show the array based on the previously selected position:
public class Class2 extends ListActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// get the Intent that started the activity
Intent i = getIntent();
// find out what position did that other activity send to us.
int position = i.getIntExtra("pos", -1);
// load the ListView with an adapter based on the array that you
// want(according to that position)
if (position == 0) {
// the first element in the main list
String[] items = getResources().getStringArray(R.array.a1);
setListAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, items));
} else if (position == 1) {
// the second element in the main list
String[] items = getResources().getStringArray(R.array.a2);
setListAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, items));
} else {
// etc
}
}
}
Luksprog's answer is indeed correct, and it is very useful for lists many levels deep (you do not put limits, just keep spawning new activity instances with the proper list loaded)
BUT
If your list isn't more than 2 levels deep you can use ExpandableListActivity instead of ListActivity which is basically an enhanced version of the single-level list you're using which natively handle group collapsing/expanding and therefore you do not need the spawn of a new activity for each sublevel.
again note that this approach works only for lists which do not go deeper than 2 levels
ExpandableListActivity documentation
ExpandableListView documentation
ExpandableListAdapter documentation - you should be fine with the BaseExpandableListAdapter implementation
And here you have some nice example from Google itself:
public class ExpandableList3 extends ExpandableListActivity {
private static final String NAME = "NAME";
private static final String IS_EVEN = "IS_EVEN";
private ExpandableListAdapter mAdapter;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
List<Map<String, String>> groupData = new ArrayList<Map<String, String>>();
List<List<Map<String, String>>> childData = new ArrayList<List<Map<String, String>>>();
for (int i = 0; i < 20; i++) {
Map<String, String> curGroupMap = new HashMap<String, String>();
groupData.add(curGroupMap);
curGroupMap.put(NAME, "Group " + i);
curGroupMap.put(IS_EVEN, (i % 2 == 0) ? "This group is even" : "This group is odd");
//filling with dummy data...
List<Map<String, String>> children = new ArrayList<Map<String, String>>();
for (int j = 0; j < 15; j++) {
Map<String, String> curChildMap = new HashMap<String, String>();
children.add(curChildMap);
curChildMap.put(NAME, "Child " + j);
curChildMap.put(IS_EVEN, (j % 2 == 0) ? "This child is even" : "This child is odd");
}
childData.add(children);
}
// Set up our adapter
mAdapter = new SimpleExpandableListAdapter(
this,
groupData,
android.R.layout.simple_expandable_list_item_1,
new String[] { NAME, IS_EVEN },
new int[] { android.R.id.text1, android.R.id.text2 },
childData,
android.R.layout.simple_expandable_list_item_2,
new String[] { NAME, IS_EVEN },
new int[] { android.R.id.text1, android.R.id.text2 }
);
setListAdapter(mAdapter);
}
}
Please excuse me, I'm new:new at this. I use a simple_list_item_2 to display 11 items. These 11 items have been loaded by using HashMap and then SimpleAdapter. This works fine in displaying everything. The problem is that I cannot get setOnItemClickListener going. The code:
public class TwoLineActivity extends ListActivity
{
ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>(2);
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.scrolllist);
// enter valid data, these 2 are the same as the remaining 9
HashMap<String, String> maplist;
maplist = new HashMap<String, String>();
maplist.put("line1", "a11 data");
maplist.put("line2", "asd asd ad 1234569780");
list.add(maplist);
maplist = new HashMap<String, String>();
maplist.put("line1", "a12 data");
maplist.put("line2", "asd asd ad 1234569781");
list.add(maplist);
String[] from = { "line1", "line2" };
int[] to = { android.R.id.text1, android.R.id.text2 };
SimpleAdapter adapter = new SimpleAdapter(this, list, android.R.layout.simple_list_item_2, from, to);
setListAdapter(adapter);`
So up to here things are great, I get my list. Now I want to be able to select an item from the list, so I coded the next 2 lines
list.setAdapter(adapter);
list.setOnItemClickListener(new OnItemClickListener()
{ .....
I get the following errors
The method setAdapter(SimpleAdapter) is undefined for the type ArrayList<HashMap<String,String>>
and
The method setOnItemClickListener(new AdapterView.OnItemClickListener(){}) is undefined for the type ArrayList<HashMap<String,String>>
If your activity extends ListActivity, you should override
protected void onListItemClick(ListView l, View v, int position, long id) {
}
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.