android - Android fragment 中 ListView 的自定义适配器

标签 android listview android-fragments custom-adapter

嗨,我正在学习 android listview,有人可以提供有关如何将 CustomAdapter 设置为 fragment Activity 的想法吗?
下面是 fragment 的简单 ListView 。对于 Fragment Activity 中的 customListview 而不是其他如何进行...

public class CallFragment extends Fragment {

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.fragment_top_rated,
                container, false);
        ListView listview = (ListView) rootView.findViewById(R.id.listView1);
        String[] items = new String[] { "Item 1", "Item 2", "Item 3" };
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),
                android.R.layout.simple_list_item_1, items);
        listview.setAdapter(adapter);
        return rootView;
    }

}

最佳答案

请检查以下链接以在 android 中创建自定义 ListView 。

https://www.caveofprogramming.com/guest-posts/custom-listview-with-imageview-and-textview-in-android.html

已编辑:为 fragment 添加了简单示例。在这里,我已将调用数据添加到自定义适配器。

这是 fragment 类:

import java.util.ArrayList;
import java.util.HashMap;

import adapter.CustomCallLogListAdapter;
import android.annotation.SuppressLint;
import android.content.CursorLoader;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.CallLog;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.broadcast.myblocklist.R;

public class CallLogFragment extends Fragment
{
    private View view;
    private ListView list_calllog;
    private ArrayList<HashMap<String,String>> callLog;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) 
    {
        if(view==null)
        {
            view=inflater.inflate(R.layout.fragment_call_log_layout, container,false);
        }   
        else
        {
            ViewGroup parent = (ViewGroup) view.getParent();
            parent.removeView(view);
        }   

        callLog=getCallLog();
        CustomCallLogListAdapter adapter=new CustomCallLogListAdapter(getActivity(),R.layout.row_call_log_layout,callLog);
        list_calllog=(ListView)view.findViewById(R.id.list_calllog);
        list_calllog.setAdapter(adapter);
        return view;
    }

    @SuppressLint("NewApi")
    public ArrayList<HashMap<String,String>> getCallLog()
    {
        ArrayList<HashMap<String,String>> callLog=new ArrayList<HashMap<String,String>>();
        CursorLoader cursorLoader=new CursorLoader(getActivity(),CallLog.Calls.CONTENT_URI, null, null, null, null);
        Cursor cursor=cursorLoader.loadInBackground();
        if(cursor.moveToFirst())
        {
            while (cursor.moveToNext())
            {
                HashMap<String,String> hashMap=new HashMap<String, String>();
                hashMap.put(CallLog.Calls.NUMBER, cursor.getString(cursor.getColumnIndex(CallLog.Calls.NUMBER)));
                hashMap.put(CallLog.Calls.DATE, cursor.getString(cursor.getColumnIndex(CallLog.Calls.DATE)));
                hashMap.put(CallLog.Calls.DURATION, cursor.getString(cursor.getColumnIndex(CallLog.Calls.DURATION)));   
                callLog.add(hashMap);
            }
        }
        return callLog;
    }
}

自定义适配器类:

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;

import android.content.Context;
import android.provider.CallLog;
import android.text.format.DateFormat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import com.broadcast.myblocklist.R;

public class CustomCallLogListAdapter extends ArrayAdapter<HashMap<String,String>>
{
    private ArrayList<HashMap<String,String>> callLogData;
    private Context context;
    private int resource;
    private View view;
    private Holder holder;
    private HashMap<String,String> hashMap;
    public CustomCallLogListAdapter(Context context, int resource,ArrayList<HashMap<String, String>> objects) 
    {
        super(context, resource, objects);
        this.context=context;
        this.resource=resource;
        this.callLogData=objects;
    }

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

        LayoutInflater inflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view=inflater.inflate(resource, parent,false);

        holder=new Holder();
        holder.text_number=(TextView)view.findViewById(R.id.text_calllog_number);
        holder.text_date=(TextView)view.findViewById(R.id.text_calllog_date);
        holder.text_time=(TextView)view.findViewById(R.id.text_calllog_time);

        hashMap=callLogData.get(position);      

        Date date=new Date(Long.parseLong(hashMap.get(CallLog.Calls.DATE)));
        java.text.DateFormat dateFormat=DateFormat.getDateFormat(context);
        java.text.DateFormat timeformat=DateFormat.getTimeFormat(context);


        holder.text_number.setText(hashMap.get(CallLog.Calls.NUMBER));
        holder.text_time.setText(timeformat.format(date));
        holder.text_date.setText(dateFormat.format(date));

        return view;
    }

    public class Holder
    {
        TextView text_number;
        TextView text_date;
        TextView text_time;
    }

}

适配器项目布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginBottom="@dimen/activity_horizontal_margin"
    android:layout_marginLeft="@dimen/activity_vertical_margin"
    android:layout_marginRight="@dimen/activity_vertical_margin"
    android:layout_marginTop="@dimen/activity_horizontal_margin" >

    <TextView
        android:id="@+id/text_calllog_number"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="20sp"
        android:textStyle="bold" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/text_calllog_number"
        android:layout_marginTop="5dp"
        android:orientation="horizontal" >

        <TextView
            android:id="@+id/text_calllog_date"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1" />

        <TextView
            android:id="@+id/text_calllog_time"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1" />
    </LinearLayout>

</RelativeLayout>

和 fragment 布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

   <ListView 
       android:id="@+id/list_calllog"
       android:layout_width="match_parent"
       android:layout_height="match_parent"></ListView>

</LinearLayout>

这比你想要的更多。您可以根据需要自定义它。将您自定义的数据放入列表以填充ArrayList。仅将其用作基本示例。!!

希望对你有帮助。

关于android - Android fragment 中 ListView 的自定义适配器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32475996/

相关文章:

android - 如何在android中的两个 fragment 之间访问onActivityResult()方法?

php - 如何将外部数据库(xampp)中的 listView 填充到 fragment 中?

android - 如何在 Android 上的 Camera 使用的 SurfaceView 上绘制叠加层?

android 媒体商店艺术

android - 如何异步调用rest api并等待所有调用在rx-android中完成

java - 从 ListView 获取子项会导致 android.view.View android.view.View.findViewById(int) 出现空对象引用

android - 如何在android中单击GridView中的另一行时隐藏 View 出现在上一个选定行的顶部

java - 只能与 Theme.AppCompat 主题(或后代)一起使用的 AppCompat 小部件

css - 在 Sencha Touch 2 中,列表不会与 View 中的其他元素一起以适当的高度显示

java - 如何获取带有子字符串文本的真实数据文本?