Note that there are some explanatory texts on larger screens.

plurals
  1. POadd data to ListView in a class which extends Activity and not ListActivity
    primarykey
    data
    text
    <p>I have a Listview in a relative layout. How can I add data to ListView from the ManActivity which extends Activity. I could have used adapter.notifyDataSetChanged() if i have used the class which extends ListActivity.... i guess. Any suggestions? Thanks in advance</p> <pre><code>Message msg[] = new Message[] { new Message(1, "Hello World", "Thank You"), new Message(1, "Hello World 2", "Thank You 2") }; adapter = new MyAdapter(context, R.layout.row, msg); listview.setAdapter(adapter); </code></pre> <p>Adding this will make my app Crash</p> <pre><code>adapter.add(new Message(1, "Hello World3", "Thank You3")); </code></pre> <p><strong>Logcat text</strong></p> <pre><code>07-07 20:22:53.680: E/AndroidRuntime(1512): FATAL EXCEPTION: AsyncTask #3 07-07 20:22:53.680: E/AndroidRuntime(1512): java.lang.RuntimeException: An error occured while executing doInBackground() 07-07 20:22:53.680: E/AndroidRuntime(1512): at android.os.AsyncTask$3.done(AsyncTask.java:299) 07-07 20:22:53.680: E/AndroidRuntime(1512): at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352) 07-07 20:22:53.680: E/AndroidRuntime(1512): at java.util.concurrent.FutureTask.setException(FutureTask.java:219) 07-07 20:22:53.680: E/AndroidRuntime(1512): at java.util.concurrent.FutureTask.run(FutureTask.java:239) 07-07 20:22:53.680: E/AndroidRuntime(1512): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230) 07-07 20:22:53.680: E/AndroidRuntime(1512): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080) 07-07 20:22:53.680: E/AndroidRuntime(1512): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573) 07-07 20:22:53.680: E/AndroidRuntime(1512): at java.lang.Thread.run(Thread.java:856) 07-07 20:22:53.680: E/AndroidRuntime(1512): Caused by: java.lang.UnsupportedOperationException 07-07 20:22:53.680: E/AndroidRuntime(1512): at java.util.AbstractList.add(AbstractList.java:404) 07-07 20:22:53.680: E/AndroidRuntime(1512): at java.util.AbstractList.add(AbstractList.java:425) 07-07 20:22:53.680: E/AndroidRuntime(1512): at android.widget.ArrayAdapter.add(ArrayAdapter.java:179) 07-07 20:22:53.680: E/AndroidRuntime(1512): at com.abhishek.ally2.chat.doInBackground(chat.java:28) 07-07 20:22:53.680: E/AndroidRuntime(1512): at com.abhishek.ally2.chat.doInBackground(chat.java:1) 07-07 20:22:53.680: E/AndroidRuntime(1512): at android.os.AsyncTask$2.call(AsyncTask.java:287) 07-07 20:22:53.680: E/AndroidRuntime(1512): at java.util.concurrent.FutureTask.run(FutureTask.java:234) 07-07 20:22:53.680: E/AndroidRuntime(1512): ... 4 more </code></pre> <p><strong>MyAdapter.java</strong></p> <pre><code>package com.abhishek.ally2; import android.app.Activity; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ArrayAdapter; import android.widget.ImageView; import android.widget.TextView; public class MyAdapter extends ArrayAdapter&lt;Message&gt;{ private final Context context; private final Message data[]; int layoutResourceId; public MyAdapter(Context context, int layoutResourceId, Message[] values) { // TODO Auto-generated constructor stub super(context, layoutResourceId, values); this.context = context; this.data = values; this.layoutResourceId = layoutResourceId; } @Override public View getView(int position, View convertView, ViewGroup parent) { View row = convertView; MessageHolder holder = null; if(row == null) { LayoutInflater inflator = ((Activity)context).getLayoutInflater(); row = inflator.inflate(layoutResourceId, parent, false); holder = new MessageHolder(); holder.pic = (ImageView)row.findViewById(R.id.pic); holder.msg = (TextView)row.findViewById(R.id.msg); holder.time = (TextView)row.findViewById(R.id.time); row.setTag(holder); } else { holder = (MessageHolder)row.getTag(); } Message message = data[position]; holder.msg.setText(message.msg); holder.time.setText(message.time); return row; } static class MessageHolder { ImageView pic; TextView msg; TextView time; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    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