custom adapter for listview in fragment Android

◇◆丶佛笑我妖孽 提交于 2020-01-10 20:12:46

问题


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

}

回答1:


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.




回答2:


I have a problem to, with listView in fragment. I try to get my database, and want to view in listView. After deep search, I find solution, here my code in fragment

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

}


来源:https://stackoverflow.com/questions/32475996/custom-adapter-for-listview-in-fragment-android

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