0

i want to make a view as bellow :

enter image description here

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....

4

3 回答 3

1

您正在添加 json 数组的数组。不要添加 json 数组,将事物添加到列表中并添加列表代替数组。

KategoriEpolicyAdapter adapter = new KategoriEpolicyAdapter(KategoriNomorPolis.this, array);

它应该是这样的:

KategoriEpolicyAdapter adapter = new KategoriEpolicyAdapter(KategoriNomorPolis.this, list);

最好看看这个适配器

公共类 TestListAdapter 扩展 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;
}

}

于 2013-05-15T05:22:34.660 回答
0

两件事情。

首先,您使用 JSONArray 和 JSONObject 有什么原因吗?虽然我对此不是百分百肯定,但我认为您不能使用 JSONArray 支持您的适配器。我认为 BaseAdapter 只接受常规的 Java 数组或 List 对象(或 List 的子类之一)。如果不需要,最好不要使用 JSON 对象。如果您确实需要使用 JSONArray,请查看此链接。它将向您展示如何将 JSONArray 解析为可在适配器中使用的 Java 数组。

其次,我认为覆盖getView()方法中的 try/catch 块可能是另一个问题。你让它捕获所有异常,但它没有输出任何东西来让你知道如果抛出一个异常就会有异常。因此,您的代码可能无法在被覆盖的getView(). 将 aSystem.out.println()和 a添加printStackTrace()到 catch 代码块,再次运行代码,看看它是否打印出任何内容。

于 2013-05-15T05:20:13.050 回答
0

首先,通过调试检查您的数组不为空。然后,您可以尝试实现getItem()适配器的方法。

public Object getItem(int index) {
    return array.get(index);
}

并且在getView()方法上不要直接从数组中获取 jsonObject。使用getItem()方法。

顺便说一句,您实现的适配器将使用太多内存。您必须小心 getView 方法。contentView如果它不为空,请尝试使用。

于 2013-05-15T06:17:40.023 回答