在我的应用程序中,我从网上下载了一个字符串列表
然后我正在使用 AsyncTask 更新我的活动的列表视图。在 prePostExecute 时创建数组适配器(私有成员)
在 doInBackground 我下载列表并返回它
在 onPostExecute 我将返回的列表设置为适配器,然后连接列表视图和适配器
在我的活动中,我只看到 1 个空白元素(而不是 2 个元素),当我单击它时,我可以看到文本,但当我不是空白时。
另外,当我的适配器不是 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();
}
}
}