片段中的ListView不显示

时间:2013-08-15 22:13:13

标签: android android-listview android-fragments

我正在尝试使用自定义布局填充片段内的listView。但是当我启动应用程序时,未加载listview的内容(这是使用数组适配器获得的)。 这里是我加载listView的代码:

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.activity_start, container, false);
        ProfilesDatabaseHelper dbHelper = ac.getDbManager();
        ProfileManagerDAO dbDao = ac.getProfileManagerDAO();
        //TODO mettere profilehandler dentroo profileapp.
        ProfileHandler handler = new ProfileHandler(view.getContext());
        Profile profile = handler.getCurrentProfile();
        TextView label = (TextView) view.findViewById(R.id.labelvlm);
        label.setText("TEST");
        if(handler!=null){
            label.setText("Ring Volume: " + profile.getRingVolume() + "Max: " 
                    + handler.getMaxVolume(AudioManager.STREAM_RING));
            label.append("\tVoice Call Volume: " + profile.getCallVolume()
                    + " Max: " + handler.getMaxVolume(AudioManager.STREAM_VOICE_CALL));
            //manager.setStreamVolume(AudioManager.STREAM_VOICE_CALL, 5, 0); 
            label.append("\tWIFI Status: " + handler.isWifiActive());           
        }
        Ringtone tone = RingtoneManager.getRingtone(view.getContext(), Settings.System.DEFAULT_RINGTONE_URI);
        if(tone!=null){
            label.append("\n" + tone.getTitle(view.getContext()));  
        }
        SeekBar bar = (SeekBar) view.findViewById(R.id.ringtonevolumeBar);
        bar.setMax(handler.getMaxVolume(AudioManager.STREAM_RING));     
        bar.setProgress(profile.getRingVolume());
        ArrayList<String> profiles = ac.getProfileManagerDAO().getprofileNamesList();
        if(profiles!=null){
            Log.d(TAG, "List size: " + profiles.size());
            ListView profileslist = (ListView) view.findViewById(R.id.profileslist);            
            adapter = new ProfilesArrayAdapter(view.getContext(), R.layout.list_item, profiles);
            profileslist.setAdapter(adapter);
//          List<String> testing = new ArrayList<String>();
//            testing.add("Hey");
//            testing.add("Hey Do");
//            testing.add("Hey It");
//            testing.add("Hey Please");
//            ArrayAdapter moviesAdapter = new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_1, testing);
//            profileslist.setAdapter(moviesAdapter);
            //profileslist.setA
        }
        return view;
    }

这里是CustomAdapter的代码:

public class ProfilesArrayAdapter extends
    ArrayAdapter<HashMap<String, Integer>> {

ArrayList<HashMap<String, Integer>> entries;
int resourceId;
Context context;

static class ProfileItemHandler {
    TextView profileId;
    TextView profileName;
}

public ProfilesArrayAdapter(Context context, int layoutResourceId, ArrayList<HashMap<String,Integer>> profiles) {
    super(context, layoutResourceId);
    this.context = context;
    this.resourceId = layoutResourceId;
    this.entries = profiles;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View row = convertView;
    ProfileItemHandler handler = null;
    if(convertView==null){
        LayoutInflater inflater = ((Activity)context).getLayoutInflater();
        row = inflater.inflate(R.layout.list_item, parent, false);
        handler = new ProfileItemHandler();
        handler.profileId = (TextView) row.findViewById(R.id.profileName);
        handler.profileName = (TextView) row.findViewById(R.id.profileId);
        row.setTag(handler);
    } else {
        handler = (ProfileItemHandler) row.getTag();
    }
    HashMap<String, Integer> entry = this.getItem(position);
    for(String cur_entry: entry.keySet()){
        handler.profileId.setText(cur_entry);
        handler.profileName.setText(entry.get(cur_entry));
    }               
    return row;
}

}

这是列表项布局的xml:

    <LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="match_parent"
  android:layout_height="match_parent">
    <TextView android:layout_height="wrap_content" android:id="@+id/profileId" android:text="TextView" android:textAppearance="?android:attr/textAppearanceLarge" android:layout_width="wrap_content"></TextView>
    <TextView android:text="TextView" android:id="@+id/profileName" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>

</LinearLayout>

这里的主要布局是:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_start"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".StartActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/labelvlm"
        android:text="@string/hello_world" />

    <SeekBar
        android:id="@+id/ringtonevolumeBar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/labelvlm"        
        android:layout_marginTop="122dp" />

    <ListView
        android:id="@+id/profileslist"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignLeft="@+id/ringtonevolumeBar"
        android:layout_below="@+id/ringtonevolumeBar"
        android:layout_marginTop="68dp" >
    </ListView>

</RelativeLayout>

我没有收到任何错误消息,但是当我启动应用程序时,listView中没有显示任何项目,并且从不调用我的适配器的getView方法。有什么想法吗?

2 个答案:

答案 0 :(得分:2)

我发现了问题。 它在构造函数中:

public ProfilesArrayAdapter(Context context, int layoutResourceId, ArrayList<HashMap<String,Integer>> profiles) {
    super(context, layoutResourceId);
    this.context = context;
    this.resourceId = layoutResourceId;
    this.entries = profiles;
}

在第一行,对超级构造函数的调用是错误的。 事实上,正确的呼叫应该是:

super(context, layoutResourceId, profiles);

这样,现在列表正确填充。

答案 1 :(得分:1)

我的第一个猜测是(因为你没有收到任何错误信息)这一行:

ArrayList<HashMap<String, Integer>> profiles = ac.getProfileManagerDAO().getprofileList();

返回一个空的ArrayList ,一般是size = 0或 null 。因此,ListView不包含任何项目,并且从不调用getView()。

你能否发布你的Fragment和你的Activity的布局文件,以及你片段的onCreateView()方法和你的Activity的onCreate()方法。

此外,我问自己为什么这个代码:

ArrayList<HashMap<String, Integer>> profiles = ac.getProfileManagerDAO().getprofileList();
    if(profiles!=null){
        Log.d(TAG, "List size: " + profiles.size());
        ListView profileslist = (ListView) getActivity().findViewById(R.id.profileslist);           
        adapter = new ProfilesArrayAdapter(getActivity(), R.layout.list_item, profiles);
        profileslist.setAdapter(adapter);
    }

不在Fragments onCreateView()方法中? 这样你就不会调用getActivity()。findViewById(...)但你会直接在膨胀的布局上调用inflatedview.findViewById(...)

<强>更新 您的活动是否扩展了FragmentActivity?您的活动代码中的位置是片段&#34;已添加&#34;或者&#34;替换&#34;布局?