Android - Custom Adapter Problem

Posted by Ryan on Stack Overflow See other posts from Stack Overflow or by Ryan
Published on 2010-06-07T19:48:05Z Indexed on 2010/06/07 19:52 UTC
Read the original article Hit count: 765

Filed under:
|

Hello, I seem to be having a problem with my Custom Adapter view. When I display the list, it only displays a white screen. Here is how it works:

1.) I send a JSON request 2.) populate the ArrayList with the returned results 3.) create a custom adapter 4.) then bind the adapter.

Here is steps 2-4

private void updateUI()

{

ListView myList = (ListView) findViewById(android.R.id.list); itemList = new ArrayList();

Iterator it = data.entrySet().iterator(); while (it.hasNext()) { //Get the key name and value for it Map.Entry pair = (Map.Entry)it.next(); String keyName = (String) pair.getKey(); String value = pair.getValue().toString(); if (value != null) { ListItem li = new ListItem(keyName, value, false); itemList.add(li); } }

CustomAdapter mAdapter = new CustomAdapter( mContext, itemList);

myList.setAdapter(mAdapter); //Bind the adapter to the list

//Tell the dialog it's cool now. dismissDialog(0);

//Show next screen flipper.setInAnimation(inFromRightAnimation()); flipper.setOutAnimation(outToRightAnimation()); flipper.showNext();

}

And here is my CustomAdapter class:

import java.util.List;

import android.R.color; import android.content.Context; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.ImageView; import android.widget.RelativeLayout; import android.widget.TextView;

class MyAdapterView extends RelativeLayout {
public MyAdapterView(Context c, ListItem li) { super( c );

RelativeLayout rL = new RelativeLayout(c); RelativeLayout.LayoutParams containerParams = new RelativeLayout.LayoutParams( ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT); rL.setLayoutParams(containerParams); rL.setBackgroundColor(color.white);

ImageView img = new ImageView (c); img.setImageResource(li.getImage()); img.setPadding(5, 5, 10, 5); rL.addView(img, 48, 48);

TextView top = new TextView(c); top.setText(li.getTopText()); top.setTextColor(color.black); top.setTextSize(20); top.setPadding(0, 20, 0, 0); rL.addView(top,ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);

TextView bot = new TextView( c ); bot.setText(li.getBottomText()); bot.setTextColor(color.black); bot.setTextSize(12); bot.setPadding(0, 0, 0, 10); bot.setAutoLinkMask(1); rL.addView(bot,ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);

} }

public class CustomAdapter extends BaseAdapter {

private Context context; private List itemList;

public CustomAdapter(Context c, List itemL ) { this.context = c; this.itemList = itemL; }

public int getCount() {
return itemList.size(); }

public Object getItem(int position) {
return itemList.get(position); }

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

@Override public View getView(int position, View convertView, ViewGroup parent) { ListItem li = itemList.get(position); return new MyAdapterView(this.context, li); }

}

Does anyone have any idea why this displays a white screen upon completion?? Thanks in advance!

© Stack Overflow or respective owner

Related posts about android

Related posts about arrayadapter