2017-06-21 43 views
-1

你好,我學習如何使用Android中 GridView中,我發現這個簡單的例子 https://developer.android.com/guide/topics/ui/layout/gridview.html 我這樣做,是寫的一切,一步一個腳印,沒有錯誤, 我的應用程序運行正常,但它仍然是空的屏幕上的應用程序的名稱,但沒有gridView。 發生了什麼事?的Android GridView控件教程示例不顯示網格

MainActivity.java

package com.example.slawek.hellogridview; 

import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 

public class MainActivity extends AppCompatActivity { 

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

}

HelloGridView.java

package com.example.slawek.hellogridview; 


import android.os.Bundle; 
import android.support.v7.app.AppCompatActivity; 
import android.view.View; 
import android.widget.AdapterView; 
import android.widget.GridView; 
import android.widget.Toast; 

import com.ImageAdapter; 

public class HelloGridView extends AppCompatActivity { 

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    GridView gridview = (GridView) findViewById(R.id.gridview); 
    gridview.setAdapter(new ImageAdapter(this)); 

    gridview.setOnItemClickListener(new GridView.OnItemClickListener() { 
     public void onItemClick(AdapterView<?> parent, View v, 
           int position, long id) { 
      Toast.makeText(HelloGridView.this, "" + position, 
        Toast.LENGTH_SHORT).show(); 
     } 
    }); 
} 

}

ImageAdapter.java

包COM;

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

import com.example.slawek.hellogridview.R; 

public class ImageAdapter extends BaseAdapter { 
private Context mContext; 

public ImageAdapter(Context c) { 
    mContext = c; 
} 

public int getCount() { 
    return mThumbIds.length; 

} 

public Object getItem(int position) { 
    return null; 
} 

public long getItemId(int position) { 
    return 0; 
} 

// create a new ImageView for each item referenced by the Adapter 
public View getView(int position, View convertView, ViewGroup parent) { 
    ImageView imageView; 
    if (convertView == null) { 
     // if it's not recycled, initialize some attributes 
     imageView = new ImageView(mContext); 
     imageView.setLayoutParams(new GridView.LayoutParams(85, 85)); 
     imageView.setScaleType(ImageView.ScaleType.CENTER_CROP); 
     imageView.setPadding(8, 8, 8, 8); 
    } else { 
     imageView = (ImageView) convertView; 
    } 

    imageView.setImageResource(mThumbIds[position]); 
    return imageView; 
} 

// references to our images 
private Integer[] mThumbIds = { 
     R.drawable.sample_2, R.drawable.sample_3, 
     R.drawable.sample_4, R.drawable.sample_5, 
     R.drawable.sample_6, R.drawable.sample_7, 
     R.drawable.sample_0, R.drawable.sample_1, 
     R.drawable.sample_2, R.drawable.sample_3, 
     R.drawable.sample_4, R.drawable.sample_5, 
     R.drawable.sample_6, R.drawable.sample_7, 
     R.drawable.sample_0, R.drawable.sample_1, 
     R.drawable.sample_2, R.drawable.sample_3, 
     R.drawable.sample_4, R.drawable.sample_5, 
     R.drawable.sample_6, R.drawable.sample_7 
}; 

}

activity_main.xml中

<?xml version="1.0" encoding="utf-8"?> 
<GridView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/gridview" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:columnWidth="90dp" 
    android:numColumns="auto_fit" 
    android:verticalSpacing="10dp" 
    android:horizontalSpacing="10dp" 
    android:stretchMode="columnWidth" 
    android:gravity="center" 
    /> 

AndroidManifest.xml中

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.example.slawek.hellogridview"> 

    <application 
     android:allowBackup="true" 
     android:icon="@mipmap/ic_launcher" 
     android:label="@string/app_name" 
     android:roundIcon="@mipmap/ic_launcher_round" 
     android:supportsRtl="true" 
     android:theme="@style/AppTheme"> 
     <activity android:name=".MainActivity"> 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
    </application> 

</manifest> 
+0

粘貼您的密碼我的朋友 –

+0

您有繪圖嗎? – dustblue

+1

看起來像你在你的代碼中有一些問題。一旦你解決了它們 - 它會起作用(是的,我知道這很明顯,但沒有別的方法可以告訴你問題的方式)。你可能會問*我的代碼中的問題在哪裏?*,我會回答,只有在你顯示你的代碼後,我才能告訴你。 –

回答

0

我認爲你必須設置你的意圖過濾器來HelloGridView這樣,從刪除MainActivity

<activity android:name=".HelloGridView "> 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
     <activity android:name=".MainActivity "> 

     </activity> 
+0

與討論中的問題無關;解決方案並不是讓他的'HelloGridView'成爲主/發射器的活動,但這只是我的兩分錢。 – Shark

+0

你可以檢查他的清單先生 –

+0

我做過了,MainActivity是主要/啓動器的活動。他似乎沒有開始他的其他活動,它將顯示gridview,即「HelloGridView」活動。但它也缺少該活動的條目。 – Shark

0
 Your AndroidManifest.xml file in replace .MainActivity 
     <activity 
     android:name=".HelloGridView"> 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 
      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity>