LinearLayout片段列表刷新

时间:2014-01-23 16:33:59

标签: android android-layout android-fragments android-linearlayout

这是我的片段类。每次“职员”改变时,

我尝试了几种方法。

  1. 触发重新创建整个活动。由于巨大的列表(为片段填充了200多个项目),这将导致几秒滞后。

  2. 我会使用findFragmentbyTag从主活动触发此片段上的displayList。

  3. 从mainActivity中删除所有childView并将其添加回来。

  4. 是否有任何其他解决方案(或上述解决方案中的哪一个),以便我可以“立即”以快速有效的方式“刷新”此列表(片段)使用多个linearlayout填充。

    只是添加我在V4片段和使用PagerAdapter从主要活动动态生成的片段。非常感谢!

    public class EncounterFragment extends Fragment {
    
        ListView list;
        TextView eid;
        TextView eclerkship;
        TextView ename;
        TextView etype;
        TextView erequiredattempts;
    
    
        // DATABASE ADAPTOR
        private eAndPDbAdapter encounterDB;
        private Context myContext = getActivity();
    
        //arrayAdapter
        ListAdapter adapter;
        CheckBox cBox = null; 
    
        //Button Btngetdata;
        ArrayList<HashMap<String, String>> encountersList = new ArrayList<HashMap<String, String>>();
    
        //various layout
        ListView encounterList;
    
        static String value;
        private EditText filterText = null;
    
        private  Cursor mCursor; 
        private Cursor cCursor; 
    
        String clerkShip = "null"; 
    
    
    
        //JSON Node Names 
        private static final String TAG_ENCOUNTERS = "encounters";
        private static final String TAG_E_ID = "e_id";
        private static final String TAG_E_CLERKSHIP = "clerkship";
        private static final String TAG_E_NAME = "encounter_name";  
        private static final String TAG_E_TYPE = "type";
        private static final String TAG_E_REQUIRED_ATTEMPTS = "required_attempts";
    
        //JSONArray android = null;
    
    
        private List<String> nameList = new ArrayList(); 
        private List<String> idList = new ArrayList(); 
        private List<String> typeList = new ArrayList(); 
        private List<String> countList = new ArrayList(); 
        public  List encounterSelected = new ArrayList();   
    
    
        public static final String ARG_SECTION_NUMBER = "section_number";
    
        public EncounterFragment() {
        }
    
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            myContext = getActivity();
    
            View rootView = inflater.inflate(R.layout.activity_encounter,
                    container, false);
    
               encountersList = new ArrayList<HashMap<String, String>>();
                displayList();
                LinearLayout parentLayout = (LinearLayout) rootView.findViewById(R.id.encounterslist);
    
    
                for(int i = 0 ; i<nameList.size();i++)
                {
                    View v = getLayoutInflater(savedInstanceState).inflate(R.layout.list_v, null);
                    final LinearLayout single = (LinearLayout) v.findViewById(R.id.listV);
                    single.setOnClickListener(new View.OnClickListener() {
    
                        @Override
                        public void onClick(View v) {
                            LinearLayout nestedLL  = (LinearLayout) single.getChildAt(1);
                            CheckBox cb = (CheckBox) nestedLL.getChildAt(0);
                            if(cb.isChecked())
                            {
                                cb.setChecked(false);
                                single.setBackgroundColor(Color.TRANSPARENT);
                            }
                            else
                            {
                                cb.setChecked(true);
                                single.setBackgroundColor(Color.rgb(255, 215, 0));;
                            }
                        }
                    });
                    TextView ID = (TextView) v.findViewById(R.id.eid);
                    TextView name = (TextView) v.findViewById(R.id.ename);
                    TextView count = (TextView) v.findViewById(R.id.eCount);
    
                    String nameValue = nameList.get(i).toString();
                    String IDValue = idList.get(i).toString();
                    String typeValue = typeList.get(i).toString(); 
                String countValue = countList.get(i).toString();
    
                    if (typeValue.equals("Must see"))
                    {
                        nameValue = nameValue + "**";
                    }
                    else
                    {
                        nameValue = nameValue + "*";
                    }
    
                    name.setText(nameValue);
                    ID.setText(IDValue);
                    count.setText(" (" + countValue + ")");
                    parentLayout.addView(v);
                }
    
    
    
            return rootView;
        }
    
        public void displayList()
        {
              displaySharedPreferences();
    
            encounterDB = new eAndPDbAdapter(myContext);
            encounterDB.open();
            mCursor = encounterDB.retrieveAllEncounterCursor(clerkShip);
            if ((mCursor != null) && (mCursor.getCount() > 0)) {
                mCursor.moveToFirst();
                {
                do {
                    String eID = mCursor.getString(1);
                    String encounter_name = mCursor.getString(3);
                    String encounter_type = mCursor.getString(4);
                    this.idList.add(eID);
                    this.nameList.add(encounter_name);
                    this.typeList.add(encounter_type);
                    //change to procedure if this is procedure 
                    cCursor = encounterDB.retrieveEncounterCounterBasedOnID(eID);
                    int count2 =cCursor.getCount();
                    if ((cCursor != null) && (count2 > 0)) {
                        cCursor.moveToFirst();
                        String NumCount = cCursor.getString(3);
                        this.countList.add(NumCount);
                    }
                    else
                    {
                        this.countList.add("0");
                    }
    
                }
    
                // move to the next row
                while (mCursor.moveToNext());
        }
            }
            encounterDB.close(); 
    
        }
    
    
    
    public void displaySharedPreferences() {
        SharedPreferences prefs = PreferenceManager
                .getDefaultSharedPreferences(myContext);
    
        String listPrefs = prefs.getString("listpref", "No Clerkship Selected");
    
        StringBuilder builder2 = new StringBuilder();
        builder2.append(listPrefs);
    clerkShip = builder2.toString();
    }   
    }
    

0 个答案:

没有答案