ArrayAdapter 的 getView() 方法在 EditText 焦点事件上自动调用
我正在创建自定义列表活动。有两个类,一个类具有扩展的 ListActivity,另一个类具有用于该列表活动的 ArrayAdapter。
问题 1: 问题是,在打开 ListActivity 屏幕时,我在其顶部有搜索框。当我启动 ListActivity 时,它会自动打开键盘,因为当前焦点位于 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;
}
}
I am creating the custom list activity. There are two classes one is having extended ListActivity and other is having ArrayAdapter for that list activity.
Problem 1:
The problem is that while opening the ListActivity screen i have Search box on top of it. When i start the ListActivity it automatically opens the KeyBoard as current focus is on EditText. I want to stop that.
Problem 2:
Also when focus goes on EditText the ArrayAdapter's getView() method is getting called automatically which is generating the all rows again. I am not getting why it happens so. I am not calling notifyDataSetchanged() method also. Still when i focus on EditText then getView() is called automatically. How to prevent it?
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;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
那么,把焦点放在其他事情上吧。
getView()
被调用很多次。它可能会也可能不会“再次生成所有行”。只需确保您的getView()
实现高效即可。你不知道。只需确保您的
getView()
实现高效即可。Give something else the focus, then.
getView()
is called lots of times. It may or may not be "generating all rows again". Simply make sure yourgetView()
implementation is efficient.You don't. Simply make sure your
getView()
implementation is efficient.当我们使用 ArrayAdapter 通过对象数组向 TextView 提供数据时,ARE 会自动调用 toString() 将数组对象转换为需要在 textview 中显示的文本。
但是,如果要使用 TextView 之外的其他内容(例如 ImageView)来进行数组显示,或者要让除了 toString() 结果之外的一些数据填充视图,我们需要重写 getView(int, View, ViewGroup) 以返回所需的视图类型。它由 RE 自动调用。
when we use ArrayAdapter for feeding a TextView by an array of objects, ARE automatically calls toString() to convert the array object into the text that needs to be displayed in the textview..
But To use something other than TextViews for the array display, for instance, ImageViews, or to have some of data besides toString() results fill the views,we override getView(int, View, ViewGroup) to return the type of view you want. It is called automatically by RE..