Is this the right way to add 2 arrays into SimpleAdapter?
I use SimpleAdapter to display 2 strings, one on the left and the other on the right, into a ListView,
The strings are in 2 different arrays. and 1st from array A with 1st in array B are in the 1st line, and so on..
Here is a part of the code开发者_开发百科 I use:
String[] array= getResources().getStringArray(R.array.Names_List);
int lengthtmp= array.length;
for(int i=0;i<lengthtmp;i++)
{
counter++;
AddToList(array[i]);
}
adapter = new SimpleAdapter(this,list,R.layout.start_row,new String[] {"number","suraname"},new int[] {R.id.Start_Numbering,R.id.Start_Name});
private void AddToList(String name) {
HashMap<String,String> temp = new HashMap<String,String>();
temp.put("number", Integer.toString(SortingPictures[counter-1]));
temp.put("suraname", name);
list.add(temp);
}
I'm sure there is a better way to make what I want. What's the correct way?
No it is not a proper way to implementing this here i am given a link for you read id and get idea for implementing it.
http://www.heikkitoivonen.net/blog/2009/02/15/multicolumn-listview-in-android/
You can use System.arraycopy to copy the contents of
To add two array
int[] a = {1, 2};
int[] b = {3, 4};
int[] ab = new int[a.length + b.length];
System.arraycopy(a, 0, ab, 0, a.length);
System.arraycopy(b, 0, ab, a.length, b.length);
Two add muliple arrays
public static String[] join(String [] ... parms) {
// calculate size of target array
int size = 0;
for (String[] array : parms) {
size += array.length;
}
String[] result = new String[size];
int j = 0;
for (String[] array : parms) {
for (String s : array) {
result[j++] = s;
}
}
return result;
}
public static void main(String[] args) {
String a[] = { "1", "2", "3" };
String b[] = { "4", "5", "6" };
String c[] = { "7", "8", "9" };
String[] big = (String [])join(a,b,c);
System.out.println(java.util.Arrays.toString(big));
/*
* output :
* [1, 2, 3, 4, 5, 6, 7, 8, 9]
*/
}
精彩评论