【问题标题】:How to make ListView show all my images?如何让 ListView 显示我所有的图像?
【发布时间】:2014-04-27 17:47:57
【问题描述】:

我是开发新手,我开始制作一个显示 30 张图片列表的应用,但不知何故,它只显示了前 10 张图片。我真的不知道我做错了什么。如果您能帮助我,我将不胜感激。

这是我所拥有的一部分:

int[] vehs = new int[]{
    R.drawable.veh1,
    R.drawable.veh2,
    R.drawable.veh3,
    R.drawable.veh4,
    R.drawable.veh5,
    R.drawable.veh6,
    R.drawable.veh7,
    R.drawable.veh8,
    R.drawable.veh9,
    R.drawable.veh10,
    R.drawable.veh11,
    R.drawable.veh12,
    R.drawable.veh13,
    R.drawable.veh14,
    R.drawable.veh15,
    R.drawable.veh16,
    R.drawable.veh17,
    R.drawable.veh18,
    R.drawable.veh19,
    R.drawable.veh20,
    R.drawable.veh21,
    R.drawable.veh22,
    R.drawable.veh23,
    R.drawable.veh24,
    R.drawable.veh25,
    R.drawable.veh26,
    R.drawable.veh27,
    R.drawable.veh28,
    R.drawable.veh29,
    R.drawable.veh30
};


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    List<HashMap<String,String>> aList = new ArrayList<HashMap<String,String>>();

    for(int i=0;i<10;i++){
        HashMap<String, String> hm = new HashMap<String,String>();
        hm.put("veh", Integer.toString(vehs[i]) );
        aList.add(hm);
    }

    // Keys used in Hashmap
    String[] from = { "veh" };

    // Ids of views in listview_layout
    int[] to = { R.id.veh };

    // Instantiating an adapter to store each items
    // R.layout.listview_layout defines the layout of each item
    SimpleAdapter adapter = new SimpleAdapter(getBaseContext(), aList, R.layout.listview_layout, from, to);

    // Getting a reference to listview of activity_main.xml layout file
    ListView listView = ( ListView ) findViewById(R.id.listView1);

    // Setting the adapter to the listView
    listView.setAdapter(adapter);
}

它只显示从 veh1 到 veh10 :(

【问题讨论】:

  • for(int i=0;i&lt;10;i++){ 可能会更改顶部索引?
  • 创建一个图像数组,然后调用在循环中创建图像的函数
  • @nKn 例如,我已经尝试更改 i

标签: android eclipse image listview


【解决方案1】:

似乎可能无法将字符串映射到可绘制对象。

尝试将资源映射为 int 而不是字符串。

List<HashMap<String,Object>> aList = new ArrayList<HashMap<String,Object>>();

for(int i=0;i<10;i++){
    HashMap<String, Object> hm = new HashMap<String,Object>();
    hm.put("veh", Integer.valueOf(vehs[i]) );
    aList.add(hm);
}

【讨论】:

  • 我添加了这个,但它仍然显示前 10 张图片:/
【解决方案2】:

我对 SimpleAdapter 没有太多经验,我一直在创建自己的自定义适配器,主要扩展 BaseAdapter。如果我是你,我会创建自己的适配器来处理这个问题。查看下面的代码,它应该像一个魅力一样工作。

public class CustomAdapter extends BaseAdapter {

Context context;
int[] vehList;

public CustomAdapter(int[] vehList, Context context)
{
    this.context = context;
    this.vehList = vehList;
}

@Override
public int getCount() {
    return vehList.size();
}

@Override
public int getItem(int position) {
    return vehList.get(position);
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    if( convertView == null ){
        //We must create a View:
        convertView = LayoutInflater.from(context)
                  .inflate(R.layout.YourListViewLayoutRow, parent,false);
    }

   // this is where you create each row . Get current item from vehList
   int currentItem = vehList.get(position);
   // and then assigne the values and return the view object 
    return convertView;
}

}

然后是活动代码:

int[] vehs = new int[]{
    R.drawable.veh1,
    R.drawable.veh2,
    R.drawable.veh3,
    R.drawable.veh4,
    R.drawable.veh5,
    R.drawable.veh6,
    R.drawable.veh7,
    R.drawable.veh8,
    R.drawable.veh9,
    R.drawable.veh10,
    R.drawable.veh11,
    R.drawable.veh12,
    R.drawable.veh13,
    R.drawable.veh14,
    R.drawable.veh15,
    R.drawable.veh16,
    R.drawable.veh17,
    R.drawable.veh18,
    R.drawable.veh19,
    R.drawable.veh20,
    R.drawable.veh21,
    R.drawable.veh22,
    R.drawable.veh23,
    R.drawable.veh24,
    R.drawable.veh25,
    R.drawable.veh26,
    R.drawable.veh27,
    R.drawable.veh28,
    R.drawable.veh29,
    R.drawable.veh30
};


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    CustomAdapter adapter = new CustomAdapter(getBaseContext() , vehs);

    // Getting a reference to listview of activity_main.xml layout file
    ListView listView = ( ListView ) findViewById(R.id.listView1);

ListView.setAdapter(adapter);

【讨论】:

    猜你喜欢
    • 2023-01-30
    • 2017-08-28
    • 2019-01-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多