I'm having a rather mysterious problem. If I try to populate the ListView with AsyncTask, getView () in this ListView adapter will never be called. Note that this is the initial data set for this ListView when the Activity starts. AsyncTask is needed because ListView populates from HTTP.
I have the following simple layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<ListView
android:id="@+id/transactionListView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:divider="#CCCCCC"
android:dividerHeight="1px"
/>
</LinearLayout>
And the base onCreate ():
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mainListView = (ListView)findViewById(R.id.transactionListView);
this.populateListView();
}
Codes you can see:
private void populateListView()
{
populateListViewTask task = new populateListViewTask();
task.execute();
}
And finally, AsyncTask onPostExecute ():
protected void onPostExecute(Void result)
{
lvAdapter = new CustomArrayAdapter(MainActivity.this, R.layout.listitem, data);
mainListView.setAdapter(lvAdapter);
}
, , , , , getView() . lvAdapter.getCount() onPostExecute(), 30 ( , , XML/XML ). notifyDataSetChanged(), , CustomArrayAdapter, , ArrayAdapter. invalidate() , .
, alter onCreate() , , AsyncTask:
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mainListView = (ListView)findViewById(R.id.transactionListView);
TestDataStructure[] data = new TestDataStructure[1];|
data[0] = new TestDataStructure();
data[0].item = "Dummy List Item";
lvAdapter = new CustomArrayAdapter(MainActivity.this, R.layout.listitem, data);
mainListView.setAdapter(lvAdapter);
this.populateListView();
}
getView() onPostExecute(), ListView AsyncTask. , Android , transactionListView , , getView(), .
, , (, , AsyncTask)? Activity, ListActivity.
!