firstly, I created a repo on gitHub with a sample project with my error in it: https://github.com/renaudfavier/SampleBug
I am having difficulty with my lists in my customViewGroups: I will simulate an assynk data request, then I tring to populate my list, and there are difficulties with it. Please note that there is a little bust in this application, because I need it in my big one. I cannot go to fragments.
My problem is that my listView does not update, it happens when I open my menu
package renaud.bug; import java.util.ArrayList; import android.app.Activity; import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.util.Log; import android.view.LayoutInflater; import android.view.Menu; import android.view.View; import android.view.ViewGroup; import android.view.View.OnClickListener; import android.widget.ArrayAdapter; import android.widget.ImageView; import android.widget.ListView; import android.widget.TextView; import android.widget.Toast; public class SampleBugActivity extends Activity { private boolean menuDisplayed; private CustomViewGroup cvg; private ListView myListView; private ArrayList<String> myStrings; private MyAdapter stringAdapter; private LayoutInflater inflater; private ArrayList<String> array; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); myStrings = new ArrayList<String>(); inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); cvg = (CustomViewGroup) findViewById(R.id.viewGroup); stringAdapter = new MyAdapter(this, R.layout.list_view_item, myStrings); myListView = (ListView) findViewById(R.id.listView1); stringAdapter.add("TEST "); stringAdapter.notifyDataSetChanged(); myListView.setAdapter(stringAdapter); new Thread(assyncSim).start(); } private Runnable onFinish = new Runnable() { @Override public void run() { Toast.makeText(getApplicationContext(), "thread finishes push menu button", Toast.LENGTH_LONG); Log.i("renaud", "thread finishes push menu button"); if (array != null && array.size() > 0) { stringAdapter.notifyDataSetChanged(); for (int i = 0; i < array.size(); i++) { stringAdapter.add(array.get(i)); } } stringAdapter.notifyDataSetChanged(); } }; private Runnable assyncSim = new Runnable() { @Override public void run() { array = new ArrayList<String>(); for (int i = 0; i < 10; i++) { array.add("Item " + i); } synchronized (this) { try { wait(5000); } catch (InterruptedException e) {
my customViewGroup:
package renaud.bug; import android.content.Context; import android.util.AttributeSet; import android.view.LayoutInflater; import android.view.View; import android.view.View.MeasureSpec; import android.view.ViewGroup; public class CustomViewGroup extends ViewGroup{ private LayoutInflater inflater; private int mCurScreen; private int mDefaultScreen = 0; private View menuView; private View practicalView; public CustomViewGroup(Context context) { super(context); initView(context); } public CustomViewGroup(Context context, AttributeSet attrs) { super(context, attrs); initView(context); } public CustomViewGroup(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); initView(context); } private void initView(Context context) { mCurScreen = mDefaultScreen; inflater = (LayoutInflater) context .getSystemService(Context.LAYOUT_INFLATER_SERVICE); practicalView = inflater.inflate(R.layout.layout, null); this.addView(practicalView); menuView = inflater.inflate(R.layout.menu_layout, null); this.addView(menuView); } @Override protected void onLayout(boolean changed, int l, int t, int r, int b) {
my layouts:
main:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <renaud.bug.CustomViewGroup xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/viewGroup" android:layout_width="fill_parent" android:layout_height="fill_parent" > </renaud.bug.CustomViewGroup> </LinearLayout>
layout.xml:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <ListView android:id="@+id/listView1" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#991111"/> </LinearLayout>
menu_layout.xml:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:background="#119911" > </LinearLayout>
Listviewitem
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:id="@+id/itemTextView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="#FF0" android:textAppearance="?android:attr/textAppearanceMedium" /> </LinearLayout>