Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I create a ListView of bitmap and string updating progressively after OnCreate?
    primarykey
    data
    text
    <p>I have a <code>ListView</code> on main activity, which shows a Contact picture on the left and a textview showing on the right on each row. Everything works like it should, but the problem is that when application is launching,and when there is many messages, the user must wait about more than 10 seconds to start using the app. </p> <p>What I tried to do was finding a way to load each row progressively, and avoid waiting every rows to be loaded to start using the app. I tried <code>AsyncTasks</code>, <code>Handlers</code>, <code>Threads</code>, <code>Hashmaps</code>.. But I can find a way to manage this task.</p> <p>My Activity is a ListActivity. </p> <p>the listview of the activity uses a custom Adapter with parameters (this,smsarray,picturearray). <code>smsarray</code> is an <code>ArrayList&lt;String&gt;</code> containing strings of each sms, and <code>picturearray</code> is an <code>ArrayList&lt;Bitmap&gt;</code> containing contact pictures in bitmap format for each sms.</p> <p>I have a method <code>getSMSData()</code> which gets the sms strings and bitmaps with the help of a cursor, and puts strings at index 0 on smsarray, and bitmaps at index 0 on picturearray, for each contact on launch.</p> <pre><code>public class MessageList extends ListActivity { static List&lt;String&gt; smsarray; static List&lt;Bitmap&gt; picturearray; static MessageListAdapter smsadapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); smsarray = new ArrayList&lt;String&gt;(); picturearray = new ArrayList&lt;Bitmap&gt;(); getSMSData(); smsadapter = new MessageListAdapter(this,smsarray,picturearray); getListView().setAdapter(smsadapter); } public void getSMSData() { Bitmap defaultbitmap = BitmapFactory.decodeResource(getResources(), R.drawable.default_contact_picture); Bitmap contactbitmap; Cursor cursor = getContentResolver().query(Uri.parse("content://sms/inbox"),new String [] {"person","address","body","date"} , null, null,"date ASC"); String[] displayname = new String[]{ContactsContract.Contacts.DISPLAY_NAME}; String name; String person; String address; String date; String body; int id; Cursor contactcursor; while (cursor.moveToNext()) { person = cursor.getString(cursor.getColumnIndex("person")); address = cursor.getString(cursor.getColumnIndex("address")); date = cursor.getString(cursor.getColumnIndex("date")); body = cursor.getString(cursor.getColumnIndex("body")); if (person != null){ contactcursor = getContentResolver().query(Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_URI, person),displayname, null, null, null); contactcursor.moveToFirst(); name = contactcursor.getString(0); id = Integer.parseInt(person); InputStream input = ContactsContract.Contacts.openContactPhotoInputStream(getContentResolver(), ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, id)); contactbitmap = null; if (input == null) { contactbitmap = defaultbitmap; } else { contactbitmap = BitmapFactory.decodeStream(input); } if (contactbitmap != null){ picturearray.add(0,contactbitmap); }else { picturearray.add(0,defaultbitmap); } smsarray.add(0,getResources().getString(R.string.From) +" "+name+" "+"\n"+getResources().getString(R.string.Number)+" "+address+" " +"\n"+getResources().getString(R.string.At) +" "+date+"\n"+getResources().getString(R.string.Message)+" "+body); } if (person == null) { picturearray.add(0,defaultbitmap); smsarray.add(0,getResources().getString(R.string.From) +" "+getResources().getString(R.string.Unknown)+" "+"\n"+getResources().getString(R.string.Number)+" "+address+" " +"\n"+getResources().getString(R.string.At) +" "+date+"\n"+getResources().getString(R.string.Message)+" "+body); } } } } public class MessageListAdapter extends ArrayAdapter &lt;String&gt; { final Context vcontext; final List&lt;String&gt; varray; final List&lt;Bitmap&gt; vpicture; final LayoutInflater layoutinflater; public MessageListAdapter (Context context,List&lt;String&gt; smsarray, List&lt;Bitmap&gt; picturearray) { super(context, 0); vcontext = context; varray = smsarray; vpicture = picturearray; this.layoutinflater = LayoutInflater.from(context); } public int getCountpicture() { return vpicture.size(); } public Bitmap getItempicture(int position) { return vpicture.get(position); } public long getItemIdpicture(int position) { return position; } public int getCount() { return varray.size(); } public String getItem(int position) { return varray.get(position); } public long getItemId(int position) { return position; } public class ViewHolder { TextView messagetext; ImageView image; } public View getView(int position, View convertView, ViewGroup parent) { ViewHolder holder; if (convertView == null){ convertView = layoutinflater.inflate(R.layout.listitem, null); holder = new ViewHolder (); holder.messagetext = (TextView) convertView.findViewById(R.id.messagetext); holder.image = (ImageView) convertView.findViewById(R.id.contactimage); convertView.setTag(holder); } else { holder = (ViewHolder) convertView.getTag(); } holder.image.setImageBitmap(vpicture.get(position)); holder.image.setPadding(2, 1, 0, 0); holder.messagetext.setText(varray.get(position)); holder.messagetext.setPadding(10, -1, 10, 5); return convertView; } } </code></pre> <p>Thanks for your precious help.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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