Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>OK, what you have done is take the getGenericView() method from the sample docs, using the "this" keyword in that scenario will work, because "this" refers to the context of the Activity (the outer class in the example). </p> <p>The best way to bind to an adapter is to inflate views (that is defined in xml). This way, Android will handle that for you.</p> <p>Here is an example of a getView method from one of my adapters:</p> <pre><code>@Override public View getView(int position, View convertview, ViewGroup parent) { View v = convertview; final NoteInformationItemEntity e = (NoteInformationItemEntity) getItem(position); if (v == null) { LayoutInflater vi = (LayoutInflater) mContext .getSystemService(Context.LAYOUT_INFLATER_SERVICE); v = vi.inflate(R.layout.reminder_item, null); } ImageView img = (ImageView)v.findViewById(R.id.reminder_item_imgIcon); TextView txtTitle = (TextView)v.findViewById(R.id.reminder_item_txtTitle); TextView txtMainDescription = (TextView)v.findViewById(R.id.reminder_item_txtMainDescription); TextView txtSubDescription = (TextView)v.findViewById(R.id.reminder_item_txtSubDescription); img.setImageDrawable(mContext.getResources().getDrawable(e.getImageResourceID())); txtTitle.setText(e.getTitle()); txtMainDescription.setText(Html.fromHtml(e.getMainDescription())); txtSubDescription.setText(Html.fromHtml(e.getSubDescription())); return v; } </code></pre> <p>R.layout.reminder_item is a xml file in your layout directory.</p> <pre><code>&lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_height="wrap_content" android:background="@drawable/list_item_styled_9" android:layout_width="fill_parent"&gt; &lt;ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="5dip" android:id="@+id/reminder_item_imgIcon"&gt;&lt;/ImageView&gt; &lt;LinearLayout android:layout_height="wrap_content" android:orientation="vertical" android:layout_width="fill_parent"&gt; &lt;LinearLayout android:layout_height="wrap_content" android:layout_width="fill_parent" android:layout_weight="1" android:orientation="horizontal"&gt; &lt;TextView android:layout_height="wrap_content" android:id="@+id/reminder_item_txtTitle" style="@style/ReminderListTitle" android:layout_width="wrap_content" android:layout_weight="1"&gt;&lt;/TextView&gt; &lt;/LinearLayout&gt; &lt;LinearLayout android:layout_height="wrap_content" android:layout_width="fill_parent" android:layout_weight="1" android:orientation="vertical"&gt; &lt;TextView android:layout_height="wrap_content" android:layout_width="fill_parent" android:layout_weight="1" android:id="@+id/reminder_item_txtMainDescription" style="@style/ReminderListText"&gt;&lt;/TextView&gt; &lt;TextView android:layout_height="wrap_content" android:layout_width="fill_parent" android:layout_weight="1" android:id="@+id/reminder_item_txtSubDescription" style="@style/ReminderListText"&gt;&lt;/TextView&gt; &lt;/LinearLayout&gt; &lt;/LinearLayout&gt; </code></pre> <p></p> <p>I pass the context and my data to my adapter in the constructor:</p> <pre><code>Context mContext; ArrayList&lt;NoteInformationItemEntity&gt; data; public NoteListViewItemAdapter(Context context, ArrayList&lt;NoteInformationItemEntity&gt; _data) { mContext = context; data = _data; } </code></pre> <p>If you don't want to inflate the views like the example above, you can use that context to create your TextView</p> <pre><code>public TextView getGenericView() { // Layout parameters for the ExpandableListView AbsListView.LayoutParams lp = new AbsListView.LayoutParams( ViewGroup.LayoutParams.MATCH_PARENT, 64); TextView textView = new TextView(mContext); textView.setLayoutParams(lp); // Center the text vertically textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT); // Set the text starting position } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload