Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to add items from database to listview?
    text
    copied!<p>I usually use this code to add item from datebase to list view:</p> <pre><code>public class ViewEvents extends Activity { DBAdapter DB=new DBAdapter(this); public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.viewevents); final ListView myListView = (ListView)findViewById(R.id.MyList); final ArrayList&lt;String&gt; todoItems = new ArrayList&lt;String&gt;(); final ArrayAdapter&lt;String&gt; aa = new ArrayAdapter&lt;String&gt;(this,android.R.layout.simple_list_item_1, todoItems); DB.open(); Cursor c=DB.select(); c.moveToFirst(); Integer n=new Integer(c.getCount()); for(int i=0;i&lt;c.getCount();i++) { todoItems.add(0, c.getString(0)); c.moveToNext(); } aa.notifyDataSetChanged(); myListView.setAdapter(aa); enter code here </code></pre> <p>but, in my project I added image to listview as here : </p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="5dp" &gt; &lt;RelativeLayout android:id="@+id/relativeLayout1" android:layout_width="fill_parent" android:layout_height="wrap_content" &gt; &lt;ImageView android:id="@+id/logo" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_alignParentTop="true" android:drawablePadding="0dip" android:src="@drawable/icon_remove" &gt; &lt;/ImageView&gt; &lt;TextView android:id="@+id/label" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_toLeftOf="@+id/logo" android:text="@+id/label" android:textSize="25px" &gt; &lt;/TextView&gt; &lt;/RelativeLayout&gt; &lt;/LinearLayout&gt; </code></pre> <p>That causes problem in arrayadapter, I can't catch listview as usuall: </p> <pre><code>ListView myListView = (ListView)findViewById(R.id.MyList); </code></pre> <p>any advice please</p> <p>the new code:</p> <pre><code>public class RemoveEvent extends ListActivity { DBAdapter DB=new DBAdapter(this); @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); final ArrayList&lt;String&gt; todoItems = new ArrayList&lt;String&gt;(); final ArrayAdapter&lt;String&gt; aa = new ArrayAdapter&lt;String&gt;(this,android.R.layout.simple_list_item_1, todoItems); DB.open(); Cursor c=DB.select(); c.moveToFirst(); Integer n=new Integer(c.getCount()); for(int i=0;i&lt;c.getCount();i++) { todoItems.add(0, c.getString(0)); c.moveToNext(); } // todoItems.add(0, c.getString(0)); aa.notifyDataSetChanged(); setListAdapter(new RemoveArrayAdapter(this, .......)); } @Override protected void onListItemClick(ListView l, View v, int position, long id) { //get selected items String selectedValue = (String) getListAdapter().getItem(position); Toast.makeText(this, selectedValue, Toast.LENGTH_SHORT).show(); } } </code></pre> <p>removearrayadapter code:</p> <pre><code>public class RemoveArrayAdapter extends ArrayAdapter&lt;String&gt; { private final Context context; private final String[] values; public RemoveArrayAdapter(Context context, String[] values) { super(context, R.layout.removeevent, values); this.context = context; this.values = values; } @Override public View getView(int position, View convertView, ViewGroup parent) { LayoutInflater inflater = (LayoutInflater) context .getSystemService(Context.LAYOUT_INFLATER_SERVICE); View rowView = inflater.inflate(R.layout.removeevent, parent, false); TextView textView = (TextView) rowView.findViewById(R.id.label); ImageView imageView = (ImageView) rowView.findViewById(R.id.logo); textView.setText(values[position]); return rowView; } } </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