带有edittexts的ListView

时间:2013-10-03 07:11:41

标签: android listview focus android-edittext

我正在开发一个Android应用程序,我想实现一个listview,每行内容4 EditTexts。 主要目标是在EditText中写入,然后将它们保存在列表中。 我在这里遇到了同样的问题https://code.google.com/p/android/issues/detail?id=21105 当我点击三个第一个元素和三个结尾时,它完美地工作,但是当我点击第五个元素时,例如(如果我的列表中有22个项目),我失去了焦点,我必须滚动另一个时间到找到我点击的点。 我创建了一个从ListView扩展的Subclass,这里是我的GUI:

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

有什么问题?

1 个答案:

答案 0 :(得分: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布局中,您可以将请求焦点设置在每个编辑文本元素上。