【问题标题】:Is this the right way to add 2 arrays into SimpleAdapter?这是将 2 个数组添加到 SimpleAdapter 的正确方法吗?
【发布时间】:2011-07-01 12:49:24
【问题描述】:

我使用 SimpleAdapter 将 2 个字符串,一个在左侧,一个在右侧,显示到一个 ListView 中,

字符串位于 2 个不同的数组中。和数组 A 中的第一个,数组 B 中的第一个在第一行,依此类推..

这是我使用的部分代码:

    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);

}

我确信有更好的方法来制作我想要的东西。正确的方法是什么?

【问题讨论】:

  • 创建一个新的适配器。扩展 BaseAdapter 并创建一个构造函数,该构造函数接受两个数组,对 lsitview 的每一侧使用一个数组

标签: java android listview adapter simpleadapter


【解决方案1】:

不,这不是实现此功能的正确方法,我提供了一个链接供您阅读 id 并了解实现它的想法。

http://www.heikkitoivonen.net/blog/2009/02/15/multicolumn-listview-in-android/

【讨论】:

    【解决方案2】:

    可以使用 System.arraycopy 复制内容
    添加两个数组

    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);  
    

    两个相加的多个数组

    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]
         */
      }
    

    【讨论】:

    • 这个答案与我的问题无关,我的问题是关于将 2 个数组添加到哈希图中..
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-03-28
    • 1970-01-01
    • 2016-05-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多