2013-10-03 21 views
1

我工作的一個Android應用程序,我想實現一個列表視圖,每排4個內容EditTexts。 主要目標是編寫EditText,然後將它們保存在列表中。 我這裏https://code.google.com/p/android/issues/detail?id=21105 遇到了同樣的問題,當我點擊了三個第一要素和三節結束它完美,但是當我點擊第5個元素,例如,(如果我有我的名單上的22個項目),我失去了重點,我不得不滾動另一次來找到我點擊的地方。 我創建一個子類從ListView的延伸,在這裏我的GUI:的ListView與edittexts

<ScrollView 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:fillViewport="true" > 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:orientation="vertical" > 

      <AutoResizingListView 
       android:id="@+id/listViewProduits" 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent" 
       android:background="@color/background_color" /> 
     </LinearLayout> 
    </ScrollView> 

子類:

import android.content.Context; 
import android.database.DataSetObserver; 
import android.util.AttributeSet; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.ListAdapter; 
import android.widget.ListView; 

/** 
* 
*/ 
public class AutoResizingListView extends ListView { 
    public AutoResizingListView(Context context) { 
     super(context); 
    } 

    public AutoResizingListView(Context context, AttributeSet attrs) { 
     super(context, attrs); 
    } 

    public AutoResizingListView(Context context, AttributeSet attrs, int defStyle) { 
     super(context, attrs, defStyle); 
    } 

    @Override 
    public void clearChildFocus(View child) { 
     super.clearChildFocus(child); 
     if (getSelectedItemPosition() != -1 && getItemsCanFocus()) { 
      requestFocus(); 
     } 
    } 


    @Override public void setAdapter(final ListAdapter adapter) { 
     super.setAdapter(adapter); 

     adapter.registerDataSetObserver(new DataSetObserver() { 
      @Override public void onChanged() { 
       setHeight(); 
      } 
     }); 

     setHeight(); 
    } 

    private void setHeight() { 
     ListAdapter adapter = getAdapter(); 
     int totalHeight = 0; 
     for (int i = 0; i < adapter.getCount(); i++) { 
      View listItem = adapter.getView(i, null, this); 
      listItem.measure(0, 0); 
      totalHeight += listItem.getMeasuredHeight(); 
     } 

     ViewGroup.LayoutParams params = this.getLayoutParams(); 
     int dividerHeights = getDividerHeight() * (adapter.getCount() == 0 ? 0 : adapter.getCount() - 1); 
     params.height = totalHeight + dividerHeights; 
     setLayoutParams(params); 
    } 
} 

問題是什麼?

+0

如果你知道有多少的TextView您將使用列表視圖不是ONY解決方案。您也可以在.xml中放置佈局,並在運行時以編程方式添加textview =) –

+0

我們不知道我們將使用多少個Edittext。我做了什麼ü說,但它不工作 – user2838231

回答

0

嘗試......

 ListView list = (ListView) findViewById(R.id.yourlistview); 
    String[] from = new String[] {"col_1","col_2","col_3","col_4"}; 
     int[] to = new int[] { R.id.ed_item1, R.id.ed_item2, R.id.ed_item3, R.id.ed_item4 }; 

     // prepare the list of all records 
     ArrayList<HashMap<String, String>> fillMaps = new ArrayList<HashMap<String, String>>(); 
     int cnt=1; 
     for(int i = 0; i < seleced_item.length(); i++){ 
      HashMap<String, String> map = new HashMap<String, String>(); 
      map.put("col_1",""); 
      map.put("col_2",""); 
      map.put("col_3",""); 
      map.put("col_4",""); 

      } 
      fillMaps.add(map); 
      cnt++; 
     } 

     // fill in the grid_item layout contains edit boxes with linear layout. 
     SimpleAdapter adapter = new SimpleAdapter(this, fillMaps, R.layout.Edit_view, from, to); 
     list.setAdapter(adapter); 

在grid_item佈局,可以設置請求焦點每個編輯文本元素。

+0

感謝烏拉圭回合的反應快,我需要做一個getview因爲我必須每個值存儲在一個對象中的每個editext。你想要整個代碼嗎? – user2838231