【问题标题】:ListView list icon showListView 列表图标显示
【发布时间】:2020-05-31 10:05:50
【问题描述】:

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

请有人帮我怎么做:-

这是我的 download.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); 
           
       
    }}

这是我的列表视图的download.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>

我的下载文件列表视图

我想要

【问题讨论】:

  • 简单地添加ImageView有什么问题?

标签: java android listview


【解决方案1】:

首先,您需要创建一个自定义的 ListView Adapter,然后您可以在 Adapter 类中使用 If 语句轻松指定图标

查看此链接以了解更多操作方法..

Create a custom ListView Adapter

【讨论】:

    猜你喜欢
    • 2012-02-24
    • 2010-09-11
    • 2016-05-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-17
    • 1970-01-01
    • 1970-01-01
    • 2010-10-02
    相关资源
    最近更新 更多