-3

I have an android project which has an activity downloads file and I want the file showing in the listview as well as an icon to be shown according to the extension of that file or if file is mp3 then mp3 Cover Picture

Please Someone Help Me How To Do it:-

This is my download.java file

package com.musicwala.djaman;
import android.app.Activity;
import android.os.Bundle;
import java.util.ArrayList;
import android.os.Environment;
import java.io.File;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import java.util.Arrays;
import org.json.JSONArray;
import java.util.List;
import android.app.ListActivity;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.ColorDrawable;
import android.graphics.Color;
import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;
import android.view.animation.OvershootInterpolator;
import android.widget.Toast;
import android.support.v7.app.AppCompatActivity;
import android.media.MediaPlayer;
import java.io.IOException;

public class download extends ListActivity
{
    ListView listView;
    private File file;
    private List<String> myList;
   // String [] items = {"Bitcoin", "Ethereum", "LiteCoin", "Dash", "Neo", "Nano", "Bitcoin Cash", "Verge", "Ripple", "Bitcoin Diamond", "Iconomi", "Stellar Lumens"}; 
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        // TODO: Implement this method
        //getActionBar().setDisplayHomeAsUpEnabled(true);
        getActionBar().setTitle("Downloads");
        getActionBar().setBackgroundDrawable(new ColorDrawable(Color.GREEN));
        getActionBar().setSubtitle("MusicWala");
        super.onCreate(savedInstanceState);
        myList = new ArrayList<String>();   

        String root_sd = Environment.getExternalStorageDirectory().toString();
        file = new File( root_sd + "/MusicWala" ) ;       
        File list[] = file.listFiles();

        for( int i=0; i< list.length; i++)
        {
            myList.add( list[i].getName() );
        }

        setListAdapter(new ArrayAdapter<String>(this,
                                                android.R.layout.simple_list_item_1, myList ));

    }

    protected void onListItemClick(ListView l, View v, int position, long id) 
    {
        super.onListItemClick(l, v, position, id);

        File temp_file = new File( file, myList.get( position ) );  

        if( !temp_file.isFile())        
        {
            file = new File( file, myList.get( position ));
            File list[] = file.listFiles();

            myList.clear();

            for( int i=0; i< list.length; i++)
            {
                myList.add( list[i].getName() );
            }
            Toast.makeText(getApplicationContext(), file.toString(), Toast.LENGTH_LONG).show(); 
            setListAdapter(new ArrayAdapter<String>(this,
                                                    android.R.layout.simple_list_item_1, myList ));

        }
        Toast.makeText(getApplicationContext(), temp_file.getPath(), Toast.LENGTH_LONG).show(); 
        /*String filePath = Environment.getExternalStorageDirectory()+"/yourfolderNAme/yopurfile.mp3";
        */
        String path = temp_file.getPath();
        String name = temp_file.getName();
        
        Intent intent = new Intent(download.this, play_ui.class); 
        intent.putExtra("file", name).putExtra("path",path);
          startActivity(intent); 
           
       
    }}

this is my download.xml of list view

<?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="fill_parent"
    android:orientation="vertical">

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="21dp">

        
    </RelativeLayout>

    <ListView
        android:id="@+id/list"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

</RelativeLayout>

my listview of download file old

I Want I want like this listview

Community
  • 1
  • 1

1 Answers1

0

First, You need to create a custom ListView Adapter, After that you can specify the icon easily using an If statement in your Adapter class

Check this link to know more how to do that ..

Create a custom ListView Adapter

Youssef Islem
  • 34
  • 2
  • 6