ON/OFF Arrayadapter 的 getView() 方法调用中的切换按钮 [使用 SQLite table 值] 来调用列表视图?

ON/OFF Switch button [using SQLite table values] in Arrayadapter's getView() method call for a Listview?

问题: 到 enable/disable Listview 上的切换按钮加载关于显示的每一行的 table 中存储的“是”和“否”值.在这个问题上花了一个星期。 SO 也没有适当的解决方案来为 getView() 方法显示的项目行启用 ON/OFF 的开关值。

到目前为止,已完成: 从 (SQLite Table) 中查询了所有值为“是”的项目的 ICODE,并添加到单独的 Arraylist 中,并为所有“是”中的值,试图 enable/disable 在列表视图的相应行中切换 [Arrayadapter 实现]。


import java.util.ArrayList;
import java.util.HashMap;
import java.util.Locale;


public class Ecs extends ListActivity {

   public static ArrayList<String> firstList ;
   public static ArrayList<String> swlist ;
   TextView searchtext;
   Con control = new Con(this);

}
public static class Apache extends ArrayAdapter<HashMap<String, String>> {

        public static final String DN = "DN";// 0 text(String)
      
        public static ArrayList<String> noon ;

        public ArrayList<HashMap<String, String>> firstList;
        Activity activity;
 
        public Apache(Activity activity, ArrayList<HashMap<String, String>> firstList) {
            super(activity, R.layout.simple, firstList);
            this.activity = activity;
            this.firstList = firstList;

        }

        @Override
        public int getCount() {
            return firstList.size();
        }

        @Override
        public HashMap<String, String> getItem(int position) {
            return Ecs.firstList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            final HashMap<String, String> map = firstList.get(position);
            final ItemViewHolder viewHolder;
            final int pos = position;

            if (convertView == null) {

                LayoutInflater layoutInflater = (LayoutInflater)
this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

                convertView = layoutInflater.inflate(R.layout.fisrtlay, parent, false);
                viewHolder = new ItemViewHolder();
                
                viewHolder.TDD = (TextView) 
 convertView.findViewById(R.id.txtdnno);             
                viewHolder.SWW = (Switch) convertView.findViewById(R.id.sw);                       
                convertView.setTag(viewHolder);

            } else {

                viewHolder = (ItemViewHolder) convertView.getTag();

            }

            viewHolder.TDD.setText(map.get(DN));          
            viewHolder.SWW.setText(map.get(TYPE));
            viewHolder.SWW.setChecked(false);
            viewHolder.SWW.setTag("SWITCH");


            if (noon.contains(firstList.get(pos).get(ICODE))) {
                viewHolder.SWW.setChecked(true);             
            }

            viewHolder.SWW.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    String title = v.getTag().toString();
                    if (title.equals("SWITCH")) {

                        //if (viewHolder.SWW.getText().toString().trim().equals("Yes"))

                        if (viewHolder.SWW.isChecked()) {
                            noon.add(firstList.get(pos).get(ICODE));

                        } else {
                            if (noon.contains(firstList.get(pos).get(ICODE))) {
                                noon.remove(firstList.get(pos).get(ICODE));
                            }
                        }
                    }
                }
            });

        
            notifyDataSetChanged();           

            return convertView;
        }

 public class ItemViewHolder {

            TextView TDD;
            Switch SWW;
      
        }
    }
}

XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    xmlns:tools="http://schemas.android.com/tools"
    android:descendantFocusability="blocksDescendants"
    android:orientation="vertical" >

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

        <LinearLayout
            android:id="@+id/lvh"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#ffe6e6e6"
            android:minHeight="50dp"
            android:orientation="horizontal"
            android:scrollbarSize="5dp"
            android:scrollbars="horizontal"
            android:weightSum="5"
            android:focusable="true">

            <TextView
                android:id="@+id/txtdnno"
                android:layout_width="22dp"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_weight="1"
                android:gravity="center"
                android:padding="3dp"
                android:text="DNNO"
                android:textColor="#000000"
                android:textSize="12sp" />

            <RadioGroup
                android:layout_width="14dp"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_weight="1"
                android:gravity="center"
                android:orientation="vertical"
                android:padding="3dp">

                <Switch
                    android:id="@+id/sw"
                    android:layout_width="45dp"
                    android:layout_height="21dp"
                    android:layout_gravity="center"
                    android:focusable="false"
                    android:gravity="center"
                    android:onClick="switchClicked"
                    android:shadowColor="#FFFFFF"
                    android:textOff="@string/no"
                    android:textOn="@string/yes"
                    android:textSize="12sp" />

            </RadioGroup>    
        </LinearLayout>
    </LinearLayout>
</LinearLayout>

如果您的noonfirstList包含这样的数据

noon.add("123");
    
firstList.add(new HashMap<>().put(ICODE, "123"));
firstList.add(new HashMap<>().put(ICODE, "321"));
firstList.add(new HashMap<>().put(ICODE, "32"));

那么您的代码应该可以工作,对于第一个项目,它将是 Yes,对于其他 2 个 No

关注:

方法调用 [Yesvalues],它查询并保存来自 table 的 [swlist] 中的所有“Yes”值,并在用户与显示的 Listview 交互时添加未来“Yes”值的记录。

检查行位置 [pos],获取该行的 ICODE 并与 [swlist] 交叉检查到 enable/disable 该行的切换按钮。 getView() 方法将对每个行项目及其旁边的开关执行相同的操作。

当用户交互 [切换 OnClickListener / 保存 OnClickListener] 时,在保存时记录或删除“是”。

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Locale;


public class Ecs extends ListActivity {

   public static ArrayList<String> firstList ;
   public static ArrayList<String> swlist ;
   TextView searchtext;
   Con control = new Con(this);

private void DNat() {

        if (searchtext.getText().toString().length() > 3) {

            swlist = control.Yesvalues(searchtext.getText().toString());  /// STEP 1
            firstList = control.Sear(searchtext.getText().toString());

            if (myList.size() != 0) {

                lv = getListView();

                adapter = new Apache(Ecs.this, firstList, swlist);
                lv.setAdapter(adapter);

                control.close();

            } 
        }

    }
}


public static class Apache extends ArrayAdapter<HashMap<String, String>> {

        public static final String DN = "DN";// 0 text(String)
      
        public static ArrayList<String> noon ;

        public ArrayList<HashMap<String, String>> firstList;
        Activity activity;
 
        public Apache(Activity activity, ArrayList<HashMap<String, String>> firstList, ArrayList<HashMap<String, String>> swlist){
            super(activity, R.layout.simple, firstList);
            this.activity = activity;
            this.firstList = firstList;
            this.noon = swlist;  

        }
        
        @Override
        public int getCount() {
            return firstList.size();
        }

        @Override
        public HashMap<String, String> getItem(int position) {
            return Ecs.firstList.get(position);
        }
        
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            final HashMap<String, String> map = myList.get(position);
            final ItemViewHolder viewHolder;
            final int pos = position;
           

            if (convertView == null) {

                LayoutInflater layoutInflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

                convertView = layoutInflater.inflate(R.layout.simple, parent, false);
                viewHolder = new ItemViewHolder();
                 viewHolder.TDD = (TextView) convertView.findViewById(R.id.txtdnno);             
                viewHolder.SWW = (Switch) convertView.findViewById(R.id.sw);                       
                convertView.setTag(viewHolder);

            } else {

                viewHolder = (ItemViewHolder) convertView.getTag();

            }

 viewHolder.TDD.setText(map.get(DN));          
            viewHolder.SWW.setText(map.get(TYPE));
            viewHolder.SWW.setChecked(false);
            viewHolder.SWW.setTag("SWITCH");


            final HashMap<String, String> tyes = new HashMap<String, String>();  ///STEP 2
            tyes.put(DDN, myList.get(pos).get(DN));
            tyes.put(IICODE, myList.get(pos).get(ICODE));

            if (noon.contains(tyes) {
                viewHolder.SWW.setChecked(true);             
            }

            viewHolder.SWW.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    String title = v.getTag().toString();
                    if (title.equals("SWITCH")) {   /// STEP 3

                     if (viewHolder.SWW.isChecked()) {  

                           noon.add(tyes);

                        } else {
       
                              if (noon.contains(tyes) && viewHolder.SWW.getText().equals("No")) {
                            
                                noon.remove(tyes);
                            }
                        }
                    }
                }
            });

            
            notifyDataSetChanged();           

            return convertView;
        }

 public class ItemViewHolder {

            TextView TDD;
            Switch SWW;     

        }

    }

查询所有记录为YES的开关:

public class Con extends AppCompatActivity {

public Con() {}

public ArrayList<HashMap<String, String>> Yesvalues(String DN) {

        ArrayList<HashMap<String, String>> yeslist;
        String Yes="Yes";
        yeslist = new ArrayList<HashMap<String, String>>();
        int sval=Integer.parseInt(String.valueOf(DN));
        String selectQuery = "SELECT DN, ICODE FROM " + Tablename
                + " WHERE DN =" + sval + " AND STYPE = "  + "'"+ Yes +"'";
        SQLiteDatabase database = dbHelper.getWritableDatabase();
        Cursor cursor = database.rawQuery(selectQuery, null);
        if (cursor.moveToFirst()) {
            do {
                HashMap<String, String> yesmap = new HashMap<String, String>();
                yesmap.put(DNNO, cursor.getString(0));
                yesmap.put(ITEMCODE, cursor.getString(1));
                yeslist.add(yesmap);
            } while (cursor.moveToNext());
        }
        return yeslist;
    }
}