GridView不起作用

时间:2015-06-22 14:36:27

标签: java android gridview adapter baseadapter

任何人都可以帮助我吗?我使用BaseAdapter来填充GridView,但它不起作用。 GridView不会出现。这是以下代码

public class PlaceholderFragment extends Fragment {

public PlaceholderFragment() {
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_main, container, false);
    String [] mock ={"http://image.tmdb.org/t/p/w185//nBNZadXqJSdt05SHLqgT0HuC5Gm.jpg",
            "http://image.tmdb.org/t/p/w185//nBNZadXqJSdt05SHLqgT0HuC5Gm.jpg",
            "http://image.tmdb.org/t/p/w185//nBNZadXqJSdt05SHLqgT0HuC5Gm.jpg",
            "http://image.tmdb.org/t/p/w185//nBNZadXqJSdt05SHLqgT0HuC5Gm.jpg"};
    GridAdapter adapter = new GridAdapter(new ArrayList<String>(Arrays.asList(mock)),getActivity());
    GridView gridView = (GridView) rootView.findViewById(R.id.topWalp);

    gridView.setAdapter(adapter);
    return rootView;
}
private  class GridAdapter extends BaseAdapter{
    Context mContext;
    private ArrayList<String> data;
    private GridAdapter(ArrayList<String> list, Context mContext) {
    data=list;
        this.mContext = mContext;
    }

    @Override
    public int getCount() {
       return data.size();
    }

    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView imageView;
        if(convertView == null){
            imageView = new ImageView(mContext);
        }
     else {
        imageView = (ImageView) convertView;
    }

        Picasso.with(mContext).load(data.get(position)).fit().into(imageView);
        Toast.makeText(mContext,data.get(position),Toast.LENGTH_SHORT).show();

        return imageView;
    }
}}
public class MainActivity extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    if (savedInstanceState == null) {
        getSupportFragmentManager().beginTransaction()
                .add(R.id.container, new PlaceholderFragment())
                .commit();
    }
    getSupportActionBar().setDisplayShowHomeEnabled(true);
    getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_HOME | ActionBar.DISPLAY_SHOW_TITLE);
    getSupportActionBar().setIcon(R.drawable.ic_launcher);
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

/**
 * A placeholder fragment containing a simple view.
 */

}

fragment_main.xml

<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"
tools:context=".MainActivity$PlaceholderFragment">


<GridView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/topWalp"
    android:layout_centerVertical="true"
    android:layout_centerHorizontal="true"
    android:numColumns="2" />

activity_main.xml中

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" 
android:id="@+id/container"
android:layout_width="match_parent" 
android:layout_height="match_parent"
tools:context=".MainActivity" 
tools:ignore="MergeRootFrame" />

虽然运行它并没有显示任何错误,但屏幕为空。 Toast显示getView()函数正在调用。

0 个答案:

没有答案