I have a ListView but I can´t put images on it.
The error is in Activity_listView at holder.image.setImageDrawable(datos[position].getImage());
The error message is
The method setImageDrawable(Drawable) in the type ImageView is not applicable
for the arguments (ImageView)
Any idea for solve this?
Activity (Activity_listView):
package com.simarro.asteroids;
import android.support.v7.app.ActionBarActivity;
import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
public class Activity_listView extends ActionBarActivity {
private ListView lstOpciones;
ImageView asteroid = (ImageView)findViewById(R.id.img_asteroid);
private puntuacion[] datos = new puntuacion[] {
new puntuacion(asteroid,"Pepito Dominguez", "15489"),
new puntuacion(asteroid,"Pedro Martínez", "16598"),
new puntuacion(asteroid,"Paco Perez", "16332"),
new puntuacion(asteroid,"Rosana Fernandez", "18792"),
new puntuacion(asteroid,"Paco Jones", "960") };
class AdaptadorTitulares extends ArrayAdapter<puntuacion> {
Activity context;
AdaptadorTitulares(Activity context) {
super(context, R.layout.puntuacion, datos);
this.context = context;
}
public View getView(int position, View convertView, ViewGroup parent) {
View item = convertView;
ViewHolder holder;
if (item == null) {
LayoutInflater inflater = context.getLayoutInflater();
item = inflater.inflate(R.layout.puntuacion, null);
holder = new ViewHolder();
holder.image=(ImageView)item.findViewById(R.id.img_asteroid);
holder.jugador = (TextView) item.findViewById(R.id.LblJugador);
holder.puntuacion = (TextView) item.findViewById(R.id.LblPuntuacion);
item.setTag(holder);
} else {
holder = (ViewHolder) item.getTag();
}
holder.image.setImageDrawable(datos[position].getImage());
holder.jugador.setText(datos[position].getJugador());
holder.puntuacion.setText(datos[position].getPuntuacion());
return (item);
}
}
static class ViewHolder {
ImageView image;
TextView jugador;
TextView puntuacion;
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_view);
AdaptadorTitulares adaptador = new AdaptadorTitulares(this);
lstOpciones = (ListView) findViewById(R.id.LstOpciones);
//¡FUUUUUUUUU-SIÓN!
lstOpciones.setAdapter(adaptador);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_list_view, 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();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
The xml of content for layout with listView (puntuacion.xml):
<?xml version="1.0" encoding="utf-8"?>
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:columnCount="3"
android:orientation="horizontal" >
<ImageView
android:id="#+id/img_asteroid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/asteroid" />
<TextView
android:id="#+id/LblJugador"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#ff0"
android:textSize="30px"
android:textStyle="bold" />
<TextView
android:id="#+id/LblPuntuacion"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="30px"
android:textStyle="normal" />
</GridLayout>
The layout where is the listView (listView.xml):
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ListView android:id="#+id/LstOpciones"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
The class for get values (Puntuacion)
package com.simarro.asteroids;
import android.graphics.drawable.Drawable;
import android.widget.ImageView;
public class puntuacion
{
private ImageView image;
private String jugador;
private String puntuacion;
public puntuacion(ImageView img, String tit, String sub){
image=img;
puntuacion = sub;
jugador = tit;
}
public ImageView getImage(){
return image;
}
public String getJugador(){
return jugador;
}
public String getPuntuacion(){
return puntuacion;
}
}
Instead of returning ImageView in getImage(), return a Drawable and change it to
public Drawable getImage() {
return image.getDrawable();
}
getDrawable() Docs
As the error says, the method setImageDrawable() takes a Drawable type but you are passing it an ImageView
Related
I created a custom adapter to fill the item i have depending on how many elements there is but it only fills the first item and i can't figure out why
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" android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin" tools:context=".MainActivity">
<TextView android:text="#string/hello_world" android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/textView"
android:textSize="25sp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<ListView
android:id="#+id/customList"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/textView" />
</RelativeLayout>
item.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">
<TextView
android:id="#+id/item_text_2"
android:layout_width="116dp"
android:layout_height="wrap_content"
android:textSize="15sp"
android:text="Default Text 2"
android:layout_gravity="right"
android:layout_below="#+id/item_text_1"
android:layout_centerHorizontal="true" />
<TextView
android:id="#+id/item_text_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15sp"
android:text="Default Text 1"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:id="#+id/item_text_3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15sp"
android:text="Default Text 3"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="42dp" />
Custom Class
public class CustomClass {
private String txt1, txt2, txt3;
public CustomClass(String txt1, String txt2, String txt3) {
this.txt1 = txt1;
this.txt2 = txt2;
this.txt3 = txt3;
}
public String getTxt1() {
return txt1;
}
public String getTxt2() {
return txt2;
}
public String getTxt3() {
return txt3;
}
}
MainActivity
import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private List<CustomClass> lst_stuff = new ArrayList<>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
populateCustomClass();
populateListView();
}
private void populateCustomClass()
{
lst_stuff.add(new CustomClass("First", "I Got", "FIRST!!!!!"));
lst_stuff.add(new CustomClass("Second", "How did you...", "you know what GG"));
lst_stuff.add(new CustomClass("Third", "I Got", "Last?!! ... I am just too good to be first"));
}
private void populateListView()
{
ArrayAdapter<CustomClass> adapter = new CustomAdapter();
ListView lst = (ListView) findViewById(R.id.customList);
lst.setAdapter(adapter);
}
private class CustomAdapter extends ArrayAdapter<CustomClass>
{
public CustomAdapter()
{
super(MainActivity.this, R.layout.item, lst_stuff);
}
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
View itemView = convertView;
if (itemView == null)
itemView = getLayoutInflater().inflate(R.layout.item, parent, false);
try
{
CustomClass customClass = lst_stuff.get(position);
TextView textView_1 = (TextView) findViewById(R.id.item_text_1);
textView_1.setText(customClass.getTxt1());
TextView textView_2 = (TextView) findViewById(R.id.item_text_2);
textView_2.setText(customClass.getTxt2());
TextView textView_3 = (TextView) findViewById(R.id.item_text_3);
textView_3.setText(customClass.getTxt3());
}
catch (NullPointerException exception)
{
Toast.makeText(MainActivity.this, "Exception", Toast.LENGTH_SHORT).show();
exception.printStackTrace();
}
return itemView;
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.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);
}
}
is there something missing or something doing wrong?
All seems to be ok, but I would change what is on the method getView() by this:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) MainActivity.this.
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.item, parent, false);
CustomClass customClass = lst_stuff.get(position);
((TextView) rowView.findViewById(R.id.item_text_1)).
setText(customClass.getTxt1());
((TextView) rowView.findViewById(R.id.item_text_2)).
setText(customClass.getTxt2());
((TextView) rowView.findViewById(R.id.item_text_3)).
setText(customClass.getTxt3());
return rowView;
}
I think that you must obtain first a instance of inflater, and then inflate your view (rowView) with your layout (R.layout.item).
Good luck!
I want my GridView to display only 16 random letters, a 4x4 tile and if it's possible I don't want any space between the letters.
I also want it to display its corresponding text to a TextView to form a word. I put it in a toast for now to show how it should be. But it only shows the corresponding letter of the image. How can I achieve that? I dont have any idea on how should i do it.
Any help will be appreciated.
New to Android Programming
grid_row.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"
android:padding="5dp" >
<ImageView
android:id="#+id/imageView"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginRight="1dp"
android:layout_marginEnd="1dp"
android:contentDescription="#string/hello_world"
android:src="#drawable/ic_launcher" >
</ImageView>
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="2dp"
android:textSize="15sp"
android:visibility="gone" >
</TextView>
</LinearLayout>
activity_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"
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="com.grid.gridgame.MainActivity" >
<TextView
android:id="#+id/wordHere"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="1234567890"/>
<GridView
android:id="#+id/gridViewCustom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="1dp"
android:columnWidth="80dp"
android:gravity="center"
android:horizontalSpacing="0dp"
android:numColumns="4"
android:stretchMode="columnWidth"
android:verticalSpacing="0dp" />
</RelativeLayout>
MainActivity.java
package com.grid.gridgame;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
GridView gridView;
String word;
int rando;
static final String[] setOne = new String[] {
"A", "M","C", "Qu",
"Z", "B", "D", "A",
"E", "I", "E", "L",
"M", "N", "T", "P" };
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gridView = (GridView) findViewById(R.id.gridViewCustom);
gridView.setAdapter(new GridViewCustomAdapter(this, setOne));
gridView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
Toast.makeText(
getApplicationContext(),
((TextView) v.findViewById(R.id.textView))
.getText(), Toast.LENGTH_SHORT).show();
}
});
}
#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();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
GridViewCustomAdapter
package com.grid.gridgame;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class GridViewCustomAdapter extends BaseAdapter {
private Context context;
private final String[] setOne;
public GridViewCustomAdapter(Context context, String[] setOne) {
this.context = context;
this.setOne = setOne;
}
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View gridView;
if (convertView == null) {
convertView = new ImageView(context);
gridView = new View(context);
// get layout from mobile.xml
gridView = inflater.inflate(R.layout.grid_row, null);
// set value into TextView
TextView textView = (TextView) gridView
.findViewById(R.id.textView);
textView.setText(setOne[position]);
// set image based on selected text
ImageView imageView = (ImageView) gridView
.findViewById(R.id.imageView);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(0, 0, 0, 0);
String allletters = setOne[position];
if (allletters.equals("A")) {
imageView.setImageResource(R.drawable.a);
} else if (allletters.equals("M")) {
imageView.setImageResource(R.drawable.m);
} else if (allletters.equals("C")) {
imageView.setImageResource(R.drawable.c);
} else if (allletters.equals("Qu")) {
imageView.setImageResource(R.drawable.qu);
} else if (allletters.equals("Z")) {
imageView.setImageResource(R.drawable.z);
} else if (allletters.equals("B")) {
imageView.setImageResource(R.drawable.b);
} else if (allletters.equals("D")) {
imageView.setImageResource(R.drawable.d);
} else if (allletters.equals("A")) {
imageView.setImageResource(R.drawable.a);
} else if (allletters.equals("E")) {
imageView.setImageResource(R.drawable.e);
} else if (allletters.equals("I")) {
imageView.setImageResource(R.drawable.i);
} else if (allletters.equals("E")) {
imageView.setImageResource(R.drawable.e);
} else if (allletters.equals("M")) {
imageView.setImageResource(R.drawable.m);
} else if (allletters.equals("M")) {
imageView.setImageResource(R.drawable.m);
} else if (allletters.equals("N")) {
imageView.setImageResource(R.drawable.n);
} else if (allletters.equals("T")) {
imageView.setImageResource(R.drawable.t);
} else if (allletters.equals("P")) {
imageView.setImageResource(R.drawable.p);
}
} else {
gridView = (View) convertView;
}
return gridView;
}
#Override
public int getCount() {
return setOne.length;
}
#Override
public Object getItem(int position) {
return null;
}
#Override
public long getItemId(int position) {
return 0;
}
}
If you want to store the letter you can simply create a string and store the letter push by user.
Example code:
String word; // word that you want to be display
String letter = MOBILE_OS[id]; // get the corresponding letter from array from the user selected
word.add(letter);
I have a Fragment in an Activity and I'm trying to display a GridView` from a custom adapter.I'm able to display the GridView but unable to select any item of the gridview.
Can any one help me in identifying the issue.
Please find the code I have used :
//MainActivity
package com.example.frag;
import java.util.ArrayList;
import android.app.Activity;
import android.app.ActionBar;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;
import android.os.Build;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
}
#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();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
GridView optionsGridView;
ArrayList<InfoItems> items = new ArrayList<InfoItems>();
OptionsAdapter adapter;
String items2[] = {"1","2","3","4"};
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
// implentation of display of options in GridView
optionsGridView = (GridView) rootView.findViewById(R.id.optionsGrid);
/*items.add(new InfoItems("Passport"));
items.add(new InfoItems("Aadhar"));
items.add(new InfoItems("Pan"));
items.add(new InfoItems("DL"));*/
adapter = new OptionsAdapter(this.getActivity(), items2);
optionsGridView.setAdapter(adapter);
return rootView;
}
}
}
Custom Adapter
package com.example.frag;
import java.util.ArrayList;
import dalvik.bytecode.OpcodeInfo;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
public class OptionsAdapter extends BaseAdapter{
private Context context;
private ArrayList<InfoItems> infoItems;
String items[];
public OptionsAdapter(Context context,ArrayList<InfoItems> items) {
// TODO Auto-generated constructor stub
this.context = context;
this.infoItems = items;
}
public OptionsAdapter(Context contsxt,String txt[]) {
// TODO Auto-generated constructor stub
this.context = contsxt;
this.items = txt;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return items.length;
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return infoItems.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
if(convertView == null){
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.info_options, null);
}
TextView option = (TextView) convertView.findViewById(R.id.optionsItem);
option.setText(items[position]);
return convertView;
}
}
fragment xml -- layout which will be displayed in the fragment
<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="com.example.frag.MainActivity$PlaceholderFragment" >
<GridView android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/optionsGrid"
android:horizontalSpacing="5dp"
android:verticalSpacing="5dp"
android:numColumns="2"
android:gravity="center"
android:layout_marginTop="10dp"/>
</RelativeLayout>
item xml -- list item of the custom adapter
<?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"
android:background="#000"
android:clickable="true">
<TextView android:layout_width="100dp"
android:layout_height="100dp"
android:layout_margin="10dp"
android:gravity="center"
android:textColor="#f000ff"
android:textSize="20sp"
android:textStyle="bold"
android:id="#+id/optionsItem"
android:text="passport"
android:clickable="true"
android:layout_gravity="center"/>
</LinearLayout>
Squonk is correct, you need to call optionsGridView.setAdapter(new OnItemClickListener....). You could also implement OnItemClickListener in your fragment and call optionsGridView.setAdapter(this).
Inside your PlaceholderFragment put this like below :
optionsGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//do your stuff!
}
});
Below is my code. When i try to run the app it gives me an error. it says there is a fatal exception in main. Can anybody help me out?
This is my main.java
package com.go.nextgendevelopment.uefaeuro2016;
import android.app.Activity;
import android.app.ListActivity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import java.util.List;
public class main extends ListActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Adapter
setListAdapter(new Myadapter(this,android.R.layout.simple_list_item_1, R.id.textView, getResources().getStringArray(R.array.teams)));
}
#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();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
private class Myadapter extends ArrayAdapter<String> {
public Myadapter(Context context, int resource, int textViewResourceID, String[] objects) {
super(context,resource,textViewResourceID,objects);
}
#Override
public View getView(int position, View convertView,ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View row = inflater.inflate(R.layout.list_item, parent, false);
String[] items = getResources().getStringArray(R.array.teams);
ImageView iv = (ImageView) row.findViewById(R.id.imageView);
TextView tv = (TextView) row.findViewById(R.id.textView);
tv.setText(items[position]);
if (items[position].equals("ajax")){
iv.setImageResource(R.drawable.logo_ajax);
}
else if (items[position].equals("feyenoord")){
iv.setImageResource(R.drawable.logo_feyenoord);
}
return super.getView(position,convertView, parent);
}
}
}
activity_main.xml
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#android:id/list"
android:layout_gravity="center_horizontal" />
</LinearLayout>
list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Medium Text"
android:id="#+id/textView"
android:layout_gravity="center_horizontal" />
</LinearLayout>
Thanks in advance!
Remove the setContentview in onCreate()
While setting the list adapter pass your listitem layout
I have followed this link to learn about implementing custom spinner. My requirement is just to get drop down menu on clicking a image, it should be like a drop down menu where in i load contents to the list from backend.
But i thought that sample example posted in the above link would work. but its not giving any list on clicking a image icon.
Here is the full source code.
package com.example.testdroid;
import java.util.ArrayList;
import java.util.List;
import android.content.Context;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.SherlockActivity;
import com.actionbarsherlock.internal.widget.IcsSpinner;
import com.actionbarsherlock.view.ActionMode;
import com.actionbarsherlock.view.ActionMode.Callback;
public class MainActivity extends SherlockActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
createCustomActionBar();
}
// #Override
// public boolean onCreateOptionsMenu(Menu menu) {
// // Inflate the menu; this adds items to the action bar if it is present.
// getMenuInflater().inflate(R.menu.activity_main, menu);
// return true;
// }
private void createCustomActionBar()
{
List<String> links = new ArrayList<String>();
links.add("Abc");
links.add("Def");
links.add("Ghi");
LinksAdapter linkAdapter = new LinksAdapter(this, R.layout.externallink, links);
View customNav = LayoutInflater.from(this).inflate(R.layout.custom_show_action_bar, null);
IcsSpinner spinner = (IcsSpinner)customNav.findViewById(R.id.spinner);
spinner.setAdapter(linkAdapter);
ImageView refresh = (ImageView) customNav.findViewById(R.id.refresh);
refresh.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
}
});
ImageView settings = (ImageView) customNav.findViewById(R.id.settings);
settings.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
}
});
getSupportActionBar().setCustomView(customNav, new ActionBar.LayoutParams(Gravity.RIGHT));
getSupportActionBar().setDisplayShowCustomEnabled(true);
}
private static class LinksAdapter extends ArrayAdapter<String> {
private List<String> strings;
private Context context;
private LinksAdapter(Context mainActivity, int textViewResourceId, List<String> objects) {
super(mainActivity, textViewResourceId, objects);
this.strings = objects;
this.context = mainActivity;
}
#Override
public int getCount() {
if (strings == null) return 0;
return strings.size();
}
#Override
public String getItem(int position) {
return super.getItem(position);
}
// return views of drop down items
#Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
final String siteLink = strings.get(position);
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// at 0 position show only icon
TextView site = (TextView) inflater.inflate(R.layout.externallink, null);
site.setText(siteLink);
site.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(siteLink.getUrl()));
//context.startActivity(i);
}
});
return site;
}
// return header view of drop down
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
return inflater.inflate(R.layout.custom_show_action_bar, null);
}
}
}
Here is my XML file, i.e layout xml file.
custom_show_action_bar.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="right"
>
<com.actionbarsherlock.internal.widget.IcsSpinner
android:id="#+id/spinner"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingRight="20dp"
android:layout_gravity="center"
/>
<ImageView android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:src="#drawable/ic_launcher"
android:paddingRight="20dp"
android:paddingLeft="10dp"
android:layout_gravity="center"
android:background="#ffffff"
android:id="#+id/refresh"/>
<ImageView android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:src="#drawable/ic_launcher"
android:paddingRight="20dp"
android:background="#ffffff"
android:layout_gravity="center"
android:id="#+id/settings"/>
</LinearLayout>
External_links.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" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
Guide me where i'm wrong.