[컴][안드로이드] 간단한 file list 를 보는 프로그램 예제

 

simple file list, file explorer, file listView

FileListAdapter.java

package com.example.com.mugallery;

import java.io.File;
import java.util.ArrayList;



import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class FileListAdapter extends BaseAdapter {


private Context mContext;
private LayoutInflater mInflater;

ArrayList<String> mData;

public FileListAdapter(Activity a, File rootPath) {
mContext = a.getApplicationContext();
mInflater = a.getLayoutInflater();

File[] list = rootPath.listFiles();

mData = new ArrayList<String>();
for(File f : list){
mData.add(f.getPath());
}


}

@Override
public int getCount() {

return mData.size();
}

@Override
public Object getItem(int position) {
return mData.get(position);
}

@Override
public long getItemId(int position) {
return position;
}

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

ViewHolder h = new ViewHolder();
if(convertView == null){
convertView = mInflater.inflate(R.layout.afile, null);
h.tv = (TextView)convertView.findViewById(R.id.file_item);

convertView.setTag(h);



}else
{
h = (ViewHolder)convertView.getTag();

}

h.tv.setText(mData.get(position));
return convertView;
}

static class ViewHolder {
TextView tv;
}

}

FileListActivity.java
package com.example.com.mugallery;


import java.io.File;

import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.widget.ListView;


public class FileListActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_filelist);

ListView lv = (ListView)findViewById(R.id.filelistview);

if (Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED))
{
FileListAdapter fileAdapter = new FileListAdapter(this,
new File(Environment.getExternalStorageDirectory(),
Environment.DIRECTORY_DOWNLOADS)
);
lv.setAdapter(fileAdapter);
}




}



}

afile.xml
<?xml version="1.0" encoding="utf-8"?>

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/file_item"
android:layout_width="match_parent"
android:layout_height="match_parent" />

activity_filelist.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<ListView
android:id="@+id/filelistview"
android:layout_width="match_parent"
android:layout_height="match_parent" />

</LinearLayout>

댓글 없음:

댓글 쓰기