To set the clickable list - android

I have written the code to set the list,Now I want the clickable list how to do it.When I am clicking on perticular Item It should be clickable.Whan I am clicking on TrxtView then some action should happen and when I am clicking on Button another action should happen.
My Code is
public class Downloadlist extends ListActivity {
private List<String> item = null;
private List<String> path = null;
private String root="/sdcard";
private TextView myPath;
ListView lv1;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mydownload);
myPath = (TextView)findViewById(R.id.path);
lv1=(ListView)findViewById(R.id.list);
getDir(root);
}
private void getDir(String dirPath)
{
myPath.setText("Location: " + dirPath);
item = new ArrayList<String>();
path = new ArrayList<String>();
File f = new File(dirPath);
File[] files = f.listFiles();
if(!dirPath.equals(root))
{
item.add(root);
path.add(root);
item.add("../");
path.add(f.getParent());
}
for(int i=0; i < files.length; i++)
{
File file = files[i];
path.add(file.getPath());
if(file.isDirectory())
item.add(file.getName() + "/");
else
item.add(file.getName());
}
Log.d("itemssssssss", item.toString());
ArrayAdapter<String> fileList =
new ArrayAdapter<String>(this, R.layout.rowmydownload,R.id.rowtext, this.item);
setListAdapter(fileList);
}
}

Having a button in the listview item will make that view unClickable. So use a imageView instead of a button and put onItemClickListener for the listView in activity. In the adapter, put OnClickListener for the Textview and ImageView.

Related

Sort list view with Folders and Files types alphabetically in andorid

Sort list view with Folders and Files alphabetically
I am trying the following code
public class MainActivity extends ActionBarActivity {
private File file;
private List<String> myList;
private ListView listView;
private TextView pathTextView;
private String mediapath = new String(Environment.getExternalStorageDirectory().getAbsolutePath());
private final static String[] acceptedExtensions= {"mp3", "mp2", "wav", "flac", "ogg", "au" , "snd", "mid", "midi", "kar"
, "mga", "aif", "aiff", "aifc", "m3u", "oga", "spx"};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView=(ListView) findViewById(R.id.pathlist);
pathTextView=(TextView) findViewById(R.id.path);
myList = new ArrayList<String>();
String root_sd = Environment.getExternalStorageDirectory().toString();
Log.e("Root",root_sd);
String state = Environment.getExternalStorageState();
File list[] = null ;
/* if ( Environment.MEDIA_MOUNTED.equals(state) || Environment.MEDIA_MOUNTED_READ_ONLY.equals(state) ) { // we can read the External Storage...
list=getAllFilesOfDir(Environment.getExternalStorageDirectory());
}*/
pathTextView.setText(root_sd);
file = new File( root_sd ) ;
list = file.listFiles(new AudioFilter());
Log.e("Size of list ","" +list.length);
//LoadDirectory(root_sd);
for( int i=0; i< list.length; i++)
{
String name=list[i].getName();
int count = getAudioFileCount(list[i].getAbsolutePath());
Log.e("Count : "+count, list[i].getAbsolutePath());
if(count!=0)
myList.add(name);
/*int count=getAllFilesOfDir(list[i]);
Log.e("Songs count ",""+count);
*/
}
listView.setAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, myList ));
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position,
long arg3) {
File temp_file = new File( file, myList.get( position ) );
if( !temp_file.isFile())
{
//LoadDirectory(myList.get( position ));
file = new File( file, myList.get( position ));
File list[] = file.listFiles(new AudioFilter());
myList.clear();
for( int i=0; i< list.length; i++)
{
String name=list[i].getName();
int count = getAudioFileCount(list[i].getAbsolutePath());
Log.e("Count : "+count, list[i].getAbsolutePath());
if(count!=0)
myList.add(name);
/*int count=getAllFilesOfDir(list[i]);
Log.e("Songs count ",""+count);
if(count!=0)
myList.add(name);*/
}
pathTextView.setText( file.toString());
//Toast.makeText(getApplicationContext(), file.toString(), Toast.LENGTH_LONG).show();
listView.setAdapter(new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1, myList ));
}
}
});
}
It gives me the files and folders for all songs
Howevever ,If a folder contains both sub folders and songs ,I want to sort the listview to show all sub folders first and then all the songs in that folder
How can this be done
use this function
Collections.sort(myList);
add this line before setAdapter.
it will sort your String Arraylist in Alphabet order.
Try the following code to sort:
Collections.sort(list, new Comparator<String>() {
#Override
public int compare(String s1, String s2) {
return s1.compareToIgnoreCase(s2);
}
});

get image from a listview onlistitemclick

I can get into my custom directory by this code which displays me the list of images in my directory now what i want is to open those image when i click on a image list .
private void getDir(String dirPath) {
myPath.setText("Location: " + dirPath);
item = new ArrayList<String>();
path = new ArrayList<String>();
File f = new File(dirPath);
File[] files = f.listFiles();
if (!dirPath.equals(root)) {
item.add(root);
path.add(root);
item.add("../");
path.add(f.getParent());
}
for (int i = 0; i < files.length; i++) {
File file = files[i];
path.add(file.getPath());
if (file.isDirectory())
item.add(file.getName() + "/");
else
item.add(file.getName());
}
ArrayAdapter<String> fileList = new ArrayAdapter<String>(this, R.layout.explorernew, item);
setListAdapter(fileList);
}
File file;
#Override
protected void onListItemClick( ListView l, View v, int position, long id) {
File imgFile = new File(root+"/20150424_173923.jpg");
// TextView stxt = (TextView) findViewById(R.id.textView2);
// stxt.setText(List.get(position));
if (imgFile.exists()) {
Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
ImageView myImage = (ImageView) findViewById(R.id.imageView);
myImage.setImageBitmap(myBitmap);
}
}
I have hard-code my imgfile as my image name which is displaying the image . I want to dynamically change the image on my selection
the position parameter in the protected void onListItemClick( ListView l, View v, int position, long id) {...} function shows the position of the element in your listView which is selected, so you could get the name of your selected image by String name =(String) l.getItemAtPosition(position); or you can do another thing without using the position parameter and it's to use String name = ((TextView) view).getText();
And then you could easily use the name variable in your line File imgFile = new File(root+name);

Display Media files in ListView, NullPointerException?

MainActivity
public class MainActivity extends Activity
{
/** Called when the activity is first created. */
private List<String> myList;
File file;
#Override
public void onCreate(Bundle savedInstanceState)
{
Log.i("MEDIA", "A");
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.i("MEDIA", "B");
ListView listView = (ListView) findViewById(R.id.listView1);
myList = new ArrayList<String>();
Log.i("MEDIA", "C");
File directory = Environment.getExternalStorageDirectory();
file = new File(directory + "/Test");
File list[] = file.listFiles();
Log.i("MEDIA", "D");
//for (int i = 0; i < list.length; i++)
for (int i = 0; i < 5; i++)
{
Log.i("MEDIA", "D1"); //CRASHES HERE
//myList.add(list[i].getName());
myList.add(list[i].getName());
Log.i("MEDIA", "D2");
}
Log.i("MEDIA", "E");
final StableArrayAdapter adapter = new StableArrayAdapter(this,android.R.layout.simple_list_item_1, myList);
listView.setAdapter(adapter);
// Set all the file in the list.
}
private class StableArrayAdapter extends ArrayAdapter<String>
{
HashMap<String, Integer> mIdMap = new HashMap<String, Integer>();
public StableArrayAdapter(Context context, int textViewResourceId,List<String> objects)
{
super(context, textViewResourceId, objects);
for (int i = 0; i < objects.size(); ++i)
{
mIdMap.put(objects.get(i), i);
}
}
#Override
public long getItemId(int position)
{
String item = getItem(position);
return mIdMap.get(item);
}
#Override
public boolean hasStableIds()
{
return true;
}
}
}
Log: http://pastebin.com/WWmqKKvQ
I've picked the answer form this question on SO:
How to show audio files in a listview in Android?
Andhave also tried this tutorial:
http://www.androidhive.info/2012/03/android-building-audio-player-tutorial/
The probem was that "/Test" directory does not exist, so i replaced it with "/Bluetooth" (which is a dir that exists) , and now it displays a list of files in that folder.
Problem solved. :D
Log.i("MEDIA", "C");
File directory = Environment.getExternalStorageDirectory();
file = new File(directory+ "/Bluetooth");// + "/Test");
File list[] = file.listFiles();

How to increase cellsize of ListView

I am using a ListView the size of each cell is very small. I thought to increase size of text so that cell size will adjust automatically. but this didnt work.
My java code is as follows:
View v= findViewById(R.id.rowtext);
myPath = (TextView)findViewById(R.id.path);
root = Environment.getExternalStorageDirectory().getPath();
getDir(root);
}
public void bt_Quit(View v)
{
finish();
}
public void back(View v)
{
getDir(pos);
}
public void home(View v)
{
getDir(root);
}
private void getDir(String dirPath)
{
myPath.setText("Location: " + dirPath);
item = new ArrayList<String>();
path = new ArrayList<String>();
File f = new File(dirPath);
File[] files = f.listFiles();
pos=f.getParent();
if(!dirPath.equals(root))
{
}
for(int i=0; i < files.length; i++)
{
File file = files[i];
if(!file.isHidden() && file.canRead()){
path.add(file.getPath());
if(file.isDirectory()){
item.add(file.getName() + "/");
}else{
item.add(file.getName());
}
}
}
ArrayAdapter<String> fileList =
new ArrayAdapter<String>(this, R.layout.row, item);
setListAdapter(fileList);
}
The cellsize of List view is too small. What i want is to know how to adjust cellsize of listView?
change in your xml of listview
android:layout_width="20dp"
android:layout_height="30dp"
Change numbers according to ur need

android how to run files from sd card when click?

this is my file browser code below when show files from sd card i put multiple apk files in sd card but this code just show apk files in sd card not install when click what do ido? please help me. i want to run apk files which show on listview but is not run how do i modify this code to run selected file??
public class MainActivity extends ListActivity {
private List<String> item = null;
private List<String> path = null;
private String root;
private TextView myPath;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myPath = (TextView)findViewById(R.id.path);
root = Environment.getExternalStorageDirectory().getPath();
getDir(root);
}
private void getDir(String dirPath)
{
myPath.setText("Location: " + dirPath);
item = new ArrayList<String>();
path = new ArrayList<String>();
File f = new File(dirPath);
File[] files = f.listFiles();
if(!dirPath.equals(root))
{
item.add(root);
path.add(root);
item.add("../");
path.add(f.getParent());
}
for(int i=0; i < files.length; i++)
{
File file = files[i];
if(!file.isHidden() && file.canRead()){
path.add(file.getPath());
if(file.isDirectory()){
item.add(file.getName() + "/");
}else{
item.add(file.getName());
}
}
}
ArrayAdapter<String> fileList =
new ArrayAdapter<String>(this, R.layout.row, item);
setListAdapter(fileList);
}
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
File file = new File(path.get(position));
if (file.isDirectory())
{
if(file.canRead()){
getDir(path.get(position));
}else{
new AlertDialog.Builder(this)
.setIcon(R.drawable.ic_launcher)
.setTitle("[" + file.getName() + "] folder can't be read!")
.setPositiveButton("OK", null).show();
}
}else {
new AlertDialog.Builder(this)
.setIcon(R.drawable.ic_launcher)
.setTitle("[" + file.getName() + "]")
.setPositiveButton("OK", null).show();
}
}
}
Implement a ListView (follow this TUTORIAL).
Then implement a listener to handle the click on the items ( listView.onItemClickListener(); )
and then use the snipper below for the installation:
Intent promptInstall = new Intent(Intent.ACTION_VIEW)
.setData(Uri.parse("file:///path/to/your.apk"))
.setType("application/vnd.android.package-archive");
startActivity(promptInstall);
ref: https://stackoverflow.com/a/4604922/847818
You have to go step-by-step.

Categories

Resources