我是开发新手,我开始创建一个应用程序来显示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 :(
答案 0 :(得分:0)
我对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);
答案 1 :(得分:0)
似乎可能无法将字符串映射到drawables。
尝试将资源映射为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);
}