タグ: Android ListView

ListView表示までの流れ

基本的な流れとして、アダプタクラスを継承したカスタムクラスを作成し、getViewメゾットで各行毎のコントロールに値やイメージをセットしていくみたい。

要素数はベースクラスのコンストラクタに渡す配列の要素数となるようだ。

それを踏まえて以下のように作ってみました。


ListView一行分のデザインを作る


AndroidでListViewを表示する

ListSingle(リスト一行分のデザイン)

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    <TableRow>
        <ImageView
            android:id="@+id/img"
            android:layout_width="50dp"
            android:layout_height="50dp"/>

        <TextView
            android:id="@+id/txt"
            android:layout_width="wrap_content"
            android:layout_height="50dp" />

    </TableRow>
</TableLayout>

MainActivity

package com.example.sample;

import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;
import android.app.Activity;

import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.graphics.drawable.Drawable;
public class MainActivity extends Activity {
    ListView list;
    String[] web = {
            "Books",
            "Butterfly",
            "White Paper",
            "Black Paper",
            "Flower",
            "Leaf",
            "Beer"
    } ;
    Integer[] imageId = {
            R.drawable.image1,
            R.drawable.image2,
            R.drawable.image3,
            R.drawable.image4,
            R.drawable.image5,
            R.drawable.image6,
            R.drawable.image7

    };
    private List<String> AppNameList = new ArrayList<String>();
    private List<Drawable> IconList = new ArrayList<Drawable>();

    private void CreateApplicationList(){
        PackageManager pm = this.getPackageManager();

        android.content.Intent intent = new android.content.Intent(android.content.Intent.ACTION_MAIN, null);
        intent.addCategory(android.content.Intent.CATEGORY_LAUNCHER);
        List<android.content.pm.ResolveInfo> appInfo = pm.queryIntentActivities(intent, 0);

        Drawable icon = null;
        List<String> retList = new java.util.ArrayList<String>();
        for(android.content.pm.ResolveInfo item: appInfo){
            String packName = item.activityInfo.packageName;
            try {
                icon = pm.getApplicationIcon(packName);
                IconList.add(icon);
                AppNameList.add(item.loadLabel(pm).toString());
            }
            catch (PackageManager.NameNotFoundException e) {
                e.printStackTrace();
                retList.add("error:" + item.loadLabel(pm).toString());
            }
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        CreateApplicationList();
        CustomList adapter = new CustomList(MainActivity.this, AppNameList, IconList,web, imageId);
//        CustomList adapter = new CustomList(MainActivity.this, web, imageId, appList);
//        CustomList adapter = new CustomList(MainActivity.this, web, imageId);
        list=(ListView)findViewById(R.id.list);
        list.setAdapter(adapter);
        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
                Toast.makeText(MainActivity.this, "You Clicked at " +web[+ position], Toast.LENGTH_SHORT).show();

            }
        });

    }
}

CustomListクラス

package com.example.sample;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import android.graphics.drawable.Drawable;

import java.util.ArrayList;
import java.util.List;

public class CustomList extends ArrayAdapter<String>{
    private final Activity context;
    private final String[] web;
    private final Integer[] imageId;

    private String[] AppNameList;
    private Drawable[] AppIconList;

    public CustomList(Activity context, List<String> appList, List<Drawable> iconList, String[] web,Integer[] imageId) {
        super(context, R.layout.list_single, appList.toArray(new String[0]));
        this.web=web;
        this.imageId = imageId;
        this.context = context;
        this.AppNameList = appList.toArray(new String[0]);
        this.AppIconList = iconList.toArray(new Drawable[0]);

    }
    public CustomList(Activity context,
                      java.util.List<String> web, Integer[] imageId) {
        super(context, R.layout.list_single, web);
        this.web=web.toArray(new String[0]);
        this.context = context;
        this.imageId = imageId;

    }
    @Override
    public View getView(int position, View view, ViewGroup parent) {
        LayoutInflater inflater = context.getLayoutInflater();
        View rowView= inflater.inflate(R.layout.list_single, null, true);
        TextView txtTitle = (TextView) rowView.findViewById(R.id.txt);

        ImageView imageView = (ImageView) rowView.findViewById(R.id.img);
//        txtTitle.setText("これは "+ web[position]);
//        imageView.setImageResource(imageId[position]);
        try {
            txtTitle.setText(this.AppNameList[position]);
            imageView.setImageDrawable(this.AppIconList[position]);
        }catch (Exception e){
            txtTitle.setText("これは "+ web[position]);
            imageView.setImageResource(imageId[position/6]);
        }

        return rowView;
    }

}

MainActivity(メイン画面)

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:background="#FFFFFF">

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

    </ListView>


</FrameLayout>