ArrayAdapter's getView() method getting called automatically on EditText focus event

纵饮孤独 提交于 2019-12-21 23:14:05

问题


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;
}

}


回答1:


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.




回答2:


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..



来源:https://stackoverflow.com/questions/6551855/arrayadapters-getview-method-getting-called-automatically-on-edittext-focus-e

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!