片段ListView新行的通货膨胀以维护onItemClick选择

时间:2015-04-22 01:35:25

标签: android android-fragments android-listview android-adapterview onitemclick

我的ListView位于Fragment之内。在onCreateView部分,我为列表设置了onItemClickListener,其中突出显示了ListView中的所选项目。我设置了两个在列表中上下导航的ImageButtons。在选择时,新的Row被夸大,其TextView被设置为选择项的内容(为了保留突出显示的选定状态)。但是我很难将该项目添加回列表。该应用无法编译,因为它想要的行routines.add(selectedPos-1, str); int locationRoutine object。我认为问题出在我的SelectedAdapter课程的构建上,但是我无法确定关于Object的更改/传递内容。 IE:

public SelectedAdapter(Context context, int textViewResourceId,List objects) {           
         super(getActivity(), R.layout.listview_routines, routines); }

我非常感谢如何纠正这个问题;以及任何建议,如果有更好的方法来维持一个选定的州。谢谢你的帮助。

片段:

 public static class FragmentRoutine extends Fragment {
    DatabaseHandler db;
    private ListView routineListView;
    private List<Routine> routines = new ArrayList<Routine>();
    ArrayAdapter<Routine> routineAdapter;
    Routine longClickedItemRoutines;
    private SelectedAdapter selectedAdapter;

    public FragmentRoutine() {}

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.routines,
                container, false);
        db = new DatabaseHandler(getActivity().getApplicationContext());
        routineListView = (ListView) rootView.findViewById(R.id.routineList);
        registerForContextMenu(routineListView);
        db.closeDB();
        if (db.getExerciseCount() != 0)
            routines.clear();
        routines.addAll(db.getAllRoutines());
        populateList();
        selectedAdapter = new SelectedAdapter(this.getActivity(), 0, routines);
        selectedAdapter.setNotifyOnChange(true);
        routineListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                longClickedItemRoutines = routines.get(position);
                return false;
            }
        });
        routineListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView arg0, View view,
                                    int position, long id) {

                selectedAdapter.setSelectedPosition(position);
            }
        });
        routineListView.post(new Runnable() {

            @Override
            public void run() {
                routineListView.setItemChecked(0, true);
            }
        });

        // move up event handler
        ImageButton btnMoveUp = (ImageButton) rootView.findViewById(R.id.btnMoveUp);
        btnMoveUp.setOnClickListener(new AdapterView.OnClickListener() {
            public void onClick(View arg0) {
                moveUp();
            }
        });

        // move down event handler
        ImageButton btnMoveDown = (ImageButton) rootView.findViewById(R.id.btnMoveDown);
        btnMoveDown.setOnClickListener(new AdapterView.OnClickListener() {
            public void onClick(View arg0) {
                moveDown();
            }
        });

        setHasOptionsMenu(true);
        return rootView;
    }

    // Move selected item "up" in the ViewList.
    private void moveUp(){
        Routine currentToDoSave = routines.get(selectedAdapter.getSelectedPosition());
        int selectedPos = selectedAdapter.getSelectedPosition();
        if (selectedPos > 0 ){
            routines.remove(selectedPos);
            String str = currentToDoSave.getTagName();
            //Problem Line Below
            routines.add(selectedPos-1, str);
            // set selected position in the adapter
            selectedAdapter.setSelectedPosition(selectedPos-1);
        }
    }

    // Move selected item "down" in the ViewList.
    private void moveDown(){
        Routine currentToDoSave = routines.get(selectedAdapter.getSelectedPosition());

        int selectedPos = selectedAdapter.getSelectedPosition();
        if (selectedPos < routines.size()-1 ){
            routines.remove(selectedPos);
            String str = currentToDoSave.getTagName();
            routines.add(selectedPos+1, str);
            // set selected position in the adapter
            selectedAdapter.setSelectedPosition(selectedPos+1);
        }
    }

    public class SelectedAdapter extends ArrayAdapter<Routine>{

        // used to keep selected position in ListView
        private int selectedPos = -1;   // init value for not-selected

        public SelectedAdapter(Context context, int textViewResourceId,
                               List objects) {
            super(getActivity(), R.layout.listview_routines, routines);
        }

        public void setSelectedPosition(int pos){
            selectedPos = pos;
            // inform the view of this change
            notifyDataSetChanged();
        }

        public int getSelectedPosition(){
            return selectedPos;
        }

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

            // only inflate the view if it's null
            if (v == null) {
                LayoutInflater vi
                        = (LayoutInflater)this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                v = vi.inflate(R.layout.selected_row, null);
            }

            // get text view
            TextView label = (TextView)v.findViewById(R.id.txtExample);

            // change the row color based on selected state
            if(selectedPos == position){
                label.setBackgroundColor(Color.CYAN);
            }else{
                label.setBackgroundColor(Color.WHITE);
            }

            label.setText(this.getItem(position).toString());
            return(v);
        }
    }

    private void populateList() {
        routineAdapter = new SaveListAdapterT();
        routineListView.setAdapter(routineAdapter);
    }

    public class SaveListAdapterT extends ArrayAdapter<Routine> {
        public SaveListAdapterT() {
            super(getActivity(), R.layout.listview_routines, routines);
        }

        @Override
        public View getView(int position, View view, ViewGroup parent) {
            if (view == null)
                view = getActivity().getLayoutInflater().inflate(R.layout.listview_routines, parent, false);

            Routine currentToDoSave = routines.get(position);

            TextView name = (TextView) view.findViewById(R.id.name);
            name.setText(currentToDoSave.getTagName());

            return view;
        }

    }
}

0 个答案:

没有答案