2011-07-01 92 views
0

我正在創建自定義列表活動。有兩個類是擴展ListActivity的,另一個是具有ArrayAdapter的列表活動。在EditText焦點事件上自動調用ArrayAdapter的getView()方法

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

}

回答

3

當我啓動ListActivity時,它會自動打開KeyBoard,因爲當前焦點位於EditText上。我想阻止它。

然後給別的焦點。

另外,當焦點進入EditText時,ArrayAdapter的getView()方法會自動調用,它將再次生成所有行。

getView()被稱爲很多次。它可能會或可能不會「再次生成所有行」。只需確保您的getView()實施效率高。

如何預防?

你不知道。只需確保您的getView()實施效率高。

+0

getView()被調用很多次。它可能會或可能不會「再次生成所有行」。只要確保你的getView()實現是高效的。'你能告訴我有什麼方法可以從getView()被調用的地方知道嗎?我只是想調用getView(),當我添加一些元素,並notifyDataSetChanged()。 – vbjain

+0

實際上,您可以在getView()中看到,有一個圖像視圖正在從服務器獲取圖像。現在每次如果getView()將被調用,那麼它將每次從服務器獲取圖像。這在應用中是不可接受的。 – vbjain

+0

@vbjain:「有沒有什麼方法可以知道getView()被調用的地方?」 - 沒有。 「我只是想調用getView(),當我添加一些元素,並notifyDataSetChanged()。」 - 你永遠不會調用getView(),並且調用getView()時不是你控制的東西。 – CommonsWare

1

當我們使用ArrayAdapter通過對象數組提供TextView時,ARE會自動調用toString()將數組對象轉換爲需要在文本視圖中顯示的文本。 但要使用除TextView之外的其他內容對於數組顯示,例如ImageViews,或者除了toString()結果之外還有一些數據填充視圖,我們重寫getView(int,View,ViewGroup)以返回所需的視圖類型。它被RE自動調用。

相關問題