Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2008/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 带有自定义适配器的ListFragment_Android_Android Listview_Slider_Android Listfragment_Android Adapter - Fatal编程技术网

Android 带有自定义适配器的ListFragment

Android 带有自定义适配器的ListFragment,android,android-listview,slider,android-listfragment,android-adapter,Android,Android Listview,Slider,Android Listfragment,Android Adapter,我在我的项目中设置了一个listfragment。但我的片段似乎无法与适配器匹配。这是因为MyListAdapter中的上下文。如果我单击以更正它。它变为menuframent menuframent。但在这些更改之后,MyListAdapter出现了错误。所以我纠正了它。它变为上下文。再次,如果我纠正它,它仍然继续下去。它是这样循环的 注意:我想要实现的是带有图标的ListFragment。就像我之前的另一个问题(但不幸的是没有人回答) 编辑:这是我的布局。现在天气很好。我只需要调整文本视图和

我在我的项目中设置了一个
listfragment
。但我的片段似乎无法与适配器匹配。这是因为
MyListAdapter
中的
上下文。如果我单击以更正它。它变为
menuframent menuframent
。但在这些更改之后,
MyListAdapter
出现了错误。所以我纠正了它。它变为
上下文
。再次,如果我纠正它,它仍然继续下去。它是这样循环的

注意:我想要实现的是带有图标的ListFragment。就像我之前的另一个问题(但不幸的是没有人回答)

编辑:这是我的布局。现在天气很好。我只需要调整文本视图和线性布局,这样单词就不会被切成两半。但我面临另一个问题。就像是背景图像在互相堆积。这是我布局上的xml

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

如果我从
线性布局中删除这个
android:background=“@drawable/menu\u drawer”
。这将是一个完美的背景。不要互相堆积。但是当我在列表中滑动时,背景变得疯狂,它消失了,并显示一些黑色背景。这就像listview
android:cacheColorHint=“#00000000”
的问题一样。我已经在
线性布局中添加了
cachecolor
。但它仍然显示那些黑色背景。是这样的

安卓:背景
“>

我知道问题是什么。因为默认背景是黑色的。但我不知道如何解决它

编辑2:黑色问题解决了


已解决。

如果未将有效的
上下文
传递给适配器,则
片段
不是
上下文
。例如,您需要使用
活动
作为
上下文

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

我希望您也在适配器中实现
getCount()
方法,否则无论有多少元素,您都不会在
列表视图中看到任何内容。

创建自定义适配器时,不要将“Context”对象作为引用传递,只需将活动对象传递给自定义适配器即可

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
以下是创建listFragement列表视图的方法:
HealthAdvice.java
包com.example.babs;
导入android.app.ListFragment;
导入android.os.Bundle;
导入android.view.LayoutInflater;
导入android.view.view;
导入android.view.ViewGroup;
公共类HealthAdvice扩展了ListFragment{
@凌驾
CreateView上的公共视图(布局、充气机、视图组容器、捆绑包保存状态){
视图根视图=充气机。充气(R.layout.health\u通知,容器,错误);
返回rootView;
}
@凌驾
已创建ActivityState上的公共无效(Bundle savedInstanceState){
super.onActivityCreated(savedInstanceState);
HealthAdviceArray健康_数据[]=新的HealthAdviceArray[]
{
新的HealthAdviceArray(R.drawable.ic_发射器,“多云”),
新的HealthAdviceArray(R.drawable.ic_发射器,“淋浴器”),
新的HealthAdviceArray(R.drawable.ic_发射器,“雪地”),
新的HealthAdviceArray(R.drawable.ic_发射器,“风暴”),
新的HealthAdviceArray(R.drawable.ic_启动器,“Sunny”)
};
HealthAdviceAdapter=新的HealthAdviceAdapter(getActivity(),
R.layout.health\u advice\u item\u row,health\u data);
/**将阵列适配器设置为列表视图*/
setListAdapter(适配器);
}
}//结束主课健康忠告
步骤2:
HealthAdviceAdapter.java
包com.example.babs;
导入android.app.Activity;
导入android.content.Context;
导入android.view.LayoutInflater;
导入android.view.view;
导入android.view.ViewGroup;
导入android.widget.ArrayAdapter;
导入android.widget.ImageView;
导入android.widget.TextView;
公共类HealthAdviceAdapter扩展了ArrayAdapter{
语境;
国际布局资源;
HealthAdviceArray数据[]=null;
public HealthAdviceAdapter(上下文上下文、int-layoutResourceId、HealthAdviceArray[]数据){
超级(上下文、布局资源ID、数据);
this.layoutResourceId=layoutResourceId;
this.context=上下文;
这个数据=数据;
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
视图行=转换视图;
HealthAdviceArrayHolder holder=null;
if(行==null)
{
LayoutInflater充气器=((活动)上下文)。getLayoutInflater();
行=充气机。充气(layoutResourceId,父级,false);
holder=新的HealthAdviceArrayHolder();
holder.imgIcon=(ImageView)row.findViewById(R.id.imgIcon);
holder.txtTitle=(TextView)row.findViewById(R.id.txtTitle);
row.setTag(支架);
}
其他的
{
holder=(HealthAdviceArrayHolder)行。getTag();
}
HealthAdviceArray天气=数据[位置];
holder.txtTitle.setText(weather.title);
holder.imgIcon.setImageResource(weather.icon);
返回行;
}
静态类HealthAdviceArrayHolder
{
ImageView imgIcon;
TextView-txtTitle;
}
}
步骤3:
HealthAdviceArray.java
包com.example.babs;
公共类HealthAdviceArray{
公共int图标;
公共字符串标题;
//我们这里超载了
公共健康忠告数组(){
超级();
}
public HealthAdviceArray(整数图标,字符串标题){
超级();
this.icon=图标;
this.title=标题;
}
}
步骤4:
健康忠告健康忠告.xml
步骤5:
健康建议项目:健康建议项目行.xml

非常感谢您非常感谢。一周后终于解决了。FYI=我没有实现getCount(),它仍然工作得很好。但是有一个问题。我的文本在列表中看起来不太合适。比如说什么时候
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
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>