title: Fragment ListView Inflation to Maintain onItemClick Selection inoperative
body: <p>I have a <code>ListView</code> that is within a Fragment. In the <code>onCreateView</code> section I have set a <code>onItemClickListener</code> for the list, which highlights the selected item in the <code>ListView</code>. I have set two <code>ImageButtons</code> that navigate up and down the list. On selection a new <code>Row</code> is inflated (<code>R.layout.selected_row</code>) that has its <code>TextView</code>'s set to the content of the select item (for the purpose of retaining the highlighted selected state). Unfortunately, upon clicking an item and/or clicking an <code>ImageButton</code> nothing appears to be inflated (nor any noticeable change on the list occurs). I believe that it may have something to do with the adapter <code>SelectedAdapter</code> construction, IE:</p>
  
  <pre><code> public SelectedAdapter(Context context, int textViewResourceId, List objects) {
              super(getActivity(), R.layout.listview_routines, routines);
          }
  </code></pre>
  
  <p>However, I don't see how that would prevent something (the inflation) from taking place. Thanks in advance for any input or advice to help resolve this issue.</p>
  
  <p>Fragment:</p>
  
  <pre><code>    public static class FragmentRoutine extends Fragment {
      DatabaseHandler db;
      private ListView routineListView;
      private List&lt;Routine&gt; routines = new ArrayList&lt;Routine&gt;();
      ArrayAdapter&lt;Routine&gt; 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&lt;?&gt; 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);
              }
          });
  
          // 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(){
  
          int selectedPos = selectedAdapter.getSelectedPosition();
          Routine currentRoutine = routines.get(selectedPos);
          if (selectedPos &gt; 0 ){
              routines.remove(selectedPos);
              String str = currentRoutine.getTagName();
              routines.add(selectedPos-1, currentRoutine);
              // set selected position in the adapter
              selectedAdapter.setSelectedPosition(selectedPos-1);
          }
      }
  
      // Move selected item "down" in the ViewList.
      private void moveDown(){
  
          int selectedPos = selectedAdapter.getSelectedPosition();
          Routine currentRoutine = routines.get(selectedPos);
          if (selectedPos &lt; routines.size()-1 ){
              routines.remove(selectedPos);
              String str = currentRoutine.getTagName();
              routines.add(selectedPos+1, currentRoutine);
              // set selected position in the adapter
              selectedAdapter.setSelectedPosition(selectedPos+1);
          }
      }
  
      public class SelectedAdapter extends ArrayAdapter&lt;Routine&gt;{
  
          // 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) {
                  v = getActivity().getLayoutInflater().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){
                  v.setBackgroundColor(Color.CYAN);
              }else{
                  v.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&lt;Routine&gt; {
          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;
          }
  
      }
  }
  </code></pre>
  
  <p>Additional Info:</p>
  
  <p>The adapter mentioned above (<code>SelectedAdapter</code>) does require me to pass <code>routines.add(selectedPos+1, currentRoutine);</code> in place of <code>routines.add(selectedPos-1, str);</code> which I believe the most direct way to get the <code>String</code> value to the new <code>Row</code>. Unsure if this is a contributor. </p>