开发者

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

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:textCol开发者_StackOverflow中文版or="@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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜