簡體   English   中英

滾動列表時,CheckBox會在ListView中自動檢查

[英]CheckBox checked automatically in ListView when scrolling the list

我在我的應用程序中使用ListView ,其中包含每個列表項中的文本框和CheckBox等元素。 當我填充第一個文本框時,第七個文本框或任何其他隨機文本框自動填充 相同的值 ,當我檢查 CheckBox時, CheckBox也會發生同樣的事情,任何其他隨機CheckBox也會自動檢查

我無法弄明白為什么會發生這種情況。

我的代碼如下:

列表視圖內容

<!--- add your MY problem code comment -->
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/hyList"
    android:layout_width="fill_parent"
    android:layout_height="35sp"
    android:layout_gravity="top"
    android:orientation="horizontal" >

    <TableLayout
        android:id="@+id/tbltab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="100" >    
        <TableRow
            android:id="@+id/rowfooter"
            android:layout_width="fill_parent"
            android:layout_height="35sp"
            android:gravity="center_horizontal|bottom" >    
            <TextView
                android:id="@+id/txt_Hy_ID"
                android:layout_width="0sp"
                android:layout_height="35sp"
                android:textSize="1pt" >
            </TextView>    
            <TextView
                android:id="@+id/txt_HyID"
                android:layout_width="0sp"
                android:layout_height="35sp"
                android:textSize="1pt" >
            </TextView>    
            <TextView
                android:id="@+id/txt_Hy_Area"
                android:layout_width="200sp"
                android:layout_height="35sp"
                android:layout_gravity="center_vertical"
                android:gravity="left"
                android:textSize="8pt" >
            </TextView>    
            <CheckBox
                android:id="@+id/chkHyCondition"
                android:layout_width="60sp"
                android:layout_height="35sp"
                android:layout_gravity="center"
                android:saveEnabled="true" />    
            <EditText
                android:id="@+id/editTxtHyRemarks"
                android:layout_width="120sp"
                android:layout_height="35sp"
                android:layout_gravity="center_vertical|left"
                android:gravity="center_vertical|left"
                android:maxLength="25"
                android:saveEnabled="true"
                android:textSize="8pt" >
            </EditText>
        </TableRow>
    </TableLayout>    
</LinearLayout>

和Listview

<ListView
   android:id="@+id/ListViewHy"
   android:layout_width="420sp"
   android:layout_height="100sp"
   android:layout_column="0"
   android:layout_span="3"
   android:clickable="true"
   android:saveEnabled="true"
   android:scrollbarSize="10sp"
   android:scrollbars="vertical" >
   </ListView>

我用它來綁定我的listview:

private void FillGridHygiene() {
    LstViewHy = (ListView) findViewById(R.id.ListViewHy);
    clsDatabase dbh = new clsDatabase(this);
    dbh.openDataBase();
    Cursor cursor;
    cursor = dbh.getGridData("030");
    dbh.close();
    if (cursor != null) {
        int cnt = cursor.getCount();
        if (cnt > 0) {
            startManagingCursor(cursor);
            try {
                // -----------BindingListView----------------------------------------------------------------------------

                SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
                        R.layout.hygiene_list, cursor, new String[] {
                                Audit_FSD_Tab.KEY_ROW_ID,
                                Audit_FSD_Tab.KEY_ID,
                                Audit_FSD_Tab.KEY_SHORT_NAME }, new int[] {
                                R.id.txt_Hy_ID, R.id.txt_HyID,
                                R.id.txt_Hy_Area });
                adapter.setViewResource(R.layout.hygiene_list);
                LstViewHy.setAdapter(adapter);
                LstViewHy.setTextFilterEnabled(true);
                LstViewHy.setFocusable(false);
                LstViewHy.setVisibility(View.VISIBLE);

            } catch (Exception ex) {
                ex.fillInStackTrace();
            }
        }

    }
}

編輯:1

API級別 - 7版本 - 2.1

問題在於列表視圖的設計。 您需要創建自定義適配器,並在視圖持有者和convertview的幫助下回收您的視圖。 Listview 列表視圖使用baseadapter

這將幫助您理解ListView概念並給出一個想法 - 其他隨機文本框自動填充相同的值,當我選中復選框時,復選框也會發生相同的任何其他隨機復選框也會自動檢查。 希望這可以幫助您並幫助您設計列表視圖

正如另一個所說,你必須制作一個自定義Adapter並正確設置/恢復bindView每一行的值(對於CheckBox / EditText)。 下面是一個存儲狀態的Adapter的簡單示例:

public class CustomAdapter extends SimpleCursorAdapter {

            // this will hold a mapping between the row ids and a special object that  will store the values for the EditText and CheckBox
    private HashMap<Long, StateSaver> status = new HashMap<Long, StateSaver>();

    public CustomAdapter(Context context, int layout, Cursor c,
            String[] from, int[] to) {
        super(context, layout, c, from, to);
    }

    @Override
    public void bindView(View view, Context context, Cursor cursor) {
        super.bindView(view, context, cursor);
                    // get th id of the this row so we can use it as an identifier
        long theId = cursor.getLong(cursor.getColumnIndex("_id"));
        CheckBox ckb = (CheckBox) view.findViewById(R.id.checkBox1);
        ckb.setTag(new Long(theId));
                    // store the status of the CheckBox when it changes
        ckb.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView,
                    boolean isChecked) {
                Long theRealId = (Long) buttonView.getTag();
                StateSaver obj = status.get(theRealId);
                if (obj == null) {
                    obj = new StateSaver();
                    status.put(theRealId, obj);
                }
                obj.checked = isChecked;
            }
        });
        EditText et = (EditText) view.findViewById(R.id.editText1);
        et.setTag(new Long(theId));
                    // when the EditText losses focus it's time to store the value entered by the user
        et.setOnFocusChangeListener(new OnFocusChangeListener() {

            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if(!hasFocus) {
                    Long theRealId = (Long) v.getTag();
                    StateSaver obj = status.get(theRealId);
                    if (obj == null) {
                        obj = new StateSaver();
                        status.put(theRealId, obj);
                    }
                    obj.currentInput = ((EditText) v).getText().toString();
                }
            }
        });
        StateSaver sv = status.get(theId);
        // setup the row with the correct data, either the default or what you've previously stored
        if (sv == null) {
            ckb.setChecked(false);
            et.setText("");
        } else {
            ckb.setChecked(sv.checked);
            et.setText(sv.currentInput);
        }

    }

    private class StateSaver {
        boolean checked = false;
        String currentInput = "";
    }

}

此外,您還需要為包含ListViewActivity設置android:windowSoftInputMode="adjustPan"

我的建議是避免在ListView行中使用EditText 另外,嘗試改進你的行布局: sp單位應僅用於文本大小(在任何其他地方使用dp ),不要使用pt單位,看看你是否真的需要layout_weight屬性等。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM