0

From this I asked about controlling and highlighting a row. One of the answers was based on a custom adapter and I was given a series of links on how to do this. As I am trying to get my head around doing this, I first hit a problem getting a null pointer exception as listed in my linked post. I soon worked out that the getview method was the cause of this and tried to resolve this by reading other posts here on this matter. So far I have stopped the error but now the listview is empty. My activity is extending a listview and my code is based on this article on creating a file explorer: File Explorer Link

In the GetDir method of this example I call the custom adapter and then in the getview I do this:

@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);

    }

    if (currpos == position) {
        ((TextView)viewnull).setBackgroundColor(Color.BLUE);
    }

    return viewnull;

}

As I do not intend to customize the actual view of the list, I just need to be able to list the folders and files and a allow for the code given in the other post to work but until I can get the list to work, I am unable to go any further.

EDIT The adapter is set up as follows:

 fl = new FileListAdapter(this,R.layout.row,itemi);

         setListAdapter(fl);

I did try and set up the text view within the getview like this:

TextView rowtext = (TextView)viewnull.findViewById(R.id.rowtext);

Edit 2 Entire adapter code:

 public class FileListAdapter extends ArrayAdapter<String>  {

         private List<String> itemsll; // Probably not needed


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

            @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);



                }

                if (currpos == position) {
                    ((TextView)viewnull).setBackgroundColor(Color.BLUE);
            }

                return viewnull;

            }





            public void setSelectedPosition( int pos )
            {
                currpos = pos; // selectedPos is global variable to handle clicked position
                // inform the view of this change
                notifyDataSetChanged();
            }

          }

EDIT 3 : getDiri Method :

private void getdiri(String dirpathi) {
         mypathi.setText("Current Folder:"+dirpathi);
         getcurpathi = dirpathi;
         itemi = new ArrayList<String>();
         pathi = new ArrayList<String>();
         fullpathi = new ArrayList<String>();
         File fi = new File(dirpathi);

         File[] filesi = fi.listFiles();

         if(!dirpathi.equals(rooti))
         {
          itemi.add(rooti);
          pathi.add(rooti);
          itemi.add("../");
          pathi.add(fi.getParent()); 
         }

         for(int i=0; i < filesi.length; i++)
         {
          File filei = filesi[i];
          if(!filei.isHidden() && filei.canRead()){

              if(filei.isDirectory()){
                pathi.add(filei.getPath());
                itemi.add(filei.getName() + "/");

              } else {

                  itemi.add(filei.getName());
                  pathi.add(filei.getName());


              }
          } 
         }

         fl = new FileListAdapter(this,R.layout.row,itemi);

         setListAdapter(fl);

     }

EDIT 4 : Further to this problem, I have found that the list is not showing the descriptions of the folders or files, the rows are blank but if clicked on they move into the folder even though it has now description against it .

EDIT 5 : Added code to getview

I have now added the following:

rowtext.setText(currow);

After :

String currow =     itemsll.get(position);
 TextView rowtext = (TextView)viewnull.findViewById(R.id.rowtext);

But although I now get the list, it appears to duplicate the files and folders if I go into a folder that has more than one file.

EDIT 6 : Sorted it out now, the entire getView method is now :

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) {
                    rowtext.setBackgroundColor(Color.BLUE);

                   }


                return viewnull;    


            }
Community
  • 1
  • 1
TimCS
  • 363
  • 1
  • 7
  • 17
  • where is your textview initialized? – Raghunandan Jul 28 '13 at 13:55
  • That is half the problem I did try and set it up in the getview but I am then unsure where to go from there, The textview is in the row.xml which is then used with the adapter - added the extra code – TimCS Jul 28 '13 at 14:50
  • It would be helpful to see the entire adapter. The list could be empty because you use an incorrect constructor for example. – user Jul 28 '13 at 15:12
  • Entire adapter added now. – TimCS Jul 28 '13 at 15:17
  • Do you have data in the *itemi* list that you pass to your adapter. – user Jul 28 '13 at 15:35
  • Yes this is populated by the getdiri method which I will add to the post, before I started with the custom adapter , I was just using a standard ArrayAdapter and this all worked fine but I need to be able to highlight a row at a time so I am trying to do this custom adapter to achieve that. – TimCS Jul 28 '13 at 15:41

0 Answers0