Reputation: 2562
i want to make a view as bellow :
for control it, i made JSON in my class variabel, this is my Activity :
public class KategoriNomorPolis extends ListActivity{
ListView lv_epolicy;
ArrayAdapter<String> adapter;
List<String> list = new ArrayList<String>();
String[][] c = new String[10][10];
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.epolicy_list_polis);
adapter=new ArrayAdapter<String>(this,R.layout.list_item);
kategori_epolicy();
}
private void kategori_epolicy(){
String result=ListPolis.json_kategori;
JSONArray array=null;
try{
array= new JSONArray(result);
for (int i=0;i<array.length();i++){
JSONObject object = array.getJSONObject(i);
c[i][0] = object.getString("id_kategori");
c[i][1] = object.getString("kategori");
// adapter.insert(object.getString("kategori"), i);
System.out.println("yuhuuu");
}
lv_epolicy=(ListView)findViewById(android.R.id.list);
KategoriEpolicyAdapter adapter = new KategoriEpolicyAdapter(KategoriNomorPolis.this, array);
lv_epolicy.setAdapter(adapter);
lv_epolicy.setCacheColorHint(Color.TRANSPARENT);
}catch (Exception e) {
Toast.makeText(getBaseContext(), e.getMessage(), Toast.LENGTH_SHORT).show();
}
I have no idea why my adapter not showing anything,i have declare my listview with lv_epolicy=(ListView)findViewById(android.R.id.list);
and nothing happen and this is my KategoriEpolicyAdapter :
public class KategoriEpolicyAdapter extends BaseAdapter{
Context context;
JSONArray array;
int count;
public KategoriEpolicyAdapter(Context context, JSONArray array){
this.context = context;
this.array = array;
this.count = array.length();
}
public int getCount() {
// TODO Auto-generated method stub
return count;
}
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return null;
}
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return 0;
}
public View getView(int position, View contentView, ViewGroup arg2) {
// TODO Auto-generated method stub
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
contentView = inflater.inflate(R.layout.epolicy_kategori, null);
ImageView img_epolicy = (ImageView)contentView.findViewById(R.id.img_epolicy);
TextView text_kategori = (TextView)contentView.findViewById(R.id.text_kategori);
String result=ListPolis.json_kategori;
try {
JSONObject object = array.getJSONObject(position);
text_kategori.setText(object.getString("kategori"));
System.out.println("bisa ga ya? ");
switch(position){
case 0:
img_epolicy.setImageResource(R.drawable.pp_icon);
break;
case 1:
img_epolicy.setImageResource(R.drawable.tertanggung_icon);
break;
case 2:
img_epolicy.setImageResource(R.drawable.dataasuransi_icon);
break;
case 3:
img_epolicy.setImageResource(R.drawable.manfaat_icon);
break;
case 4:
img_epolicy.setImageResource(R.drawable.inventaris_icon);
break;
case 5:
img_epolicy.setImageResource(R.drawable.status_icon);
break;
case 6:
img_epolicy.setImageResource(R.drawable.rekening_icon);
break;
}
} catch (Exception e) {
// TODO: handle exception
}
return contentView;
}
i hope somebody can tell me where is my fault....
Upvotes: 0
Views: 1163
Reputation: 7092
You are adding the array that is json array. Do not add json array, add the things in a list and add the list in place of array.
KategoriEpolicyAdapter adapter = new KategoriEpolicyAdapter(KategoriNomorPolis.this, array);
It should look like that:
KategoriEpolicyAdapter adapter = new KategoriEpolicyAdapter(KategoriNomorPolis.this, list);
Better see this adapter
public class TestListAdapter extends ArrayAdapter {
private LayoutInflater mInflater=null;
private HttpImageManager mHttpImageManager=null;
private ArrayList<SeedObject> listData=null;
public TestListAdapter(Activity context,ArrayList<SeedObject> list, List<Uri> uris) {
super(context, 0, uris);
mInflater = context.getLayoutInflater();
this.listData=list;
mHttpImageManager = ((TestApplication) context.getApplication()).getHttpImageManager();
}
public View getView(int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
if (convertView == null || convertView.getTag() == null) {
holder = new ViewHolder();
convertView = mInflater.inflate(R.layout.custom_row, null);
holder.url = (TextView) convertView.findViewById(R.id.text_List);
holder.city = (TextView) convertView.findViewById(R.id.text_city);
holder.image = (ImageView) convertView.findViewById(R.id.image_List);
convertView.setTag(holder);
}
else {
holder = (ViewHolder) convertView.getTag();
}
final Uri uri = getItem(position);
ImageView imageView = holder.image;
if(uri != null){
Bitmap bitmap = mHttpImageManager.loadImage(new HttpImageManager.LoadRequest(uri, imageView));
if (bitmap != null) {
// image download now compress and set on Image view
bitmap = Bitmap.createScaledBitmap(bitmap, 60, 60, true);
imageView.setImageBitmap(bitmap);
}
else if(position %2== 0){
imageView.setImageResource(R.drawable.greenseed_new);
}
else{
imageView.setImageResource(R.drawable.grayseed_new);
}
}
//set the seed name
holder.url.setText(listData.get(position).getTitle());
String distance =listData.get(position).getDistance();
return convertView;
}
private static class ViewHolder {
ImageView image;
TextView city;
TextView url;
}
}
Upvotes: 1
Reputation: 15533
Firstly, check your array is not null by debugging.
Then, you may try to implement getItem()
method of adapter.
public Object getItem(int index) {
return array.get(index);
}
And on getView()
method do not get jsonObject from array directly. use getItem()
method.
By the way, adapter you have implemented is going to use too much memory. You must be careful on getView method. Try to use contentView
if it is not null.
Upvotes: 0
Reputation: 526
Two things.
First off, is there any reason that you're using a JSONArray and JSONObject? While I'm not one hundred percent sure about this, I don't think that you can back your adapter with a JSONArray. I think the BaseAdapter only accepts regular Java arrays or the List object (or one of List's sub-classes). It would be better to not use JSON objects if you don't have to. If you really need to use a JSONArray, check out this link. It will show you how to parse a JSONArray into a Java array that you can use in your Adapter.
Second, I think that the try/catch block in your overriden getView()
method might be another problem. You have it catching all exceptions, but it's not outputting anything to let you know that there's an exception if one is thrown. So, your code may not be executing correctly in the overridden getView()
. Add a System.out.println()
and a printStackTrace()
to the catch code block, run the code again, and see if it prints anything out.
Upvotes: 0