Note that there are some explanatory texts on larger screens.

plurals
  1. POCan you point out the issue with ListView in this code?
    primarykey
    data
    text
    <p>Following is some test code , done to recreate a strange bug: After deleting some items from a ListView , it stops refreshing when data is invalidated. More items are deleted but list does not refresh. Even Log cat does not show debug messages for deletion. I will appreciate if any one could find out what's wrong.</p> <p>Item Layout:</p> <pre><code>&lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="horizontal"&gt; &lt;TextView android:id="@+id/nameTextView" android:layout_width="wrap_content" android:layout_height="wrap_content" /&gt; &lt;Button android:id="@+id/deleteButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Delete" /&gt; &lt;/LinearLayout&gt; </code></pre> <p>Item class:</p> <pre><code>import android.content.Context; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.widget.Button; import android.widget.TextView; public class Item implements View.OnClickListener { private String name; private View itemView; private MyActivity owner; //--- getters-- public String getName() { return name; } public View getView() { return itemView; } public Item(String n, Context c , MyActivity o) { //---store the name given-- name = n; //---store reference to the owner activity-- owner = o; //--- create a View for this item---- LayoutInflater inflater = (LayoutInflater) c.getSystemService(Context.LAYOUT_INFLATER_SERVICE); itemView = inflater.inflate(R.layout.item,null); //---set up data to show-- TextView nameTextView = (TextView) itemView.findViewById(R.id.nameTextView); Button deleteButton = (Button) itemView.findViewById(R.id.deleteButton); nameTextView.setText(name); //---set up events to be handled-- deleteButton.setOnClickListener(this); Log.d("My_Test","Item: Hello world, my name is " + name); } //----request owner to delete this item--- @Override public void onClick(View view) { Log.d("My_Test","Item:"+name+" requesting owner to delete me"); owner.deleteItem(this); } </code></pre> <p>Activity layout:</p> <pre><code>&lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" &gt; &lt;ListView android:id="@+id/myListView" android:layout_width="fill_parent" android:layout_height="fill_parent" /&gt; &lt;/LinearLayout&gt; </code></pre> <p>Activity class:</p> <pre><code>import android.app.Activity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.view.ViewGroup; import android.widget.ArrayAdapter; import android.widget.ListView; import java.util.ArrayList; public class MyActivity extends Activity { private ArrayList&lt;Item&gt; myItems; private ListView myListView; private ArrayAdapter&lt;Item&gt; myArrayAdapter; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //-----adapter for item list---- //----since each item has its own view , it just returns the same--- myArrayAdapter = new ArrayAdapter&lt;Item&gt;(this,0){ @Override public View getView(final int position, View convertView, ViewGroup parent) { Item item = getItem(position); Log.d("My_Test","Adapter : View for Item: " + item.getName() +"is requested." ); return item.getView(); } }; //-----set up my list view with the adapter------ myListView = (ListView) findViewById(R.id.myListView); myListView.setAdapter(myArrayAdapter); //------add items------- //----each item has its own view and a reference to this activity as their owner---- myArrayAdapter.add(new Item("Sunday", this, this)); myArrayAdapter.add(new Item("Monday", this, this)); myArrayAdapter.add(new Item("Tuesday", this, this)); myArrayAdapter.add(new Item("Wednesday", this, this)); myArrayAdapter.add(new Item("Thursday", this, this)); myArrayAdapter.add(new Item("Friday", this, this)); myArrayAdapter.add(new Item("Saturday", this, this)); myArrayAdapter.notifyDataSetChanged(); } //----- called by items requesting to be deleted from the item list---- public void deleteItem(Item item) { myArrayAdapter.remove(item); Log.d("My_Test","Owner : Deleted item :" + item.getName()); myArrayAdapter.notifyDataSetChanged(); } } </code></pre> <p>Looks like ListView stops re-drawing it self. Even when List Item is no more in the item array, and <code>myAdapter.notifyDataSetInvalidated();</code> is called, The List Item stays visible , with further code execution some how blocked.</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.
 

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