i am trying to display image using GridView. This is the first time i using GridView, so i using the example from here and implement it to mine (i have tried the example that contained there, and it's works).
But, i have checked it many time, there's no error comes from LogCat, no Force Closed, the image didn't show. i have no idea where's the wrong part.
Here's my code:
choosepic.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#drawable/bg_inner">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/book_inner"
android:layout_marginTop="50dp"
/>
<ImageButton
android:id="#+id/homeBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/home_btn"
android:background="#null"
/>
<ImageView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:src="#drawable/bg_arrow_btn"
android:layout_alignParentRight="true"
/>
<ImageButton
android:id="#+id/nextBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/right_arrow"
android:background="#null"
android:layout_alignParentRight="true"
android:layout_marginTop="5dp"
android:layout_marginRight="7dp"
android:layout_marginLeft="7dp"
/>
<ImageButton
android:id="#+id/prevBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/left_arrow"
android:background="#null"
android:layout_toLeftOf="#+id/nextBtn"
android:layout_marginTop="5dp"
/>
<GridView
android:id="#+id/gridView1"
android:numColumns="3"
android:gravity="center"
android:columnWidth="30dp"
android:stretchMode="columnWidth"
android:layout_width="300dp"
android:layout_height="200dp"
android:layout_marginLeft="60dp"
android:layout_marginTop="70dp"
>
</GridView>
</RelativeLayout>
animalbutton.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ImageView
android:id="#+id/grid_item_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true">
</ImageView>
<TextView
android:text="TextView"
android:layout_height="wrap_content"
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_centerHorizontal="true"
android:textSize="18sp"
android:visibility="gone">
</TextView>
ImageAdapter.java
public class ImageAdapter extends BaseAdapter{
private Activity activity;
private ArrayList<String> listNm;
private ArrayList<Integer> listAnim;
public ImageAdapter(Activity activity,ArrayList<String> listName, ArrayList<Integer> listImage) {
super();
this.listNm = listName;
this.listAnim = listImage;
this.activity = activity;
}
public static class ViewHolder
{
public ImageView imgViewAnim;
public TextView txtViewAnim;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder view;
LayoutInflater inflator = activity.getLayoutInflater();
if(convertView==null)
{
view = new ViewHolder();
convertView = inflator.inflate(R.layout.animalbutton, null);
view.txtViewAnim = (TextView) convertView.findViewById(R.id.textView1);
view.imgViewAnim = (ImageView) convertView.findViewById(R.id.grid_item_image);
convertView.setTag(view);
}
else
{
view = (ViewHolder) convertView.getTag();
}
view.txtViewAnim.setText(listNm.get(position));
view.imgViewAnim.setImageResource(listAnim.get(position));
return convertView;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return 0;
}
#Override
public String getItem(int position) {
// TODO Auto-generated method stub
return listNm.get(position);
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
}
choosepic.java
public class choosepic extends Activity {
/** Called when the activity is first created. */
ImageAdapter mAdapter;
GridView gridView;
static final String[] animal = new String[] {
"cat", "cow","croc", "duck", "elephant", "giraffe", "lion", "moose", "mouse"};
private ArrayList<String> listNm;
private ArrayList<Integer> listAnim;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.choosepic);
gridView = (GridView) findViewById(R.id.gridView1);
prepare_list1();
mAdapter = new ImageAdapter(this,listNm, listAnim);
gridView.setAdapter(mAdapter);
gridView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View v, int position, long id) {
Toast.makeText(getApplicationContext(), mAdapter.getItem(position), Toast.LENGTH_SHORT).show();
}
});
}
public void prepare_list1(){
listNm = new ArrayList<String>();
listAnim = new ArrayList<Integer>();
for (int i = 0; i < animal.length; i++) {
listNm.add(animal[i]);
listAnim.add(getResources().getIdentifier("anim_"+animal[i], "drawable", getPackageName()));
}
}
}
i need some help. i appreciate any help. thank you in advance!
i think the problem is in your getCount() that returns 0 elements
instead of that make it like this
#Override
public int getCount() {
// TODO Auto-generated method stub
return listNm.size();
}
Related
I am trying to implement a Card view inside grid view. The grid view is showing perfectly; even when I touch on a grid view item it's showing nothing. But when I click on the text which is display below its work. I referred This but not working...
Thanks.
My code is as below,
// Contact_Backup.java
public class Contact_Backup extends AppCompatActivity {
#Bind(R.id.gridview)
GridView gridview;
String[] contact_screen_array;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.contact_backup);
ButterKnife.bind(this);
contact_screen_array = getResources().getStringArray(R.array.TextScreen);
String[] contact_icon_array = getResources().getStringArray(R.array.IconScreen);
String concat = "\nContacts";
gridview.setAdapter(new Custom_Adapter(getApplicationContext(),contact_icon_array,contact_screen_array,concat));
}
}
//Custom_Adapter
public class Custom_Adapter extends BaseAdapter {
Context contaxt;
String[] Array_text;
String[] Array_icon;
String concat;
LayoutInflater inflater;
public Custom_Adapter(Context context,String[] Array_icon, String[] Array_text, String concat)
{
this.contaxt = context;
this.Array_text = Array_text;
this.Array_icon = Array_icon;
this.concat=concat;
inflater = (LayoutInflater)context.
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
return Array_text.length;
}
#Override
public Object getItem(int position) {
return position;
}
#Override
public long getItemId(int position) {
return position;
}
#Bind(R.id.Screen_icon)
Button Screen_icon;
#Bind(R.id.Screen_text)
TextView Screen_text;
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
View grid;
LayoutInflater inflater = (LayoutInflater) contaxt
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
grid = new View(contaxt);
grid = inflater.inflate(R.layout.new_app_card_layout, null);
ButterKnife.bind(this, grid);
Screen_text.setText(Array_text[position]+concat);
Screen_icon.setText(Array_icon[position]);
Typeface font = Typeface.createFromAsset(contaxt.getAssets(), "fonts/fontawesome-webfont.ttf" );
Screen_icon.setTypeface(font);
} else {
grid = (View) convertView;
}
grid.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d("TAG::",""+position);
}
});
return grid;
}
}
// contact_backup.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/main_backgorund"
>
<RelativeLayout
android:background="#color/transparent"
android:padding="5.0dip"
android:id="#+id/top_bar_screen"
android:layout_weight="0.2"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:textColor="#color/newclr"
android:id="#+id/tvContacts"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/contacts"
android:shadowColor="#000000"
android:shadowDx="1.0"
android:shadowDy="1.0"
android:shadowRadius="2.0"
android:layout_centerVertical="true" />
<TextView
android:textColor="#color/newclr"
android:id="#+id/tvLastBackup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/last_backup"
android:shadowColor="#000000"
android:shadowDx="1.0"
android:shadowDy="1.0"
android:shadowRadius="2.0"
android:layout_alignParentRight="true"
android:layout_centerVertical="true" />
</RelativeLayout>
<View
android:layout_width="fill_parent"
android:layout_height="1dp"
android:id="#+id/v1"
android:layout_below="#+id/top_bar_screen"
android:background="#color/newclr"/>
<GridLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/swipe"
android:layout_below="#+id/v1"
android:paddingTop="20sp"
android:paddingLeft="10sp"
android:paddingRight="10sp"
android:layout_weight="0.6"
>
<GridView
android:id="#+id/gridview"
android:layout_width="fill_parent"
android:columnWidth="90dp"
android:numColumns="auto_fit"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:layout_height="wrap_content" />
</GridLayout>
<!-- com.startapp.android.publish.banner.Banner
android:id="#+id/startAppBanner1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_weight="0.2"
android:layout_centerHorizontal="true" /-->
</RelativeLayout>
// new_app_card_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="#+id/fssr_card"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
card_view:cardBackgroundColor="#color/transparent"
>
<LinearLayout
android:gravity="center"
android:orientation="vertical"
android:id="#+id/Contacts_Button_Layout"
android:background="#drawable/linearlayout_normal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginLeft="-1.0dip"
android:layout_weight="1.0"
>
<Button
android:textSize="35.0sp"
android:textColor="#color/newclr"
android:layout_gravity="center"
android:id="#+id/Screen_icon"
android:background="#null"
android:padding="20sp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/fa_icon_contact"
android:drawablePadding="5.0dip"
android:textAllCaps="false" />
<TextView
android:textSize="15.0sp"
android:textColor="#color/white"
android:layout_gravity="center"
android:textAlignment="center"
android:id="#+id/Screen_text"
android:background="#color/newclr"
android:padding="5sp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/contactsbackup"
android:textAllCaps="false"/>
</LinearLayout>
</android.support.v7.widget.CardView>
use this method for clicking on a gridview item:
gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// your action when gridView item is clicked
}
});
Set the listener on the actual gridview object.
gridView.setOnItemClickListener(new OnItemClickListener()...);
Try this,
public class Contact_Backup extends AppCompatActivity {
#Bind(R.id.gridview)
GridView gridview;
String[] contact_screen_array;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.contact_backup);
ButterKnife.bind(this);
contact_screen_array = getResources().getStringArray(R.array.TextScreen);
String[] contact_icon_array = getResources().getStringArray(R.array.IconScreen);
String concat = "\nContacts";
gridview.setAdapter(new Custom_Adapter(getApplicationContext(),contact_icon_array,contact_screen_array,concat));
gridview.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
for(int i=0;i<contact_screen_array.length;i++) {
Log.d("TAG::", "Position"+contact_screen_array[i]);
}
}
});
}
}
Finally, I succeed. here is the code.
Just use Textview instead of Button in // new_app_card_layout.xml.
Just Update it,
<TextView
android:textSize="35.0sp"
android:textColor="#color/newclr"
android:layout_gravity="center"
android:id="#+id/Screen_icon"
android:background="#null"
android:padding="20sp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/fa_icon_contact"
android:drawablePadding="5.0dip"
android:textAllCaps="false"
android:textAlignment="center" />
Thank You so much, All of you for help... :)
Try this i have updated your custom Adapter class.please check again with this updated code.
public class Custom_Adapter extends BaseAdapter {
Context contaxt;
String[] Array_text;
String[] Array_icon;
String concat;
LayoutInflater inflater;
public Custom_Adapter(Context context,String[] Array_icon, String[] Array_text, String concat)
{
this.contaxt = context;
this.Array_text = Array_text;
this.Array_icon = Array_icon;
this.concat=concat;
inflater = (LayoutInflater)context.
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
return Array_text.length;
}
#Override
public Object getItem(int position) {
return position;
}
#Override
public long getItemId(int position) {
return position;
}
#Bind(R.id.Screen_icon)
Button Screen_icon;
#Bind(R.id.Screen_text)
TextView Screen_text;
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
View grid;
LayoutInflater inflater = (LayoutInflater) contaxt
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
grid = new View(contaxt);
grid = inflater.inflate(R.layout.new_app_card_layout, null);
ButterKnife.bind(this, grid);
Screen_text.setText(Array_text[position]+concat);
Screen_icon.setText(Array_icon[position]);
Typeface font = Typeface.createFromAsset(contaxt.getAssets(), "fonts/fontawesome-webfont.ttf" );
Screen_icon.setTypeface(font);
grid.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d("TAG::",""+position);
}
});
} else {
grid = (View) convertView;
grid.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d("TAG::",""+position);
}
});
}
return grid;
}
}
Hope this helps you out !
I am creating a gridview with different images and texts from a class containing those images and texts in a fragment. The GridView is inside a TabHost. The TabHost is working fine. I can see other buttons in the same view. But nothing is inside the GridView. Here is the code:
public class MenuPage extends Fragment{
GridView CategoryGridview;
View MainMenuView, HomePageView, GalleryView;
#Override
public void onAttach(Activity activity) {
// TODO Auto-generated method stub
super.onAttach(activity);
}
#Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final Context context = getActivity();
this.MainMenuView = inflater.inflate(R.layout.mainmenupage, container, false);
this.GalleryView = inflater.inflate(R.layout.gallery, container, false);
this.CategoryGridview = (GridView)GalleryView.findViewById(R.id.gridViewGallery);
CategoryGridview.setAdapter(new GalleryAdapter(getActivity()));
:
:
:
return MainMenuView;
}
Adapter:
public class GalleryAdapter extends BaseAdapter{
ArrayList<Gallery> list;
Context context;
GalleryAdapter(Context context){
this.context = context;
list = new ArrayList<Gallery>();
int[] TempGalleryId = {R.drawable.a, R.drawable.b, R.drawable.c, R.drawable.d, R.drawable.e, R.drawable.f, R.drawable.g, R.drawable.h, R.drawable.i, R.drawable.j, R.drawable.k, R.drawable.l, R.drawable.m, R.drawable.n, R.drawable.o, R.drawable.p, R.drawable.q, R.drawable.r, R.drawable.s};
String[] TempGalleryName = {"a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s"};
for (int i = 0; i < 19; i++){
Gallery TempGallery = new Gallery(TempGalleryId[i], TempGalleryName[i]);
list.add(TempGallery);
}
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return list.size();
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return list.get(position);
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View row = convertView;
ViewHolder holder = null;
if(row == null){
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.single_gallery, parent, false);
holder = new ViewHolder(row);
row.setTag(holder);
}
else{
holder = (ViewHolder) row.getTag();
}
Gallery temp = list.get(position);
holder.imageViewGallery.setImageResource(temp.GalleryId);
return row;
}
}
class ViewHolder{
ImageView imageViewGallery;
ViewHolder(View v)
{
imageViewGallery = (ImageView) v.findViewById(R.id.imageViewGallery);
}
}
class Gallery{
int GalleryId;
String GalleryName;
Gallery(int GalleryId, String GalleryName){
this.GalleryId = GalleryId;
this.GalleryName = GalleryName;
}
}
In mainmenupage.xml, one of the tab include gallery.xml file for animation with a gridview imageViewGallery and some buttons in it.
<LinearLayout
android:orientation="vertical"
android:id="#+id/tab2"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ListView
android:id="#+id/DinnerList"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
<include
android:id="#+id/gallery"
layout="#layout/gallery" />
</LinearLayout>
SingleGallery.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:id="#+id/imageViewGallery"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:src="#drawable/a" />
</RelativeLayout>
This is Gallery.xml
<RelativeLayout
android:id="#+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="#+id/btnBack"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="Back" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/button1"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_toRightOf="#+id/button1"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
<GridView
android:id="#+id/gridViewGallery"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/btnBack"
android:numColumns="auto_fit"
android:horizontalSpacing="15dp"
android:verticalSpacing="15dp"
android:stretchMode="spacingWidthUniform">
</GridView>
</RelativeLayout>
I tried change
CategoryGridview.setAdapter(new GalleryAdapter(getActivity()));
to
CategoryGridview.setAdapter(new GalleryAdapter(context));
and
this.CategoryGridview = GridView)MainMenuView.findViewById(R.id.gridViewGallery);
to
this.CategoryGridview = (GridView)GalleryView.findViewById(R.id.gridViewGallery);
still doesn't work.
I also tried open a new project with extend Activity(){}. It work fine with
CategoryGridview.setAdapter(new GalleryAdapter(this));
So I guess the problem is somewhere else. Can anyone help?
I have a model class. Now from my activity i want to set the values in model class & show them in gridview using my custom adapter. After that i need to store the object in a variable(class type) from gridview's onItemClick. I have done the below codes. But its not working. Where did i go wrong?
activity_country.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"
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=".CountryActivity" >
<TextView
android:id="#+id/nTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="21dp"
android:layout_marginTop="26dp"
android:text="Name" />
<TextView
android:id="#+id/aTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/nTextView"
android:layout_below="#+id/nTextView"
android:layout_marginTop="24dp"
android:text="About" />
<EditText
android:id="#+id/CountryNameEditText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/nTextView"
android:layout_alignBottom="#+id/nTextView"
android:layout_marginLeft="48dp"
android:layout_toRightOf="#+id/nTextView"
android:ems="10" >
<requestFocus />
</EditText>
<EditText
android:id="#+id/CountryAboutEditText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/CountryNameEditText"
android:layout_alignTop="#+id/aTextView"
android:ems="10" />
<Button
android:id="#+id/saveCountryButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/CountryAboutEditText"
android:layout_below="#+id/CountryAboutEditText"
android:layout_marginLeft="16dp"
android:layout_marginTop="22dp"
android:text="Save"
android:onClick="saveCountry"
/>
<GridView
android:id="#+id/countryGridView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/saveCountryButton"
android:layout_centerHorizontal="true"
android:numColumns="2">
</GridView>
</RelativeLayout>
CountryActivity.java
public class CountryActivity extends Activity
{
ArrayList<Country> countries = new ArrayList<Country>();
Country aCountry;
EditText CountryNameTxtBox;
EditText CountryAboutTxtBox;
GridView countrygGridView;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_country);
// Show the Up button in the action bar.
setupActionBar();
CountryNameTxtBox = (EditText)findViewById(R.id.CountryNameEditText);
CountryAboutTxtBox = (EditText)findViewById(R.id.CountryAboutEditText);
countrygGridView = (GridView)findViewById(R.id.countryGridView);
}
public void saveCountry(View view)
{
String txtName = CountryNameTxtBox.getText().toString();
String txtAbout = CountryAboutTxtBox.getText().toString();
showGrid();
}
public void showGrid()
{
/*
ArrayAdapter<Country> adapter = new ArrayAdapter<Country>(this,android.R.layout.simple_list_item_1, countries);
countrygGridView.setAdapter(adapter);
*/
countrygGridView.setAdapter(new myAdapter(this,countries));
countrygGridView.setOnItemClickListener(new OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> parent, View v,int position, long id)
{
for(Country mCountry: countries)
{
if(countries.contains(aCountry))
{
Toast.makeText(CountryActivity.this,countries.get(position).getName(), 2000).show();
}
}
}
});
}
countrygrid.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<TextView
android:id="#+id/label1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#+id/label"
android:layout_marginTop="15sp"
android:textSize="15sp" >
</TextView>
<TextView
android:id="#+id/label2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#+id/label"
android:layout_marginTop="15sp"
android:textSize="15sp" >
</TextView>
</LinearLayout>
myAdapter.java
public class myAdapter extends BaseAdapter
{
Context mycontext;
ArrayList<Country> countries;
public myAdapter(Context c, ArrayList<Country> obj)
{
this.mycontext=c;
this.countries = obj;
}
public int getCount() {
// TODO Auto-generated method stub
return countries.size();
}
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return countries.get(arg0);
}
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return 0;
}
public View getView(int position, View convertview, ViewGroup parent)
{
LayoutInflater inflater = (LayoutInflater) mycontext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View gridView;
if(convertview==null)
{
gridView =new View(mycontext);
gridView =inflater.inflate(R.layout.countrygrid, null);
TextView txtvw1 = (TextView) gridView.findViewById(R.id.label1);
txtvw1.setText(countries.get(position).getName());
TextView txtvw2 = (TextView) gridView.findViewById(R.id.label2);
txtvw2.setText(countries.get(position).getAbout());
}
else
{
gridView = (View) convertview;
}
return gridView;
}
}
*Its showing error in countryActivity.java- when i am trying to set the adapter inside showGrid() *
countrygGridView.setAdapter(new myAdapter(this,countries));
public class myAdapter
{
does not extend anything
It should be
public class myAdapter extends BaseAdapter //orArrayAdapter
{
If you still have a problem post the stacktrace for further help
Good day to all.
I am currently working on a customized ListView which is not inflating (is not becoming visible) inside the activity.
Below are the xml and ListView adapter classes.
main.xml
<LinearLayout
android:id="#+id/llListView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:paddingTop="2dp"
android:paddingBottom="2dp"
android:layout_below="#+id/rlFirstRow">
<ListView
android:id="#+id/lvErrorsReport"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/border"/>
</LinearLayout>
listview_row.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/llError"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_alignParentTop="true">
<TextView
android:id="#+id/txtErrorInfo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textStyle="bold"
android:textSize="20dp"/>
</LinearLayout>
<LinearLayout
android:id="#+id/llStatus"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_below="#+id/llError">
<TextView
android:id="#+id/txtStatus"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="Status: "/>
<TextView
android:id="#+id/txtStatusInfo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textStyle="italic"/>
</LinearLayout>
<LinearLayout
android:id="#+id/llMaterials"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_below="#+id/llStatus">
<TextView
android:id="#+id/txtMaterials"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="Materials: "/>
<TextView
android:id="#+id/txtMaterialsInfo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textStyle="italic"/>
</LinearLayout>
<LinearLayout
android:id="#+id/llButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_below="#+id/llMaterials">
<Button
android:id="#+id/btnShowMaterials"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Show Materials"
android:visibility="gone"/>
</LinearLayout>
</RelativeLayout>
BaseAdapter class
public class RepairReportListViewAdapter extends BaseAdapter
{
ArrayList<String> errors;
ArrayList<String> statuses;
ArrayList<Boolean> materials;
Context mContext;
LayoutInflater inflater;
public RepairReportListViewAdapter(Context context, ArrayList<String> err, ArrayList<String> stat, ArrayList<Boolean> mat)
{
this.mContext = context;
this.errors = err;
this.statuses = stat;
this.materials = mat;
inflater = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount()
{
return 0;
}
#Override
public Object getItem(int position)
{
return null;
}
#Override
public long getItemId(int position)
{
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
ViewHolder holder;
if(convertView == null)
{
holder = new ViewHolder();
convertView = inflater.inflate(R.layout.repairreport_listview, null);
holder.error = (TextView)convertView.findViewById(R.id.txtErrorInfo);
holder.status = (TextView)convertView.findViewById(R.id.txtStatusInfo);
holder.material = (TextView)convertView.findViewById(R.id.txtMaterialsInfo);
holder.showMaterials = (Button)convertView.findViewById(R.id.btnShowMaterials);
convertView.setTag(holder);
}
else
{
holder = (ViewHolder)convertView.getTag();
}
holder.error.setText(this.errors.get(position).toString());
holder.status.setText(this.statuses.get(position).toString());
if(this.materials.get(position) == true)
{
holder.material.setText("Materials where used to fix this error. Click on the button below to view the materials.");
holder.showMaterials.setVisibility(Button.VISIBLE);
holder.showMaterials.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
//LATER
}
});
}
else
{
holder.material.setText("No materials where used to fix this error.");
}
return convertView;
}
static class ViewHolder
{
TextView error;
TextView status;
TextView material;
Button showMaterials;
}
}
I've already tried changing the layouts as suggested here but the ListView is still not revealed.
Any ideas why it is not showing? Sorry for the code dump but I really cannot figure out what the heck is going on.
P.S. I did set the adapter to the ListView in the main activity.
adapter = new RepairReportListViewAdapter(this, errorNames, statuses, materialsInUse);
lvErrors.setAdapter(adapter);
your ListView does not inflate nothing because getCount is returning 0
change
#Override
public int getCount()
{
return 0;
}
with
#Override
public int getCount()
{
return (stat == null) ? 0 : stat.size();
}
Also, instead of keeping three differents ArrayList, you can create a class that holds all the info you need and an ArrayList of this class
I want to make an app with a interface like google+ or evernote like the following , how to perform these ui? Use GridView in Android?
They are similar to that. But they have their own term called DashBoard.
Here is a link on a tutorial,
DashBoard..
Here is a link which explains few things to you,
http://android-developers.blogspot.in/2010/05/twitter-for-android-closer-look-at.html
And also this question here, which was previously discussed about it,
Android Dashboard Pattern
And this looks to be the suggested way to do it,
<com.google.android.apps.iosched.ui.widget.DashboardLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button android:id="#+id/home_btn_schedule"
style="#style/DashboardButton"
android:text="#string/btn_schedule"
android:drawableTop="#drawable/home_btn_schedule" />
<Button android:id="#+id/home_btn_map"
style="#style/DashboardButton"
android:text="#string/btn_map"
android:drawableTop="#drawable/home_btn_map" />
<Button android:id="#+id/home_btn_sessions"
style="#style/DashboardButton"
android:text="#string/btn_sessions"
android:drawableTop="#drawable/home_btn_sessions" />
<Button android:id="#+id/home_btn_starred"
style="#style/DashboardButton"
android:text="#string/btn_starred"
android:drawableTop="#drawable/home_btn_starred" />
<Button android:id="#+id/home_btn_vendors"
style="#style/DashboardButton"
android:text="#string/btn_vendors"
android:drawableTop="#drawable/home_btn_vendors" />
<Button android:id="#+id/home_btn_announcements"
style="#style/DashboardButton"
android:text="#string/btn_announcements"
android:drawableTop="#drawable/home_btn_announcements" />
Taken From Here..
public class Dashboard extends Activity
implements OnItemClickListener {
Context con;
static LauncherIcon[] ICONS = {
new LauncherIcon(R.drawable.breakfasdd, "text1"),
new LauncherIcon(R.drawable.lunch, "text2"),
new LauncherIcon(R.drawable.dinner1, "text3"),
new LauncherIcon(R.drawable.syn1, "text4"),
};
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dashboard);
GridView gridview = (GridView) findViewById(R.id.dashboard_grid);
gridview.setAdapter(new ImageAdapter(this));
gridview.setOnItemClickListener(this);
}
static class LauncherIcon {
final String text;
final int imgId;
public LauncherIcon(int imgId, String text) {
super();
this.imgId = imgId;
this.text = text;
}
}
static class ImageAdapter extends BaseAdapter {
private Context mContext;
public ImageAdapter(Context c) {
mContext = c;
}
#Override
public int getCount() {
return ICONS.length;
}
#Override
public LauncherIcon getItem(int position) {
return null;
}
#Override
public long getItemId(int position) {
return 0;
}
static class ViewHolder {
public ImageView icon;
public TextView text;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
ViewHolder holder;
if (v == null) {
LayoutInflater vi = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.dashboard_icon, null);
holder = new ViewHolder();
holder.text = (TextView) v
.findViewById(R.id.dashboard_icon_text);
holder.icon = (ImageView) v
.findViewById(R.id.dashboard_icon_img);
v.setTag(holder);
} else {
holder = (ViewHolder) v.getTag();
}
holder.icon.setImageResource(ICONS[position].imgId);
holder.text.setText(ICONS[position].text);
return v;
}
}
and to get item selected
#Override
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
int pos = position;{
}
dashboard_icon.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:id="#+id/dashboard_icon_img"
android:layout_width="fill_parent"
android:layout_height="96.0dip"
android:scaleType="fitCenter" />
<TextView
android:id="#+id/dashboard_icon_text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="20.0dip"
android:layout_marginTop="2.0dip"
android:gravity="center"
android:textAppearance="?android:textAppearanceSmall"
android:textColor="#android:color/black" />
</LinearLayout>
dashboard.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="#+id/back"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|center_horizontal"
android:layout_weight="1"
android:text="BACK" />
<GridView
android:id="#+id/dashboard_grid"
style="#style/dashboard"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:listSelector="#android:color/transparent"
android:stretchMode="columnWidth"
android:verticalSpacing="20.0dip" />
</RelativeLayout>