custom adapter and highlighting a row [SOLVED]

Put your problem here if it does not fit any of the other categories.

custom adapter and highlighting a row [SOLVED]

Postby timcs » Mon Jul 29, 2013 9:24 pm

Hi

I have been having difficulty in getting a single row to be highlighted . The list is folders and files and I only want a single file to be highlighted.

My activity is a ListActivity and the layout(s) consist of a layout for the main display which includes a listview and then I have a separate row.xml to represent the folders or files . The main structure of this activity and its layouts is based on this tutorial : http://android-er.blogspot.co.uk/2012/07/example-of-file-explorer-in-android.html

the only difference is that I am trying to get a single file to be highlighted and the way I was directed in was a custom adapter. However I cannot get the highlighter to work correctly and quite often stays highlighted against a folder after moving back from a sub folder and before hand had selected a file within. the customer adapter code is as follows :


Code: Select all
public class FileListAdapter extends ArrayAdapter<String>  {

       private List<String> itemsll;
            

         public FileListAdapter(Context context, int row,
               List<String> iteml) {
            super(context,row,iteml);
            // TODO Auto-generated constructor stub
            this.itemsll = iteml;
         }

         @Override
         public View getView(int position, View convertView, ViewGroup parent) {
            
            View viewnull = convertView;
            if (viewnull == null) {
               LayoutInflater vrow;
               vrow = LayoutInflater.from(getContext());
               
               viewnull = vrow.inflate(R.layout.row, null);
         
            }
            String currow =    itemsll.get(position);
            TextView rowtext = (TextView)viewnull.findViewById(R.id.rowtext);
            rowtext.setText(currow);
            
            if (currpos == position) {
               ((TextView)viewnull).setBackgroundColor(Color.BLUE);
               
                } else {
                   ((TextView)viewnull).setBackgroundColor(Color.TRANSPARENT);
                }
            
            
            return viewnull;   
            
            
         }
         
         

         

         public void setSelectedPosition( int pos )
          {
              currpos = pos;

              notifyDataSetChanged();
          }
         
         
         
         
          
        }




and I am currently using onListItemClick which is used to check if the selected row is a folder or file :

Code: Select all
@Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
     // TODO Auto-generated method stub
     File fileci = new File(pathi.get(position));


     if (fileci.isDirectory())
     {

      if(fileci.canRead()){

       getdiri(pathi.get(position));
      
       ((TextView)v).setBackgroundColor(Color.TRANSPARENT);
       oki.setEnabled(false);
      }else{
      new AlertDialog.Builder(Importfile.this)
        .setIcon(R.drawable.quitapp)
        .setTitle("[" + fileci.getName() + "] Cannot be accessed! Please try another folder")
        .setPositiveButton("OK", null).show();
      
      }
     }    else {
          int selpos = position;
          fl.setSelectedPosition(selpos);
    
    }
   


Thanks in Advance

TimCS


**EDIT***

Sorted this by using getListView() method to create a listview object as this is a ListActivity and then using the listview object set up OnItemClick and used the customadapter to allow me to slelect the row I need. I now only have one problem and that is to deselect a row but I aim to solve that hopefully by myself :)
timcs
Freshman
Freshman
 
Posts: 2
Joined: Thu Jan 12, 2012 8:33 pm

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: Google [Bot] and 18 guests