我正在创建自定义列表活动。有两个类,一个是具有扩展的ListActivity,另一个是具有该列表活动的ArrayAdapter。
问题1: 问题是,在打开ListActivity屏幕时,我有一个搜索框。当我启动ListActivity时,它会自动打开KeyBoard,因为当前焦点在 EditText 上。我想阻止它。
问题2: 此外,当焦点转到EditText时,会自动调用ArrayAdapter的 getView()方法,这将再次生成所有行。我不知道为什么会这样。我也没有调用 notifyDataSetchanged()方法。当我专注于EditText时,会自动调用getView()。怎么预防呢?
storelistview.xml:
<?xml version="1.0" encoding="UTF-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:background="@color/black" android:orientation="vertical"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:focusable="true" android:focusableInTouchMode="true">
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TableRow android:id="@+id/tableRow2" android:layout_height="wrap_content"
android:gravity="center" android:layout_width="fill_parent"
android:background="#919191">
<EditText android:id="@+id/searchText" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:width="240dp"
android:hint="Search"></EditText>
<ImageView android:layout_width="wrap_content"
android:src="@drawable/search" android:layout_height="wrap_content"
android:id="@+id/searchImage"></ImageView>
</TableRow>
</TableLayout>
<ListView android:id="@+id/android:list" android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
<TextView android:id="@+id/android:empty"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:textColor="@color/white" android:textSize="14sp"
android:gravity="top|center" android:text="No store found.." /></LinearLayout>
StoreListView.java:
public class StoreListView extends ListActivity {
private List<Store> stores = new ArrayList<Store>();
private StoreListRowAdapter rowAdapter;
private Runnable fetchStores;
private Handler handler = new Handler();
private ImageView searchImage;
private EditText searchText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.storelistview);
searchText = (EditText) findViewById(R.id.searchText);
searchImage = (ImageView) findViewById(R.id.searchImage);
searchImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
}
});
rowAdapter = new StoreListRowAdapter(this, R.layout.storelistview, stores);
setListAdapter(rowAdapter);
fetchStores = new Runnable() {
@Override
public void run() {
Store store = new Store();
StoreListAsynTask s = new StoreListAsynTask();
s.execute(store);
}
};
handler.post(fetchStores);
}
private Runnable resultThread = new Runnable() {
public void run() {
rowAdapter.clear();
for (int i = 0; i < stores.size(); i++) {
Store bean = stores.get(i);
rowAdapter.add(bean);
}
//rowAdapter.notifyDataSetChanged();
}
};
class StoreListAsynTask extends AsyncTask<Store, Void, String> {
private Gson gson = new Gson();
private List<Store> storeList;
private ProgressDialog m_ProgressDialog = null;
@Override
protected String doInBackground(Store... params) {
return respData;
}
@Override
protected void onPostExecute(String result) {
//populate store list
stores = storeList;
runOnUiThread(resultThread);
m_ProgressDialog.dismiss();
}
}
}
StoreListRowAdapter.java:
public class StoreListRowAdapter extends ArrayAdapter<Store> {
List<Store> stores;
public StoreListRowAdapter(Context context, int textViewResourceId, List<Store> stores) {
super(context, textViewResourceId, stores);
this.stores = stores;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
if (view == null) {
LayoutInflater vi = (LayoutInflater) getContext().getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
view = vi.inflate(R.layout.storelist_rowview, null);
}
final Store store = stores.get(position);
if (store != null) {
LinearLayout storeLogoContainer = (LinearLayout) view.findViewById(R.id.storeLogoContainer);
LoaderImageView imageView = new LoaderImageView(getContext(), getContext().getString(R.string.logoUrl), store.getId().getId());
storeLogoContainer.addView(imageView);
TextView storeName = (TextView) view.findViewById(R.id.storeName);
storeName.setText(store.getStoreName());
}
return view;
}
}
答案 0 :(得分:3)
当我启动ListActivity时,它会自动打开KeyBoard,因为当前焦点在EditText上。我想阻止它。
然后将焦点放在其他地方。
多次调用当焦点转到EditText时,会自动调用ArrayAdapter的getView()方法,这会再次生成所有行。
getView()
。它可能是也可能不是“再次生成所有行”。只需确保您的getView()
实施效率很高。
如何预防?
你没有。只需确保您的getView()
实施效率很高。
答案 1 :(得分:1)
当我们使用ArrayAdapter通过一组对象提供TextView时,ARE会自动调用toString()将数组对象转换为需要在textview中显示的文本。 但是要使用TextViews之外的其他东西进行数组显示,比如ImageViews,或者除了toString()之外还有一些数据填充视图,我们覆盖getView(int,View,ViewGroup)以返回你想要的视图类型。它由RE ..自动调用。