简体   繁体   English

ListView 列表图标显示

[英]ListView list icon show

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我有一个 android 项目,它有一个活动下载文件,我希望该文件显示在列表视图中,并根据该文件的扩展名显示一个图标,或者如果文件是 mp3,则为 mp3 封面图片

Please Someone Help Me How To Do it:-请有人帮我怎么做: -

This is my download.java file这是我下载的.java文件

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

<?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我的下载文件列表视图老的

I Want我想要我想要这个列表视图

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首先,您需要创建一个自定义 ListView 适配器,之后您可以使用适配器 class 中的 If 语句轻松指定图标

Check this link to know more how to do that..检查此链接以了解更多如何做到这一点..

Create a custom ListView Adapter 创建自定义 ListView 适配器

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM