自定义列表适配器,获得空结果

时间:2013-09-07 20:41:19

标签: android android-listview android-fragments android-listfragment

我试图使用片段在导航抽屉内创建​​自定义适配器,但是当我运行我的应用程序时,我得到一个空列表,没有任何东西..这里是我的代码

MainActivity.java

public class MainActivity extends FragmentActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // Set scrim color to drawer
    ((DrawerLayout) findViewById(R.id.drawer_layout)).setScrimColor(Color.TRANSPARENT);

    // Set navigation list in the drawer
    NavigationFragment navigationFragment = new NavigationFragment();
    getSupportFragmentManager().beginTransaction().replace(R.id.framelayout_navigation_list, navigationFragment);

    }
}

这是我的自定义适配器NavigationAdapter.java

public class NavigationAdapter extends ArrayAdapter<NavigationItem> {

private ArrayList<NavigationItem> navigationItems;

// Override constructor for ArrayAdapter
public NavigationAdapter(Context context, int viewResourceId,
                         ArrayList<NavigationItem> navigationItems) {

    super(context, viewResourceId, navigationItems);
    this.navigationItems = navigationItems;
}

// Override getView to defined custom look
public View getView(int position, View convertView, ViewGroup parent) {

    // assign the view to be converted
    View view = convertView;

    // Inflate navigation_item.xml into the new view

    LayoutInflater inflater = (LayoutInflater) getContext().
            getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    view = inflater.inflate(R.layout.navigation_item, null);


    // Get current navigation item using position
    NavigationItem item = navigationItems.get(position);

    ImageView icon = (ImageView) view.findViewById(R.id.imageview_navigation_icon);
    TextView title = (TextView) view.findViewById(R.id.textview_navigation_title);
    TextView count = (TextView) view.findViewById(R.id.textview_navigation_count);

    icon.setImageResource(item.getIcon());
    title.setText(item.getTitle());
    count.setText(item.getCount());

    return view;
    }

}


我将其替换为导航抽屉中的framelayout的片段:NavigationFragment.java

public class NavigationFragment extends ListFragment {

private ArrayList<NavigationItem> items = new ArrayList<NavigationItem>();

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    items.add(new NavigationItem(R.drawable.non_followers, 0, "Non-Followers"));
    setListAdapter(new NavigationAdapter(getActivity().getApplicationContext(),     R.layout.navigation_item, items));
}
}



NavigationItem.java

public class NavigationItem {

private int count, icon;
private String title;

// Empty Constructor
public NavigationItem() {
}

public NavigationItem(int icon, int count, String title) {
    this.icon = icon;
    this.count = count;
    this.title = title;
}

public int getCount() {
    return this.count;
}

public void setCount(int count) {
    this.count = count;
}

public int getIcon() {
    return this.icon;
}

public void setIcon(int icon) {
    this.icon = icon;
}

public String getTitle() {
    return this.title;
}

public void setTitle(String title) {
    this.title = title;
}
}

这是我想在自定义适配器navigation_item.xml中膨胀的布局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:layout_width="match_parent" >

<ImageView
    android:id="@+id/imageview_navigation_icon"
    android:contentDescription="@string/imageview_navigation_icon"
    android:layout_height="50dp"
    android:layout_width="50dp"
    android:layout_alignParentLeft="true"/>

<TextView
    android:id="@+id/textview_navigation_title"
    android:layout_toRightOf="@id/imageview_navigation_icon"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:paddingLeft="5dp"
    android:layout_centerVertical="true"
    android:textSize="14sp"
    android:textColor="@android:color/white" />

<TextView
    android:id="@+id/textview_navigation_count"
    android:layout_toRightOf="@id/textview_navigation_title"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_centerVertical="true"
    android:textSize="14sp"
    android:textColor="@android:color/white" />

</RelativeLayout>


最后我在activity_main.xml

的抽屉布局中得到了这个
<FrameLayout
   android:id="@+id/framelayout_navigation_list"
   android:layout_height="wrap_content"
   android:layout_width="match_parent" />



1 个答案:

答案 0 :(得分:2)

getSupportFragmentManager().beginTransaction().replace(R.id.framelayout_navigation_list, navigationFragment);

最后添加.commit()