列表视图的问题

时间:2014-05-21 18:48:49

标签: android listview

我的简单listview应用程序遇到问题,使用了arrayadapter。 我没有得到任何错误,但listview没有显示。我认为这个问题会影响片段活动,但我不知道如何解决它。

祝你有个美好的夜晚,并提前致谢!

public class GamesFragment extends Fragment {

ListView platformList;

String[] platform = { "Xbox One", "Playstation 4", "Xbox 360",
        "Playstation 3", "Pc" };

String[] producer = { "Microsoft", "Sony", "Microsoft", "Sony", "Pc" };

Integer[] imageId = { R.drawable.xboxone, R.drawable.xboxone,
        R.drawable.xboxone, R.drawable.xboxone,
        R.drawable.xboxone };

public static Fragment newInstance(Context context) {
    GamesFragment f = new GamesFragment();
    return f;
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_games, container,
            false);

    CustomList adapter = new CustomList(getActivity(), platform, producer, imageId);
    platformList = (ListView)rootView.findViewById(R.id.platformList);
    platformList.setAdapter(adapter);

    Toast.makeText(getActivity(), "Games Fragment", Toast.LENGTH_SHORT).show();

    return rootView;
}

}

Arrayadapter:

public class CustomList extends ArrayAdapter<String> {

private  Context context;
private final String[] platform;
private final String[] producer;
private final Integer[] imageId;


public CustomList(Context context, String[] platform, String[] producer, Integer[] imageId) {
    super(context, R.layout.list_item_platforms);

    this.context = context;
    this.platform = platform;
    this.producer = producer;
    this.imageId = imageId;
}

@Override
public View getView(int position, View view, ViewGroup parent){

    LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rootView = inflater.inflate(R.layout.list_item_platforms, null);   


//  LayoutInflater inflater = context.getLayoutInflater();
    //View rootView = inflater.inflate(R.layout.list_item_platforms, null, true);

    TextView txtPlatform = (TextView)rootView.findViewById(R.id.platform);
    TextView txtProducer = (TextView)rootView.findViewById(R.id.producer);
    ImageView imageView = (ImageView)rootView.findViewById(R.id.list_image);

    txtPlatform.setText(platform[position]);
    txtProducer.setText(producer[position]);
    imageView.setImageResource(imageId[position]);

    return rootView;
}

}

布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
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="com.example.rworlddemo.MainActivity$PlaceholderFragment" 
>

<ListView android:id="@+id/platformList"
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"
    >
</ListView>

MainActivity:

public class MainActivity extends FragmentActivity {

final String[] menuEntries = { "Games", "One", "Two", "Three" };
final String[] fragments = {
        "com.example.rworlddemo.GamesFragment",
        "com.example.rworlddemo.GamesFragment",
        "com.example.rworlddemo.GamesFragment",
        "com.example.rworlddemo.GamesFragment",
        };

private ActionBarDrawerToggle drawerToggle;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActionBar()
            .getThemedContext(), android.R.layout.simple_list_item_1,
            menuEntries);

    final DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    final ListView navList = (ListView) findViewById(R.id.drawer);

    getActionBar().setDisplayHomeAsUpEnabled(true);
    getActionBar().setHomeButtonEnabled(true);

    drawerToggle = new ActionBarDrawerToggle(this, drawer,
            R.drawable.ic_launcher, R.string.drawer_open,
            R.string.drawer_close) {

        /** Called when a drawer has settled in a completely closed state. */
        public void onDrawerClosed(View view) {

        }

        /** Called when a drawer has settled in a completely open state. */
        public void onDrawerOpened(View drawerView) {

        }
    };

    drawer.setDrawerListener(drawerToggle);

    navList.setAdapter(adapter);
    navList.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                final int pos, long id) {
            drawer.setDrawerListener(new DrawerLayout.SimpleDrawerListener() {
                @Override
                public void onDrawerClosed(View drawerView) {
                    super.onDrawerClosed(drawerView);
                    FragmentTransaction tx = getSupportFragmentManager()
                            .beginTransaction();
                    tx.replace(R.id.main, Fragment.instantiate(
                            MainActivity.this, fragments[pos]));
                    tx.commit();
                }
            });
            drawer.closeDrawer(navList);
        }
    });
    FragmentTransaction tx = getSupportFragmentManager().beginTransaction();
    tx.replace(R.id.main,
            Fragment.instantiate(MainActivity.this, fragments[0]));
    tx.commit();
}

@Override
protected void onPostCreate(Bundle savedInstanceState) {
    super.onPostCreate(savedInstanceState);
    drawerToggle.syncState();
}

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    drawerToggle.onConfigurationChanged(newConfig);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    if (drawerToggle.onOptionsItemSelected(item)) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

}

2 个答案:

答案 0 :(得分:0)

好的,我自己发现了自己的错误。我忘了在super()中包含参数平台。现在它工作正常。

但有人可以解释一下,为什么我只能添加一个参数,尽管我有三个(平台,制作人和imageId)

祝七月最好的问候

答案 1 :(得分:-1)

我可以看到你已经设置了arrayAdapter,但你从不在任何地方调用setListAdapter()方法吗?我觉得你的代码中缺少了什么。