片段 Android 中列表视图的自定义适配器

custom adapter for listview in fragment Android

嗨,我正在学习 android 列表视图,任何人都可以提供有关如何将 CustomAdapter 设置为片段的想法 activity?
下面是片段的简单列表视图。对于片段中的 customListview Activity 而不是其他如何进行...

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

}

请查看下方 link 以在 android 中创建自定义列表视图。

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

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

这是片段 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;
    }
}

自定义适配器 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;
    }

}

适配器项目布局:

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

和片段布局:

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

这比你想要的更多。您可以自定义它,因为您 want.Put 您的自定义数据要列出以填充 ArrayList。仅将其用作基本示例。!!

希望对您有所帮助。

我有一个问题,listView 片段。我尝试获取我的数据库,并希望在 listView 中查看。经过深入搜索,我找到了解决方案,这里是我的代码片段

protected void tampilEvent() {

    HashMap<String, String> map=new HashMap<String, String>();
    final ArrayList<event_constructor> arayList = new ArrayList<event_constructor>();
    List<event_constructor> allEvents = db.getAllEvent();
    for (event_constructor event : allEvents) {
        map=new HashMap<String, String>();
        map.put("id", ""+event.getId());
        map.put("nama", event.getEventName());
        data.add(map);

    }
    // Don't forget to close database connection
    db.closeDB();
    //KEYS IN MAP
    String[] from={"id", "nama"};
    //IDS OF VIEWS
    int[] to={R.id.noId, R.id.event};
    //ADAPTER
    adapter=new SimpleAdapter(getActivity(), data, R.layout.list_event, from, to);
    setListAdapter(adapter);

}