使用CustomAdapter更改ListView的显示方式

时间:2013-08-04 16:27:18

标签: java android android-listview custom-adapter

我想用于自定义适配器的XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity"
    android:background="@drawable/bginset3" >
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal"
            android:paddingTop="10dp"
            android:paddingBottom="10dp"
            android:paddingLeft="10dp"
            android:paddingRight="10dp"
            android:background="@drawable/forfileicon" >
            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@android:color/white" >
                <ImageView
                    android:id="@+id/imgFileIcon"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@drawable/ic_launcher"
                    android:background="@drawable/forfileicon" />
                <TextView
                    android:id="@+id/tvFN"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text=""
                    android:textStyle="bold"
                    android:paddingTop="10dp"
                    android:textColor="#00A21E"
                    android:textSize="14dp"
                    android:paddingLeft="25dp"
                    android:layout_toRightOf="@+id/imgFileIcon" />
                <TextView
                    android:id="@+id/tvMD"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text=""
                    android:textStyle="bold"
                    android:paddingTop="10dp"
                    android:textColor="#00A21E"
                    android:textSize="14dp"
                    android:paddingLeft="25dp"
                    android:layout_below="@+id/tvFN"
                    android:layout_toRightOf="@+id/imgFileIcon" />
            </RelativeLayout>
        </LinearLayout>
</RelativeLayout>

我的列表视图XML布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity"
    android:background="@drawable/bginset3" >

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

</RelativeLayout>

我的Java代码:

package com.test.testing;

import java.io.File;
import java.util.ArrayList;
import java.util.Date;

import android.app.Fragment;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Environment;
import android.text.Editable;
import android.text.TextWatcher;
import android.text.format.DateFormat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

public class PreviousTrip extends Fragment {
    int i;
    String k = "";

    @Override
    public View onCreateView( LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState ) {
        final RelativeLayout mFrame3 =  (RelativeLayout) inflater.inflate( R.layout.ptrip, container, false );

        File folder = new File(Environment.getExternalStorageDirectory() + "/tc/");
        ListView lv;
        ArrayList<String> FilesInFolder = GetFiles(folder.getAbsolutePath());
        lv = (ListView) mFrame3.findViewById(R.id.lvFiles);

        lv.setAdapter(new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, FilesInFolder));

        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
                // Clicking on items
            Toast.makeText(getActivity(), "Item " + (position + 1) + ": ID" + v, Toast.LENGTH_SHORT).show();
             }
        });
        return mFrame3;
    }

    public ArrayList<String> GetFiles(String DirectoryPath) {
        ArrayList<String> MyFiles = new ArrayList<String>();
        File f = new File(DirectoryPath);


        //f.mkdirs();
        File[] files = f.listFiles();
        if (files.length == 0)
            return null;
        else {
            for (int i=0; i<files.length; i++)  {
                if (files[i].getName().endsWith(".tol")) {
                    long lastTime = files[i].lastModified();
                    String dateString = DateFormat.format("MM/dd/yyyy", new Date(lastTime)).toString();
                    String sizeString = getReadableSize(files[i].length());
                    String fileName = files[i].getName().substring(0, files[i].getName().lastIndexOf(".")); 
                    MyFiles.add("Trip Name: " + fileName + "\nTrip Taken On: " + dateString + "\n" + sizeString);
                }
            }
        }

        return MyFiles;
    }

    public String getReadableSize(long size) {
        String returnSize = "";

        if ((double) size / (1024 * 1024 * 1024) > 1.0) {
        double i = ((double) size / (1024 * 1024 * 1024)) * 100.0;
        long j = (long) i;
        double k = (double) j / 100.0;
        returnSize = k + " GB";
        } else if ((double) size / (1024 * 1024) > 1.0) {
        double i = ((double) size / (1024 * 1024)) * 100.0;
        long j = (long) i;
        double k = (double) j / 100.0;
        returnSize = k + " MB";
        } else if ((double) size / (1024) > 1.0) {
        double i = ((double) size / (1024)) * 100.0;
        long j = (long) i;
        double k = (double) j / 100.0;
        returnSize = k + " KB";
        } else {
        returnSize = size + " bytes";
        } 
        return returnSize;
    }
}

上面的代码产生了一种非常简洁的格式:

enter image description here

我想帮助实现CustomAdapter以使用这两个XML文件,所以我可以像下面那样:

enter image description here

2 个答案:

答案 0 :(得分:1)

您必须延长BaseAdapter。我将从过去的一个项目中为您提供一些代码:

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class CustomAdapter extends BaseAdapter{

    private list_cons[] data;
    private static LayoutInflater inflater=null;

    public CustomAdapter(Context context, list_cons[] data){
        this.data=data;
        inflater= (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    public int getCount(){
        return data.length;
    }

    public Object getItem(int position){
        return data[position];
    }

    public long getItemId(int position){
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent){

        View current= convertView;
        if(convertView==null)
            current = inflater.inflate(R.layout.list_item,null);

        TextView name = (TextView) current.findViewById(R.id.genre_name);
        ImageView imag = (ImageView) current.findViewById(R.id.list_image);

        //Setting all values in ListView

        name.setText(data[position].title);
        imag.setImageResource(data[position].image_id);

        return current; 

    }//end of method getView :)

}

list_item是.xml,我在其中定义了列表中每个项目的布局。 list_cons只是我项目中的一个类,我用它来存储数据。

答案 1 :(得分:1)

http://www.josecgomez.com/2010/05/03/android-putting-custom-objects-in-listview/

为您提供了很好的信息,以及如何使用适配器在自定义列表视图布局中插入对象。它的代码不像它看起来那么多。我已经使用了这段代码,这是一件轻而易举的事。在列表视图适配器类中,您可以指定要在所需对象上显示该部件的文本视图。