Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ListFragment with Custom Adapter

I have setup a listfragment in my project. but it seems my fragment cant get it right with my adapter. its because of Context context in MyListAdapter. if i click to correct it. it changes into MenuFragment menuFragment. But after that changes, MyListAdapter got error. so i correct it. it changes into Context context. and again if i correct it, its still goes on and on. its looping like that.

Note: What i want to achieve is ListFragment with icon. like my other question before (but unfortunately no one answer it).

public class MenuFragment extends ListFragment {


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    return super.onCreateView(inflater, container, savedInstanceState);  
    }

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

     String [] proMenu ={ "Homies", "Best Nearby", "Coupon" , "Profile" , "History" , "", "Setting" ,
               "About" , "Sign Out"};

     setListAdapter(new MyListAdapter(this, proMenu));

}

@Override
public void onListItemClick(ListView lv, View v, int position, long id) {
    Fragment newContent = null;
    switch (position) {
    case 0:
        newContent = new ColorFragment();
        break;
    case 1:
        Intent intent7 = new Intent();
        intent7.setClass(getActivity(), Home.class);
        intent7.putExtra("index", position);
        startActivity(intent7);
        break;

EDIT : This is my layout. its perfectly fine now. i just have to tweak the textview and linear layout, so that the word not cut in half. but i'm facing another problem. its like the background image is piling up each other. this is the xml on my 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="50dp"
android:orientation="horizontal" 
android:cacheColorHint="#00000000" 
android:background="@drawable/menu_drawer">  

<ImageView
    android:id="@+id/row_icon"
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:padding="10dp"
    android:src="@drawable/ic_launcher" />

<TextView
    android:id="@+id/row_title"
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:gravity="center_vertical"
    android:padding="10dp"
    android:text="Medium Text"
    android:textSize="20dp"
    android:textAppearance="@android:style/TextAppearance.Medium" />

</LinearLayout>

the layout

And if i remove this android:background="@drawable/menu_drawer" from the linear layout. it will be the perfect background. not piling up each other. but when i'm swiping in the list, the background go nuts, its gone and showing some black background in it. its like the problem with listview android:cacheColorHint="#00000000". i already added that cachecolor in linear layout. but its still showing those black background. its like this.

this is what happen when i remove <code>android:background</code>

i know whats the problem are. its because the default background are black. but i dont know how to solve it.

EDIT2 : Black Problem Solved.

SOLVED.

like image 580
Nicolas Avatar asked Feb 16 '23 20:02

Nicolas


2 Answers

You don't pass a valid Context to your adapter, a Fragment isn't a Context. You need to use , for example, an Activity as the Context:

setListAdapter(new MyListAdapter(getActivity(), proMenu));

I hope you also implement the getCount() method in the adapter otherwise you'll not see anything in the ListView no matter how many elements do you have.

like image 130
user Avatar answered Feb 21 '23 01:02

user


Following is the method to create listFragement list view:
HealthAdvice.java

package com.example.babs;


import android.app.ListFragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class HealthAdvice extends ListFragment {

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

        View rootView = inflater.inflate(R.layout.health_advice, container, false);

        return rootView;

    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        HealthAdviceArray health_data[] = new HealthAdviceArray[]
                {
                new HealthAdviceArray(R.drawable.ic_launcher, "Cloudy"),
                new HealthAdviceArray(R.drawable.ic_launcher, "Showers"),
                new HealthAdviceArray(R.drawable.ic_launcher, "Snow"),
                new HealthAdviceArray(R.drawable.ic_launcher, "Storm"),
                new HealthAdviceArray(R.drawable.ic_launcher, "Sunny")
                };

        HealthAdviceAdapter adapter = new HealthAdviceAdapter(getActivity(), 
                R.layout.health_advice_item_row, health_data);

        /** Setting the array adapter to the list view */
        setListAdapter(adapter);

    }

}// end main class HealthAdvice

Step 2:

HealthAdviceAdapter.java
package com.example.babs;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class HealthAdviceAdapter extends ArrayAdapter<HealthAdviceArray>{

    Context context; 
    int layoutResourceId;    
    HealthAdviceArray data[] = null;

    public HealthAdviceAdapter(Context context, int layoutResourceId, HealthAdviceArray[] data) {
        super(context, layoutResourceId, data);
        this.layoutResourceId = layoutResourceId;
        this.context = context;
        this.data = data;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View row = convertView;
        HealthAdviceArrayHolder holder = null;

        if(row == null)
        {
            LayoutInflater inflater = ((Activity)context).getLayoutInflater();
            row = inflater.inflate(layoutResourceId, parent, false);

            holder = new HealthAdviceArrayHolder();
            holder.imgIcon = (ImageView)row.findViewById(R.id.imgIcon);
            holder.txtTitle = (TextView)row.findViewById(R.id.txtTitle);

            row.setTag(holder);
        }
        else
        {
            holder = (HealthAdviceArrayHolder)row.getTag();
        }

        HealthAdviceArray weather = data[position];
        holder.txtTitle.setText(weather.title);
        holder.imgIcon.setImageResource(weather.icon);

        return row;
    }

    static class HealthAdviceArrayHolder
    {
        ImageView imgIcon;
        TextView txtTitle;
    }
}

Step 3:

HealthAdviceArray.java

package com.example.babs;


public class HealthAdviceArray {

    public int icon;
    public String title;

    // we are over loading here
    public HealthAdviceArray(){
        super();
    }

    public HealthAdviceArray(int icon, String title) {
        super();
        this.icon = icon;
        this.title = title;
    }

}

step 4: 

Health Advice health_advice.xml

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

    <ListView
        android:id="@android:id/list"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />

</LinearLayout>

step 5:

Health Advice items: health_advice_item_row.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal"
    android:padding="10dp" >

    <ImageView
        android:id="@+id/imgIcon"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_marginBottom="5dp"
        android:layout_marginRight="15dp"
        android:layout_marginTop="5dp"
        android:contentDescription="@string/image_view"
        android:gravity="center_vertical" />

    <TextView
        android:id="@+id/txtTitle"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_marginBottom="5dp"
        android:layout_marginTop="5dp"
        android:gravity="center_vertical"
        android:textColor="#000000"
        android:textSize="22sp"
        android:textStyle="bold" />

</LinearLayout>
like image 22
Vinod Joshi Avatar answered Feb 21 '23 02:02

Vinod Joshi