在Android的碎片列表视图自定义适配器 [英] custom adapter for listview in fragment Android

查看:1041
本文介绍了在Android的碎片列表视图自定义适配器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我第m个学习an​​droid的列表视图,有人就如何CustomAdapter设置成片段活动的想法?结果
下面是简单的片段列表视图。对于customListview在片段活动不是别的如何进行...

 公共类CallFragment扩展片段{    公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,
            捆绑savedInstanceState){        查看rootView = inflater.inflate(R.layout.fragment_top_rated,
                集装箱,FALSE);
        ListView控件列表视图=(ListView控件)rootView.findViewById(R.id.listView1);
        的String []项目=新的String [] {项目1,项目2,3项};
        ArrayAdapter<串GT;适配器=新ArrayAdapter<串GT;(getActivity()
                android.R.layout.simple_list_item_1,项目);
        listview.setAdapter(适配器);
        返回rootView;
    }}


解决方案

请查看以下链接android系统中创建自定义列表视图。

<一个href=\"https://www.caveofprogramming.com/guest-posts/custom-listview-with-imageview-and-textview-in-android.html\" rel=\"nofollow\">https://www.caveofprogramming.com/guest-posts/custom-listview-with-imageview-and-textview-in-android.html

编辑:为片段添加简单的例子。在这里,我自己添加的呼叫数据,自定义适配器。

这是片段类:

 进口的java.util.ArrayList;
进口的java.util.HashMap;进口adapter.CustomCallLogListAdapter;
进口android.annotation.Sup pressLint;
进口android.content.CursorLoader;
进口android.database.Cursor;
进口android.os.Bundle;
进口android.provider.CallLog;
进口android.support.v4.app.Fragment;
进口android.view.LayoutInflater;
进口android.view.View;
进口android.view.ViewGroup;
进口android.widget.ListView;进口com.broadcast.myblocklist.R;公共类CallLogFragment扩展片段
{
    私人浏览视图。
    私人的ListView list_calllog;
    私人的ArrayList&LT;&HashMap的LT;字符串,字符串&GT;&GT;通话记录;
    @覆盖
    公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,捆绑savedInstanceState)
    {
        如果(查看== NULL)
        {
            鉴于= inflater.inflate(R.layout.fragment_call_log_layout,集装箱,FALSE);
        }
        其他
        {
            ViewGroup中父=(ViewGroup中)view.getParent();
            parent.removeView(视图);
        }        callLog = getCallLog();
        CustomCallLogListAdapter适配器=新CustomCallLogListAdapter(getActivity(),R.layout.row_call_log_layout,callLog);
        list_calllog =(ListView控件)view.findViewById(R.id.list_calllog);
        list_calllog.setAdapter(适配器);
        返回视图。
    }    @燮pressLint(NewApi)
    公众的ArrayList&LT;&HashMap的LT;字符串,字符串&GT;&GT; getCallLog()
    {
        ArrayList的&LT;&HashMap的LT;字符串,字符串&GT;&GT; callLog =新的ArrayList&LT;&HashMap的LT;字符串,字符串&GT;&GT;();
        CursorLoader cursorLoader =新CursorLoader(getActivity(),CallLog.Calls.CONTENT_URI,NULL,NULL,NULL,NULL);
        光标光标= cursorLoader.loadInBackground();
        如果(cursor.moveToFirst())
        {
            而(cursor.moveToNext())
            {
                HashMap的&LT;字符串,字符串&GT; HashMap的=新的HashMap&LT;字符串,字符串&GT;();
                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的);
            }
        }
        返回callLog;
    }
}

自定义适配器类:

 进口的java.util.ArrayList;
进口java.util.Date;
进口的java.util.HashMap;进口android.content.Context;
进口android.provider.CallLog;
进口android.text.format.DateFormat;
进口android.view.LayoutInflater;
进口android.view.View;
进口android.view.ViewGroup;
进口android.widget.ArrayAdapter;
进口android.widget.TextView;进口com.broadcast.myblocklist.R;公共类CustomCallLogListAdapter扩展ArrayAdapter&LT;&HashMap的LT;字符串,字符串&GT;&GT;
{
    私人的ArrayList&LT;&HashMap的LT;字符串,字符串&GT;&GT; callLogData;
    私人上下文的背景下;
    私人诠释的资源;
    私人浏览视图。
    私人持有人持有人;
    私人的HashMap&LT;字符串,字符串&GT; HashMap的;
    公共CustomCallLogListAdapter(上下文的背景下,INT资源的ArrayList&LT;&HashMap的LT;字符串,字符串&GT;&GT;的对象)
    {
        超级(上下文,资源,对象);
        this.context =背景;
        this.resource =资源;
        this.callLogData =物体;
    }    @覆盖
    公共查看getView(INT位置,查看convertView,父母的ViewGroup)
    {        LayoutInflater吹气=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        鉴于= inflater.inflate(资源,家长,FALSE);        持有人=新的持有人();
        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(位置);        日期日期=新的日期(的Long.parseLong(hashMap.get(CallLog.Calls.DATE)));
        java.text.DateFormat中的日期格式= DateFormat.getDateFormat(背景);
        java.text.DateFormat中TIMEFORMAT = DateFormat.getTimeFormat(背景);
        holder.text_number.setText(hashMap.get(CallLog.Calls.NUMBER));
        holder.text_time.setText(timeformat.format(日期));
        holder.text_date.setText(dateFormat.format(日期));        返回视图。
    }    公共类持有人
    {
        TextView的text_number;
        TextView的text_date;
        TextView的text_time;
    }}

适配器产品布局:

 &LT;?XML版本=1.0编码=UTF-8&GT?;
&LT;的RelativeLayout的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent
    机器人:layout_marginBottom =@扪/ activity_horizo​​ntal_margin
    机器人:layout_marginLeft =@扪/ activity_vertical_margin
    机器人:layout_marginRight =@扪/ activity_vertical_margin
    机器人:layout_marginTop =@扪/ activity_horizo​​ntal_margin&GT;    &LT;的TextView
        机器人:ID =@ + ID / text_calllog_number
        机器人:layout_width =match_parent
        机器人:layout_height =WRAP_CONTENT
        机器人:TEXTSIZE =20SP
        机器人:文字样式=大胆/&GT;    &LT;的LinearLayout
        机器人:layout_width =match_parent
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_below =@ ID / text_calllog_number
        机器人:layout_marginTop =5DP
        机器人:方向=横向&GT;        &LT;的TextView
            机器人:ID =@ + ID / text_calllog_date
            机器人:layout_width =0dp
            机器人:layout_height =WRAP_CONTENT
            机器人:layout_weight =1/&GT;        &LT;的TextView
            机器人:ID =@ + ID / text_calllog_time
            机器人:layout_width =0dp
            机器人:layout_height =WRAP_CONTENT
            机器人:layout_weight =1/&GT;
    &LT; / LinearLayout中&GT;&LT; / RelativeLayout的&GT;

和片段布局:

 &LT;?XML版本=1.0编码=UTF-8&GT?;
&LT; LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent
    机器人:方向=垂直&GT;   &LT; ListView控件
       机器人:ID =@ + ID / list_calllog
       机器人:layout_width =match_parent
       机器人:layout_height =match_parent&GT;&LT; /&的ListView GT;&LT; / LinearLayout中&GT;

这是不是你想要更多。当你want.Put自定义数据列出来填充ArrayList中,你可以自定义。只是用它作为基地为例。!!

希望这会有所帮助。

hi i m learning android listview , anyone give idea on How to set CustomAdapter into fragment activity?
Below is simple listview for fragment. For customListview in Fragment Activity not other how to proceed...

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

}

解决方案

Please check below link for creating custom listview in android.

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

Edited : Added simple example for fragments. Here I have added call data to custom adapter.

This is fragment class :

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

Custom Adapter class :

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

}

Adapter Item layout :

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

and fragment layout :

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

This is more than you want. You can customize it as you want.Put your custom data to list to fill ArrayList. Just use it as base example.!!

Hope It will help.

这篇关于在Android的碎片列表视图自定义适配器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆