1. Download our Official Android App: Forums for Android!

Apps Beginner List Help

Discussion in 'Android Development' started by JosephTaylorC, Apr 22, 2011.

  1. JosephTaylorC

    JosephTaylorC Lurker
    Thread Starter
    Rank:
    None
    Points:
    5
    Posts:
    7
    Joined:
    Apr 5, 2011

    Apr 5, 2011
    7
    0
    5
    This code is directly from the Beginning Android 2 book and I cannot seem to get it to work. I do not know if it is outdated or not but I would really like to know why it does not work.

    The purpose is to have a list of views, each with an image and some text.

    public class DynamicDemo extends ListActivity {
    TextView selection;
    String[] items={"lorem", "ipsum", "dolor", "sit", "amet",
    "consectetuer", "adipiscing", "elit", "morbi", "vel",
    "ligula", "vitae", "arcu", "aliquet", "mollis",
    "etiam", "vel", "erat", "placerat", "ante",
    "porttitor", "sodales", "pellentesque", "augue",
    "purus"};

    @Override
    public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    setContentView(R.layout.main);
    setListAdapter(new IconicAdapter());
    selection=(TextView)findViewById(R.id.selection);
    }

    public void onListItemClick(ListView parent, View v,
    int position, long id) {
    selection.setText(items[position]);
    }

    class IconicAdapter extends ArrayAdapter {
    IconicAdapter() {
    super(DynamicDemo.this, R.layout.row, items);
    }

    public View getView(int position, View convertView,
    ViewGroup parent) {
    LayoutInflater inflater=getLayoutInflater();
    View row=inflater.inflate(R.layout.row, parent, false);
    TextView label=(TextView)row.findViewById(R.id.label);

    label.setText(items[position]);

    ImageView icon=(ImageView)row.findViewById(R.id.icon);

    if (items[position].length()>4) {
    icon.setImageResource(R.drawable.delete);
    }
    else {
    icon.setImageResource(R.drawable.ok);
    }

    return(row);
    }
    }
    }
     

    Advertisement

Share This Page

Loading...