2

我想用于自定义适配器的 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;
    }
}

上面的代码产生了一个非常简单的格式:

在此处输入图像描述

我需要一些帮助来实现 CustomAdapter 以使用这两个 XML 文件,这样我就可以使它如下所示:

在此处输入图像描述

4

2 回答 2

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只是我项目中的一个类,用于存储数据。

于 2013-08-04T16:41:53.490 回答
1

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

为您提供了一个很棒的教程,以及如何使用适配器在自定义列表视图布局中插入对象。它不像看起来那样多的代码。我已经使用了这段代码,这很容易。在列表视图适配器类中,您指定要在您想要的对象上显示部分的文本视图。

于 2013-08-04T23:54:15.013 回答