android GridView does not work in my emulator:does not display it though no error is shown
-
25-06-2021 - |
Вопрос
I tried everything. I tested by copy pasting the example here from developer website but still didnt do work. it just shows a blank space whee the gridview was supposed to come. Here is my code.
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();
}
});
}
}
MyAdapter.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"};
}
enterapp.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>
Решение
you mistake lies in your adapter as
@Override
public int getCount() {
// TODO Auto-generated method stub
return 0;
}
saying you dont have any item to display that is why its not showing any element.
to solve this change your getCount()
like this
@Override
public int getCount() {
// TODO Auto-generated method stub
return thumbsID.length;
}
Другие советы
getCount returns 0, that's why!
public int getCount() {
// TODO Auto-generated method stub
return text.lenght;
}
You are value is not coming because in your adapter getCount()
method is returning 0. put some values on that. or if u put the list as argument in adapter, the u can take that list size as the return for the getCount().
Не связан с StackOverflow