私のアプリでは、ウェブから文字列のリストをダウンロードします
次に、AsyncTask を使用してアクティビティのリストビューを更新しています。prePostExecute で、アレイ アダプターを作成します (プライベート メンバー)
doInBackground で、リストをダウンロードして返します
onPostExecute で、返されたリストをアダプターに設定し、リストビューとアダプターを接続します
私のアクティビティでは、(2 つの要素ではなく) 空白の要素が 1 つだけ表示されます。クリックするとテキストが表示されますが、空白ではない場合は表示されます。
また、アダプターが android.R.layout.simple_list_1 ではなく android.R.layout.acitiviy_list_item (および他の 2 つのテンプレート) の場合、テキストが表示された 2 つの要素が表示されますが、テキストは小さく灰色です
コード: XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/parent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusable="true"
android:focusableInTouchMode="true"
android:orientation="vertical" >
<TableRow
android:id="@+id/tableRow0"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingBottom="5dip" >
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:background="#4CB8FB"
android:gravity="center"
android:text="Search"
android:textColor="#FAFAFA"
android:textSize="40sp" />
</TableRow>
<EditText
android:id="@+id/searchInput"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawableLeft="@drawable/ic_btn_search" />
<ListView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fastScrollEnabled="true"
android:scrollingCache="true" >
</ListView>
</LinearLayout>
コードビハインド
public class ProductPickerActivity extends Activity {
private ListView listView;
private EditText inputSearch;
private ArrayAdapter<String> adapter;
private final Integer PRODUCT_REQUEST_ID = 5;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_product_listview);
inputSearch = (EditText) findViewById(R.id.searchInput);
inputSearch.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
// When user changed the Text
ProductPickerActivity.this.adapter.getFilter().filter(cs);
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
}
@Override
public void afterTextChanged(Editable arg0) {
}
});
new InitializeUi().execute(this);
setupUI(findViewById(R.id.parent));
}
private class InitializeUi extends AsyncTask<Object, Void, List<IProduct>> {
@Override
protected void onPreExecute() {
// Get ListView object from xml
listView = (ListView) findViewById(R.id.list);
// Define a new Adapter
// First parameter - Context
// Second parameter - Layout for the row
// Third parameter - ID of the TextView to which the data is written
// Forth - the Array of data
adapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.activity_list_item, new ArrayList<String>());
// ListView Item Click Listener
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// ListView Clicked item index
int itemPosition = position;
// ListView Clicked item value
String itemValue = (String) listView.getItemAtPosition(position);
Intent intent = new Intent(getApplicationContext(), ProductViewActivity.class);
intent.putExtra("ProductName", itemValue);
startActivityForResult(intent, PRODUCT_REQUEST_ID);
}
});
}
@Override
protected List<IProduct> doInBackground(Object... params) {
// Defined Array values to show in ListView
IProductManager manager = new ProductManager(getApplicationContext());
// download from web
List<IProduct> lst = manager.GetProductList();
return lst;
}
@Override
protected void onPostExecute(List<IProduct> lst) {
String[] values = new String[lst.size()];
Integer i = 0;
for (IProduct p : lst) {
values[i] = p.getName();
i = i + 1;
Log.d("ProductPickerActivity", "Got Values : " + p.getName());
adapter.add(p.getName());
adapter.notifyDataSetChanged();
}
// Assign adapter to ListView
listView.setAdapter(adapter);
adapter.notifyDataSetChanged();
((BaseAdapter) listView.getAdapter()).notifyDataSetChanged();
listView.refreshDrawableState();
}
}
}