ArrayAdapter的getView()方法获取有关的EditText焦点事件自动调用 [英] ArrayAdapter's getView() method getting called automatically on EditText focus event
问题描述
我创建自定义列表活动。有两类之一具有延伸ListActivity和其他由具有ArrayAdapter为该列表活性。
问题1: 问题是,当打开ListActivity屏幕我有搜索框,在它上面。当我开始ListActivity就自动打开键盘作为目前的重点是的EditText 。我要停止。
问题2: 此外,当焦点推移的EditText一个ArrayAdapter的 getView()方式是越来越自动调用其再次发生的所有行。我没有得到为什么会发生如此。我不是叫 notifyDataSetchanged()方式也。不过当我专注于的EditText然后getView()被自动调用。如何prevent呢?
storelistview.xml:
? < XML版本=1.0编码=UTF-8>< LinearLayout中的xmlns:机器人=http://schemas.android.com/apk / RES /安卓
机器人:背景=@彩色/黑白机器人:方向=垂直
机器人:layout_width =FILL_PARENT机器人:layout_height =FILL_PARENT
机器人:可聚焦=真正的机器人:focusableInTouchMode =真正的>
< TableLayout的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:方向=垂直机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT>
<的TableRow机器人:ID =@ + ID / tableRow2机器人:layout_height =WRAP_CONTENT
机器人:重力=中心的Android:layout_width =FILL_PARENT
机器人:后台=#919191>
< EditText上机器人:ID =@ + ID / searchText机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT机器人:宽=240dp
机器人:提示=搜索>< /的EditText>
< ImageView的机器人:layout_width =WRAP_CONTENT
机器人:SRC =@可绘制/搜索机器人:layout_height =WRAP_CONTENT
机器人:ID =@ + ID / searchImage>< / ImageView的>
< /的TableRow>
< / TableLayout>
< ListView的机器人:ID =@ + ID / Android的:清单机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT/>
< TextView的机器人:ID =@ + ID /安卓:空
机器人:layout_width =FILL_PARENT机器人:layout_height =FILL_PARENT
机器人:文字颜色=@色/白机器人:TEXTSIZE =14sp
机器人:重力=顶|中心的Android版本:文本=没有存储中找到。/>< / LinearLayout中>
StoreListView.java:
公共类StoreListView扩展ListActivity {
私人列表<店>商店=新的ArrayList<店>();
私人StoreListRowAdapter rowAdapter;
私人的Runnable fetchStores;
私人处理程序处理程序=新的处理程序();
私人ImageView的searchImage;
私人的EditText searchText;
@覆盖
保护无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.storelistview);
searchText =(EditText上)findViewById(R.id.searchText);
searchImage =(ImageView的)findViewById(R.id.searchImage);
searchImage.setOnClickListener(新View.OnClickListener(){
@覆盖
公共无效的onClick(视图v){
}
}
});
rowAdapter =新StoreListRowAdapter(这一点,R.layout.storelistview,商店);
setListAdapter(rowAdapter);
fetchStores =新的Runnable(){
@覆盖
公共无效的run(){
店中店=新店();
StoreListAsynTask S =新StoreListAsynTask();
s.execute(店);
}
};
handler.post(fetchStores);
}
私人可运行resultThread =新的Runnable(){
公共无效的run(){
rowAdapter.clear();
的for(int i = 0; I< stores.size();我++){
商店豆= stores.get(ⅰ);
rowAdapter.add(豆);
}
//rowAdapter.notifyDataSetChanged();
}
};
类StoreListAsynTask扩展的AsyncTask<店,太虚,字符串> {
私人GSON GSON =新GSON();
私人列表<店> storeList;
私人ProgressDialog m_ProgressDialog = NULL;
@覆盖
保护字符串doInBackground(店... PARAMS){
返回respData;
}
@覆盖
保护无效onPostExecute(字符串结果){
//填充商店名单
商店= storeList;
runOnUiThread(resultThread);
m_ProgressDialog.dismiss();
}
}
}
StoreListRowAdapter.java:
公共类StoreListRowAdapter扩展ArrayAdapter<店> {
名单<店>专卖店;
公共StoreListRowAdapter(上下文的背景下,INT textViewResourceId,列表和LT;店>店){
超(背景下,textViewResourceId,专卖店);
this.stores =专卖店;
}
@覆盖
公共查看getView(INT位置,查看convertView,ViewGroup中父){
查看查看= convertView;
如果(查看== NULL){
LayoutInflater VI =(LayoutInflater)的getContext()。getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
鉴于= vi.inflate(R.layout.storelist_rowview,NULL);
}
最后店中店= stores.get(位置);
如果(店!= NULL){
的LinearLayout storeLogoContainer =(的LinearLayout)view.findViewById(R.id.storeLogoContainer);
LoaderImageView的ImageView =新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());
}
返回查看;
}
}
当我开始ListActivity就自动打开键盘作为目前的重点是的EditText。我要停止。
给别的东西的焦点,那么。
此外,当焦点推移的EditText一个ArrayAdapter的getView()方法得到自动调用其再次发生的所有行。
getView()
被称为很多次。它可能是也可能不是产生的所有行再次。只要确保你的 getView()
的实施是有效的。
如何prevent呢?
您不知道。只要确保你的 getView()
的实施是有效的。
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;
}
}
When i start the ListActivity it automatically opens the KeyBoard as current focus is on EditText. I want to stop that.
Give something else the focus, then.
Also when focus goes on EditText the ArrayAdapter's getView() method is getting called automatically which is generating the all rows again.
getView()
is called lots of times. It may or may not be "generating all rows again". Simply make sure your getView()
implementation is efficient.
How to prevent it?
You don't. Simply make sure your getView()
implementation is efficient.
这篇关于ArrayAdapter的getView()方法获取有关的EditText焦点事件自动调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!