【问题标题】:Custom Adapter Cannot be applied in navigation drawer's fragment自定义适配器不能应用于导航抽屉的片段
【发布时间】:2016-02-22 19:36:35
【问题描述】:

我想在导航抽屉的片段页面中显示自定义列表视图。但是,在我的片段类中,我收到一个错误,似乎我无法设置自定义适配器。

package android_gcm_client.mynavigation;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import java.util.ArrayList;
public class List_Fragment extends Fragment {
    View rootview;
    ArrayList prgmName;
public static int [] prgmImages=      {R.drawable.images,R.drawable.images1,R.drawable.images2,R.drawable.images3,R.drawable.images4,R.drawable.images5,R.drawable.images6,R.drawable.images7,R.drawable.images8};
    public static String [] prgmNameList={"Let Us   C","c++","JAVA","Jsp","Microsoft .Net","Android","PHP","Jquery","JavaScript"};

    @Nullable

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        rootview=inflater.inflate(R.layout.custom_layout,container,false);

        CustomAdapter ca = new CustomAdapter(this,prgmNameList,prgmImages);
        ListView listview=(ListView) getView().findViewById(R.id.listView);
        listview.setAdapter(ca);
        return rootview;
    }
}

似乎错误发生在下面一行(无法应用自定义适配器)。

CustomAdapter ca = new CustomAdapter(this,prgmNameList,prgmImages);

在 MainActivity 中,我按如下方式调用片段:

public void onNavigationDrawerItemSelected(int position) {
    // update the main content by replacing fragments

    Fragment objFragment=null;
    switch(position) {
        case 0:
            objFragment=new ListFragment();
            break;
    }

     FragmentManager fragmentManager = getSupportFragmentManager();
    fragmentManager.beginTransaction()
            .replace(R.id.container, objFragment)
            .commit();
}

我尝试了不同的方法来显示导航项选择方法的自定义列表视图。

我尝试直接调用活动而不是片段,但问题是导航抽屉对所有活动都不可见。因此,我尝试像在片段中一样在活动中调用 CustomAdapter。

我很难解决这个错误。 (抱歉英语不好)。

【问题讨论】:

  • 请分享您收到的确切错误。另外,您是否创建了 CustomAdapter.java 类?纯 Android 中不存在此类。所以,你必须创造它。

标签: android listview android-fragments


【解决方案1】:

您在自定义适配器中使用this。通常适配器在构造函数调用中需要Context。但是这个构造函数是在一个fragment里面调用的,this不能用。

您可以使用getActivity() 作为片段内的上下文。 但是。 如果在这个片段的onAttach() 之前调用它,有时它可以返回一个null

CustomAdapter ca = new CustomAdapter(getActivity(), prgmNameList, prgmImages);

您还可以通过在应用程序类中创建静态变量来使用应用程序上下文:

public class Application extends android.app.Application {
    public static Context AppContext = null;

    @Override
    public void onCreate() {
        super.onCreate();
        AppContext = getApplicationContext();

        // You can use this line to solve styling problems
        // because Manifest android:theme is not working
        AppContext.setTheme(R.style.AppTheme);
    }
}

并像这样创建适配器:

CustomAdapter ca = new CustomAdapter(Application.AppContext, prgmNameList, prgmImages);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多