So, so far i built a list of json object like this
public class list extends ListActivity{
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list);
Intent i = getIntent();
String snopel = i.getStringExtra("nopel");
String snama = i.getStringExtra("nama");
String salamat = i.getStringExtra("alamat");
String sgolongan = i.getStringExtra("golongan");
TextView tx_nopel = (TextView)findViewById(R.id.l_nopel);
TextView tx_nama= (TextView)findViewById(R.id.l_nama);
TextView tx_alamat = (TextView)findViewById(R.id.l_alamat);
TextView tx_golongan = (TextView)findViewById(R.id.l_golongan);
tx_nopel.setText(snopel);
tx_nama.setText(snama);
tx_alamat.setText(salamat);
tx_golongan.setText(sgolongan);
List<NameValuePair> pairs = new ArrayList<NameValuePair>();
pairs.add(new BasicNameValuePair("nopel", snopel));
ArrayList<HashMap<String, String>> lr = new ArrayList<HashMap<String, String>>();
JSON json_lr = new JSON();
JSONObject jobj_lr = json_lr.getJSON("http://10.0.2.2/KP/pdam/listtagihan.php", pairs);
try {
int length = jobj_lr.getInt("panjang");
for(int n = 1; n <= length; n++){
String m = Integer.toString(n);
JSONObject row = jobj_lr.getJSONObject(m);
String snomor = row.getString("nomor");
String sbulan = row.getString("bulan");
String stahun = row.getString("tahun");
String stagihan = "Rp. " + row.getString("tagihan");
HashMap<String, String> rek = new HashMap<String, String>();
rek.put("nomor", snomor);
rek.put("bulan", sbulan);
rek.put("tahun", stahun);
rek.put("tagihan", stagihan);
lr.add(rek);
}
} catch (JSONException e) {
e.printStackTrace();
}
ListAdapter adapter_lr = new SimpleAdapter(this, lr, R.layout.list_data,
new String[]{"nomor","bulan","tahun","tagihan"},
new int[]{R.id.textView1, R.id.textView2, R.id.textView3, R.id.textView4});
setListAdapter(adapter_lr);
ListView lv_lr = getListView();
lv_lr.setOnItemClickListener(new OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
// TODO Auto-generated method stub
Intent i = new Intent(list.this, rincian.class);
i.putExtra("nomor", ((TextView)view.findViewById(R.id.textView1)).getText().toString());
startActivity(i);
}
});
}
}
that will show one listview in one listactivity, but I wonder if I can make 2 custom listviews in 1 list activity, but I can't figure out how to
I think that it's impossible because in listactivity we must set the adapter that can just choose 1 list adapter like this setListAdapter(adapter_lr);
but I want to know for sure is it true?
Thanks in advance.
why you need two listview in one Activity ?
if you want two listview then you can extends Activity and add two listview in layout file.
Now,
ListView listView1=(ListView)findViewById(R.id.listview1);
ListView listView2=(ListView)findViewById(R.id.listview2);
You can create two custom list view in one list activity by declaring them in xml file
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="Value in dps"
></ListView>
<ListView
android:id="#+id/list1"
android:layout_width="fill_parent"
android:layout_height="Value in dps"
></ListView>
one list id must be #android:id/list other can be anything of yor choice and you can set adapters as you want in code.
You need to extend Activity class in YourClass, and you can have as many List view from layout.
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'm trying to add items to an arraylist using this class template:
public class Template {
public String username;
public String email;
}
Here's the whole code:
public void JsonToArrayList(JSONArray myJsonArray) throws JSONException
{
ArrayList<Template> listItems = new ArrayList<Template>();
JSONObject jo = new JSONObject();
Template tem = new Template();
ListView lv = (ListView) findViewById(R.id.listView1);
for(int i = 0; i<myJsonArray.length(); i++)
{
jo = myJsonArray.getJSONObject(i);
tem.username = jo.getString("username");
tem.email = jo.getString("user_email");
listItems.add(tem);
Log.e("Ninja Archives", tem.username);
}
// This is the array adapter, it takes the context of the activity as a first // parameter, the type of list view as a second parameter and your array as a third parameter
ArrayAdapter<Template> arrayAdapter = new ArrayAdapter<Template>(this,android.R.layout.simple_list_item_1, listItems);
lv.setAdapter(arrayAdapter);
}
The problem is, instead of filling my listview with nice username and email strings, it's filling up with items like this:
com.android.ninjaarchives.
Template#40585690
I think somewhere along the line I have become lost, but I've been trying all sorts for ages now and getting nowhere. Can anyone point me in the right direction?
Thanks for any help.
Note: not really sure what's going on with the code; it doesn't appear to be pasting correctly.
Use below code, it can be a solution for you
public void JsonToArrayList(JSONArray myJsonArray) throws JSONException
{
ArrayList<Template> listItems = new ArrayList<Template>();
JSONObject jo = new JSONObject();
Template tem = new Template();
ListView lv = (ListView) findViewById(R.id.listView1);
String listItemString[] = new String[myJsonArray.length];
for(int i = 0; i<myJsonArray.length(); i++)
{
jo = myJsonArray.getJSONObject(i);
tem.username = jo.getString("username");
tem.email = jo.getString("user_email");
listItemString[i] = tem.username +" - " + tem.email; // u can change it according to ur need.
listItems.add(tem);
Log.e("Ninja Archives", tem.username);
}
// This is the array adapter, it takes the context of the activity as a first // parameter, the type of list view as a second parameter and your array as a third parameter
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, listItemString);
lv.setAdapter(arrayAdapter);
}
But better to write Custom adapter by extending BaseAdapter, and do listItem handling in getView method here is one simple tutorial
Take a class extending Base
private class CustomAdapter extends BaseAdapter
{
LayoutInflater inflater;
public CustomAdapter(Context context)
{
inflater = LayoutInflater.from(context);
}
public int getCount()
{
return listItems.size();
}
public Object getItem(int position)
{
return listItems.get(position);
}
public long getItemId(int position)
{
return position;
}
public View getView(final int position, View convertView,ViewGroup parent)
{
//if(convertView==null)
//convertView = inflater.inflate(R.layout.listlayout, parent, false);
Template data = (Template) getItem(position);
TextView v=new TextView(context);
v.setText(data.name);
return v;
}
}
and set adapter to your listview
lv.setAdapter(new CustomAdapter(this));
In this case you have to use a custom adapter (that extends from ArrayAdapter) and override the getView method to display in a custom layout the username and the email.
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);
}
}
I'm new to android, i've spent the last 2 days trying previous examples and online solutions but I just can't seem to get my head around it :(
I'm able to display a list view, parse some json from online and store a book title, book description and book ID and display this data in the listview. I want to be able to put a 'download' button in each row for the ListView, each button will correspond to its book ID on Click() and the action listener will download the book by appending that ID to a url.
e.g www.books.com/download_book1 or /download_book2....
Here is my code. Catalogue.java class
public class Catalogue extends ListActivity {
private JSONObject json;
private ListView lv;
private ArrayList<Integer> alKey = new ArrayList<Integer>();
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); //icicle
setContentView(R.layout.shelflist);
ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
....
try{
JSONArray entries = json.getJSONArray("entries");
for(int i=0;i<entries.length();i++){
HashMap<String, String> map = new HashMap<String, String>();
JSONObject e = entries.getJSONObject(i);
alKey.add(e.getInt("key"));
map.put("id", String.valueOf(i));
map.put("title", "Title:" + e.getString("title"));
map.put("description", "Description: " + e.getString("description"));
mylist.add(map);
}
}catch(JSONException e) {
Log.e("log_tag", "Error parsing data "+e.toString());
}
ListAdapter adapter = new SimpleAdapter(this, mylist , R.layout.shelfrow,
new String[] { "title", "description" },
new int[] { R.id.item_title, R.id.item_subtitle });
setListAdapter(adapter);
lv = getListView();
lv.setTextFilterEnabled(true);
.....
This is as far as I get. I don't know how to add 1 button per row in the List and assign an action listener to each button.
I also have a shelfrow.xml file (textView, textView for item_title and item_subtitle) and a shelflist.xml file (ListView).
I have a shelf.xml file with
Basically you need to learn the concept of ListAdapter.
Here's the short story: picture an object that holds the data to be displayed inside a list, along with the way to display each line individually. That's your ListAdapter. Now take each individual line: it's a book with a title and an OnClickListener. It's rendered inside a View with a TextView (for the title) and a Button (for the OnClickListener). All you need to do is give one View to the adapter that will be used for each line, and a List of the books you want to be inside the list.
Here's some sample code. I hope it clears things up a bit
private class MyItemModel{ //that's our book
String title; // the book's title
String description;
long id;
OnClickListener listener = new OnClickListener(){ // the book's action
#Override
public void onClick(View v) {
// the default action for all lines
doSomethingWithTheBookTitleOrUniqueId(this);
}
};
}
private class MyListAdapter extends BaseAdapter{
View renderer;
List<MyItemModel> items;
// call this one and pass it layoutInflater.inflate(R.layout.my_list_item)
public MyListAdapter(View renderer) {
this.renderer = renderer;
}
// whenever you need to set the list of items just use this method.
// call it when you have the data ready and want to display it
public void setModel(List<MyItemModel> items){
this.items = items;
notifyDataSetChanged();
}
#Override
public int getCount() {
return items!=null?items.size():0;
}
#Override
public Object getItem(int position) {
return items!=null?items.get(position):null;
}
#Override
public long getItemId(int position) {
return items!=null?items.get(position).id:-1;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView==null){
convertView = renderer;
}
MyItemModel item = items.get(position);
// replace those R.ids by the ones inside your custom list_item layout.
TextView label = (TextView)convertView.findViewById(R.id.item_title);
label.setText(item.label);
Button button = (Button)convertView.findViewById(R.id.item_button);
button.setOnClickListener(item.listener);
return convertView;
}
}
In order to pass the List, instead of putting the data inside your list of hashmaps you can do this for instance (be careful, I also updated the MyItemModel and MyListAdapter to your need, added the id and description properties):
List<MyItemModel> myListModel = new ArrayList<MyItemModel>();
try{
JSONArray entries = json.getJSONArray("entries");
for(int i=0;i<entries.length();i++){
MyItemModel item = new MyItemModel();
JSONObject e = entries.getJSONObject(i);
alKey.add(e.getInt("key"));
item.id = i;
item.title = e.getString("title");
item.description = e.getString("description");
// you can change the button action at this point:
// item.onClickListener = new OnClickListener(){...};
myListModel.add(item);
}
}catch(JSONException e) {
Log.e("log_tag", "Error parsing data "+e.toString());
}
ListAdapter adapter = new MyListAdapter(getLayoutInflater().inflate(R.layout.shelfrow, this));
adapter.setModel(myListModel);
setListAdapter(adapter);
lv = getListView();
lv.setTextFilterEnabled(true);
You can create your own class extending ArrayAdapter that will hold your list and set onClickListener to the Button in each row.
But in getView method of your ArrayAdapter you have to create a new view every time.
for example - row layout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="110dp"
android:background="#FFF"
android:layout_width="fill_parent">
<LinearLayout
android:layout_width="fill_parent"
android:background="#FFF"
android:orientation="vertical"
android:padding="2dp"
android:layout_height="110dp">
<TextView android:id="#+id/list_item_title"
android:background="#FFF"
android:layout_width="fill_parent"
android:layout_height="40dp"/>
<Button android:id="#+id/download_button"
android:gravity="center"
android:text="Download"
android:layout_height="35dp"/>
</LinearLayout>
</RelativeLayout>
and getView method in ArrayAdapter
private List<Map<String, String>> jsonMapList;
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflater.inflate(R.layout.list_item, null);
// here you set textview values (title and description)
// TextView title = (TextView) v.findViewById(R.id.list_item_title);
// title.setText('bla');
// and set OnClickListener
Button button = (Button) v.findViewById(R.id.download_button);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
downloadFile(getUrl(position));
}
});
return v;
}
// method that downloads file
private void downloadFile(String url) {}
// get url from your list by index
private String getUrl(int index) {
return jsonMapList.get(index).get("url");
}
Usage of Map is unnecessary, you could use any object you prefer.
In activity class
CustomAdapter listAdapter = new CustomAdapter(this, android.R.layout.simple_list_item_single_choice, jsonMapList);
setListAdapter(listAdapter);
I had done xml parsing and able to display one array item in list view .but i want to display 2 array item in one list...using array adapter..means i want array_barrio and array_ciudad both in one list view how can i do this...my java class is
public class XMLParsingExample extends ListActivity {
String name = null;
private String array_estado[];
private String[] array_ciudad;
private String array_barrio[];
/** Create Object For SiteList Class */
SitesList sitesList = null;
/** Called when the activity is first created. */
#SuppressWarnings("unchecked")
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listplaceholder);
/** Create a new layout to display the view */
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(1);
try {
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();
XMLReader xr = sp.getXMLReader();
/** Send URL to parse XML Tags */
URL sourceUrl = new URL(
"http://www.arteonline.mobi/iphone/output.php?key=Buenos");
MyXMLHandler myXMLHandler = new MyXMLHandler();
xr.setContentHandler(myXMLHandler);
xr.parse(new InputSource(sourceUrl.openStream()));
} catch (Exception e) {
System.out.println("XML Pasing Excpetion = " + e);
}
sitesList = MyXMLHandler.sitesList;
array_ciudad = new String[sitesList.getEstado().size()];
for (int i = 0; i < sitesList.getEstado().size(); i++)
{
name = sitesList.getEstado().get(i);
array_ciudad[i] = name;
Log.i("array_spinner" + i, array_ciudad[i]);
}
array_barrio = new String[sitesList.getBarrio().size()];
for (int i = 0; i < sitesList.getBarrio().size(); i++)
{
name = sitesList.getBarrio().get(i);
array_barrio[i] = name;
// Log.i("array_spinner" + i, array_ciudad[i]);
}
setListAdapter(new ArrayAdapter(this,
android.R.layout.simple_list_item_1, array_barrio));
// setListAdapter(new ArrayAdapter(this,
// android.R.layout.simple_list_item_1, array_barrio ));
}
You can make a new String[] that is big enough to hold both, and then copy the elements of each array to the new array. Use the new array in your ArrayAdapter.
String[] bigArray = new String[array_barrio.length + array_ciudad.length];
System.arraycopy(array_barrio, 0, bigArray, 0, array_barrio.length);
System.arraycopy(array_ciudad, array_barrio.length, bigArray, array_barrio.length, array_ciudad.length);
Another thing worth considering would be to just create the bigArray initially, and just add both sets of elements to the big array rather than the smaller ones.
Do you want to combine two array items and make it as one. then you can follow nicholas way . Add both the array and make it as one array and pass the array to ArrayAdapter. But if you want to display data of both arrays in the same list then you have to use BaseAdapter and write your own logic for that.
Thanks
Deepak
If you want to show more than one item in a list view item you can use your own Adapter by extending BaseAdapter. But first of array_ciudad and array_barrio would need to be the same size.
Each Item will need to be represented by a list item layout like the one below. This layout would need to be placed in the res/layout directory.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- Location where barrio items will be displayed -->
<TextView android:id="#+id/barrio"
android:layout_height="wrap_content"
android:layout_width="wrap_content" />
<!-- Location where ciudad items will be displayed -->
<TextView android:id="#+id/ciudad"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
This example custom adapter assumes that both array_barrio and array_ciudad will be the same length. All you really need to do is create the view for the list items by inflating the layout and inserting your Strings into the TextViews where needed. In this example MyAdapter should be an inner class of XMLParsingExample giving it direct access to the array properties you defined.
protected class MyAdapter extends BaseAdapter {
private LayoutInflater inflater;
public MyAdapter(Context ctx) {
super();
this.inflater = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
return XMLParsingExample.this.array_barrio.length;
}
/* Not implemented but not really needed */
#Override
public Object getItem(int position) {
return null;
}
/* Not implemented but not really needed */
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = inflater.inflate(R.layout.listitem_layout, parent, false);
Course c = courses.get(position);
String barrio = XMLParsingExample.this.array_barrio[position];
String ciudad = XMLParsingExample.this.array_ciudad[position];
TextView tv = (TextView) v.findViewById(R.id.barrio);
tv.setText(barrio);
tv = (TextView) v.findViewById(R.id.ciudad);
tv.setText(ciudad);
return v;
}
}