Use CustomAdapter to change how ListView is displayed

♀尐吖头ヾ 提交于 2019-12-08 01:36:23

问题


My XML that I would like to use for custom adapter:

<?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>

My List view XML layout:

<?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>

My Java code:

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;
    }
}

The above code produces a very plain looking format:

I would like some help in implementing CustomAdapter to use both XML file so I can make it like the following:


回答1:


You have to extend BaseAdapter. I'll provide you with some code from one of my past projects:

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 is the .xml where I defined the layout of each item in the list. list_cons is just a class from my project, which I used for storing data.




回答2:


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

have a great tutural for you and how to insert objects in a custom listview layout using an adapter. Its not as much code as it looks like. I've used this code and it was a breeze. In the list view adapter class you specify which textview to show the part on the object you want.



来源:https://stackoverflow.com/questions/18045035/use-customadapter-to-change-how-listview-is-displayed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!