0

Hello i am trying to show list of files and folder in ListView in a android app but i am unable to do that. I got succeeded to show the sdcard files but not able to show the files from ftp server. Can someone help me with this?

 FTPFile[] list = client.list();
            String[] fileNames = new String[list.length];
            for (int i = 0; i < list.length; i++) {

                fileNames[i] = list[i].getName();
                fileList[i]=list[i].getName();

Now i want to use this array in array adapter but don't know how to? Tried few ways but application force stopped.

Vivo
  • 758
  • 1
  • 7
  • 20
Sid
  • 1
  • 1
  • 5
  • I would add these strings to an ArrayList , then use a BaseAdapter. Then assign the adapter to the listview. – Breadbin Aug 30 '14 at 12:57
  • Could you please the post code with little explaination in answer as i dpn't know how to do it correctly because i am very new to android. – Sid Aug 30 '14 at 13:04

2 Answers2

0

You can follow this exising post. They use string arrays too. You can ignore the image views if you don't want them How to customize listview using baseadapter

Community
  • 1
  • 1
Breadbin
  • 410
  • 4
  • 15
0

this is a simple custom adapter code that you can use it for show an image and a textview in a row:

public class myDynAdap extends ArrayAdapter<String> {
    String[] list;
    Context mContext; // ADD THIS to keep a context

    public myDynAdap(Context context, int textViewResourceId,String[] objects) {
        super(context, textViewResourceId, objects);
        this.mContext = context;
        list = objects;
    }

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

        if (row == null) {

            LayoutInflater inflater = ((Activity) mContext).getLayoutInflater(); 
            row = inflater.inflate(R.layout.yourRowLayout, parent,false);
        }

        TextView tv1 = (TextView) row.findViewById(R.id.textView);

        ImageView i = (ImageView) row.findViewById(R.id.imageView);
        tv1.setText(StringArray[position]);
        i.setImageDrawable(getResources().getDrawable(yourImageArrayId[position]));

        return row;

    }
}

add a xml in your layout folder with this xml code:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="start" >

<ImageView
    android:id="@+id/imageView"
    android:layout_width="64dp"
    android:layout_height="48dp"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true" />

<TextView
    android:id="@+id/textView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true" />

saleh sereshki
  • 2,041
  • 3
  • 14
  • 18