如何从SDCard向ArrayList添加数据

时间:2021-09-22 17:17:13

I am trying to listing images from sdcard ("mydata") folder with Thumb and Name...

我试图用拇指和名字列出sdcard(“mydata”)文件夹中的图像...

I am reading data from sdcard and then trying to displaying it in a ListView for that i am using ArrayList but now i am not getting How to add data into ArrayList, check my code below:

我正在从SD卡读取数据,然后尝试在ListView中显示它,因为我正在使用ArrayList但现在我没有得到如何将数据添加到ArrayList中,请检查下面的代码:

Actors.java:-

public class Actors {

    private String name;
    private String status;
    private String thumb;

    public Actors() {
        // TODO Auto-generated constructor stub
    }

    public Actors(String name, String status, String thumb) {
        super();
        this.name = name;
        this.status = status;
        this.thumb = thumb;
    }


    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public String getThumb() {
        return thumb;
    }

    public void setThumb(String thumb) {
        this.thumb = thumb;
    }

}

ActorAdapter.java:-

    public class ActorAdapter extends BaseAdapter {
    ArrayList<Actors> actorList;
    LayoutInflater vi;
    int Resource;
    ViewHolder holder;

    public ActorAdapter(Context context, int resource, ArrayList<Actors> objects) {
        vi = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        Resource = resource;
        actorList = objects;
    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // convert view = design
        View v = convertView;
        if (v == null) {
            holder = new ViewHolder();
            v = vi.inflate(Resource, null);
            holder.thumbnail = (ImageView) v.findViewById(R.id.thumbnail);
            holder.textName = (TextView) v.findViewById(R.id.textName);
            holder.textStatus = (TextView) v.findViewById(R.id.textStatus);
            v.setTag(holder);
        } else {
            holder = (ViewHolder) v.getTag();
        }

        String strName = actorList.get(position).getName().toString();
        Log.v("strName", strName);
        String strStatus = actorList.get(position).getStatus().toString();
        Log.v("strStaus", strStatus);

        // Name
        String fileName = strName.substring(strName.lastIndexOf('/')+1, strName.length() );
        File file = new File(strName);
        long length = file.length();                
        holder.textName.setText(fileName + " ("+length/1024+" KB.)");
        Log.v("getName", holder.textName.getText().toString());

        // Thumb
        holder.thumbnail.setScaleType(ImageView.ScaleType.CENTER_CROP);
        Bitmap bm = BitmapFactory.decodeFile(strName);
        holder.thumbnail.setImageBitmap(bm);


        // Status
        holder.textStatus.setText(strStatus);
        Log.v("getStaus", holder.textStatus.getText().toString());

        return v;

    }

    static class ViewHolder {
        public ImageView thumbnail;
        public TextView textName;
        public TextView textStatus;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return actorList.size();
    }


    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return actorList.get(position);
    }


    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

}

MainActivity.java:-

    public class MainActivity extends Activity {

    ArrayList<Actors> actorsList;   
    ActorAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        actorsList = new ArrayList<Actors>();       

        File f = new File ("/mnt/sdcard/mydata/");
        File[] files = f.listFiles ();

        for (int i = 0; i <files.length; i++)
        {
            File  file = files[i];
            Log.d("Count",file.getPath());

            Actors actor = new Actors();
            actor.setName(file.getPath());           

            actorsList.add (actor);
        }

        ListView listview = (ListView)findViewById(R.id.list);
        adapter = new ActorAdapter(getApplicationContext(), R.layout.row, actorsList);

        adapter.notifyDataSetChanged();

        listview.setAdapter(adapter);

        listview.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int position,
                    long id) {
                // TODO Auto-generated method stub
                Toast.makeText(getApplicationContext(), actorsList.get(position).getName(), Toast.LENGTH_LONG).show();              
            }
        });
    }

}

Logcat:-

03-18 03:33:54.964: E/AndroidRuntime(1566): FATAL EXCEPTION: main
03-18 03:33:54.964: E/AndroidRuntime(1566): Process: com.wingnity.jsonparsingtutorial, PID: 1566
03-18 03:33:54.964: E/AndroidRuntime(1566): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.wingnity.jsonparsingtutorial/com.wingnity.jsonparsingtutorial.MainActivity}: java.lang.NullPointerException
03-18 03:33:54.964: E/AndroidRuntime(1566):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
03-18 03:33:54.964: E/AndroidRuntime(1566):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
03-18 03:33:54.964: E/AndroidRuntime(1566):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
03-18 03:33:54.964: E/AndroidRuntime(1566):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
03-18 03:33:54.964: E/AndroidRuntime(1566):     at android.os.Handler.dispatchMessage(Handler.java:102)
03-18 03:33:54.964: E/AndroidRuntime(1566):     at android.os.Looper.loop(Looper.java:136)
03-18 03:33:54.964: E/AndroidRuntime(1566):     at android.app.ActivityThread.main(ActivityThread.java:5017)
03-18 03:33:54.964: E/AndroidRuntime(1566):     at java.lang.reflect.Method.invokeNative(Native Method)
03-18 03:33:54.964: E/AndroidRuntime(1566):     at java.lang.reflect.Method.invoke(Method.java:515)
03-18 03:33:54.964: E/AndroidRuntime(1566):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
03-18 03:33:54.964: E/AndroidRuntime(1566):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
03-18 03:33:54.964: E/AndroidRuntime(1566):     at dalvik.system.NativeStart.main(Native Method)
03-18 03:33:54.964: E/AndroidRuntime(1566): Caused by: java.lang.NullPointerException
03-18 03:33:54.964: E/AndroidRuntime(1566):     at com.wingnity.jsonparsingtutorial.MainActivity.onCreate(MainActivity.java:29)
03-18 03:33:54.964: E/AndroidRuntime(1566):     at android.app.Activity.performCreate(Activity.java:5231)
03-18 03:33:54.964: E/AndroidRuntime(1566):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
03-18 03:33:54.964: E/AndroidRuntime(1566):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
03-18 03:33:54.964: E/AndroidRuntime(1566):     ... 11 more

Getting NPE : for (int i = 0; i <files.length; i++)

获取NPE:for(int i = 0; i ;>

row.xml:-

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:padding="8dp" >

    <!-- Thumbnail Image -->
    <ImageView
        android:id="@+id/thumbnail"
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:layout_alignParentLeft="true"
        android:contentDescription="@string/app_name"
        android:src="@drawable/ic_launcher"
        android:layout_marginRight="8dp" />

    <!-- Movie textName -->
    <TextView
        android:id="@+id/textName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/thumbnail"
        android:layout_toRightOf="@+id/thumbnail"
        android:text="Image Name" />

    <!-- textStatus -->
    <TextView
        android:id="@+id/textStatus"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/textName"
        android:layout_toRightOf="@+id/thumbnail"
        android:text="Status" />       

    <!-- Release Year -->
    <Button
        android:id="@+id/btnUpload"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/textStatus"
        android:text="Upload"
        style="?android:attr/buttonStyleSmall"
        android:layout_toRightOf="@+id/thumbnail" />

</RelativeLayout>

activity_main.xml:-

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"    android:background="#F1F1F1"
    tools:context=".MainActivity" >

    <ListView
        android:id="@+id/list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        tools:listitem="@layout/row" >

    </ListView>

</LinearLayout>

2 个解决方案

#1


1  

try this...

 for (int i = 0; i <files.length; i++)
    {
        File  file = files[i];
        Log.d("Count",file.getPath());

        Actors actor = new Actors();
        actor.setName("file name here");
        actor.setStatus("file status here");
        actor.setThumb("file thumb here");

        actorsList.add (actor);
    }

#2


1  

You should add item to your ArrayList like

您应该将项添加到您的ArrayList中

actorsList.add(new Actors("FileName","Status","thumb"));

You create actorsList with the type ArrayList<Actors>

您可以使用ArrayList 类型创建actorsList

#1


1  

try this...

 for (int i = 0; i <files.length; i++)
    {
        File  file = files[i];
        Log.d("Count",file.getPath());

        Actors actor = new Actors();
        actor.setName("file name here");
        actor.setStatus("file status here");
        actor.setThumb("file thumb here");

        actorsList.add (actor);
    }

#2


1  

You should add item to your ArrayList like

您应该将项添加到您的ArrayList中

actorsList.add(new Actors("FileName","Status","thumb"));

You create actorsList with the type ArrayList<Actors>

您可以使用ArrayList 类型创建actorsList