0

我什么都试过了。我通过从开发者网站复制粘贴示例进行测试,但仍然没有工作。它只是显示一个空白空间,而 gridview 应该来。这是我的代码。
EnterApp.java

package com.locationremind.app;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.Toast;

public class EnterApp extends Activity {
    Context con;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        con=this;
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.enterapp);
        GridView gridView=(GridView) findViewById(R.id.grid_menu);
        gridView.setAdapter(new MyAdapter(this));
        gridView.setOnItemClickListener(new OnItemClickListener(){

            @Override
            public void onItemClick(AdapterView<?> parent, View v, int position,
                    long id) {
                // TODO Auto-generated method stub
                if(position==0)
                    startActivity(new Intent(getBaseContext(),LocationReminderActivity.class));
                else
                    Toast.makeText(getBaseContext(), "Item"+position+"not Allocated yet",Toast.LENGTH_SHORT).show();

            }

        });
    }


}

我的适配器.java

package com.locationremind.app;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.TextView;

public class MyAdapter extends BaseAdapter {
    Context context;

    public MyAdapter(Context c) {
        context = c;

    }
    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public Object getItem(int pos) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        TextView tv;
        /*ImageView imageView;
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        Display display = wm.getDefaultDisplay();*/
        if(convertView==null)
        {    
            tv=new TextView(context);
            tv.setLayoutParams(new GridView.LayoutParams(85,85));

        }
        else
            tv=(TextView)convertView;
        tv.setBackgroundResource(thumbsID[position]);
        tv.setMinimumHeight(128);
        tv.setMinimumWidth(128);
        tv.setLayoutParams(new GridView.LayoutParams(85,85));
        tv.setText(text[position]);
        return tv;
    }

    private Integer[] thumbsID={R.drawable.gridview_icon,R.drawable.icn};
    private String[] text={"Select Location Using GPS","Select Location Using Wi-Fi",
                          "Select Location Using Network","Settings"};
}

输入应用程序.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:id="@+id/Rlayout"
     >
    <ImageView 
        android:id="@+id/img_icon"
        android:src="@drawable/android"
        android:maxWidth="80dp"
        android:maxHeight="80dp"
        android:layout_width="64px"
        android:layout_height="64px"
        android:contentDescription="logo-icon"/>
    <GridView 
        android:id="@+id/grid_menu"
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent"
        android:columnWidth="90dp"
        android:numColumns="auto_fit"
        android:verticalSpacing="10dp"
        android:horizontalSpacing="10dp"
        android:stretchMode="columnWidth"
        android:gravity="center"/>

</LinearLayout>  
4

3 回答 3

1

你的错误在于你的适配器

@Override
public int getCount() {
    // TODO Auto-generated method stub
    return 0;
}

说您没有任何要显示的项目,这就是它不显示任何元素的原因。

解决这个改变你getCount()这样

@Override
public int getCount() {
    // TODO Auto-generated method stub
    return thumbsID.length;
}
于 2012-08-10T06:14:22.420 回答
0

getCount 返回 0,这就是原因!

  public int getCount() {
        // TODO Auto-generated method stub
        return text.lenght;
    }
于 2012-08-10T06:15:50.610 回答
0

您的价值不会到来,因为在您的适配器getCount()方法中返回 0。在其上放置一些值。或者,如果您将列表作为参数放入适配器中,您可以将该列表大小作为 getCount() 的返回值。

于 2012-08-10T06:25:17.173 回答