I'm new at developing and I started making an app to show a list of 30 images, but somehow, it only shows the first 10 images. I don't really know what I'm doing wrong. If you could help me, I will be much appreciated.
This is a part of what I have:
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);
}
It only shows from veh1 to veh10 :(
I dont have much experience with SimpleAdapter
, i've always been creating my own custom adapter mostly extending BaseAdapter
. If I were you i'd create my own adapter to handle that problem . Check out the code below it should work like a charm.
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;
}
}
Then the activity code:
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);
seems like maybe it's having trouble mapping the strings to drawables.
try this to map the resources as int's instead of strings.
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);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.