I created custom adapter for listview which contain text and images, on click of particular list item I have to open another activity, but I am not able to fire listview click event, below is my code.
Thanks.
Adapter
public class ImageAndTextAdapter extends ArrayAdapter<String> {
private LayoutInflater mInflater;
private Context myCtx;
private String[] mStrings;
private TypedArray mIcons;
private int mViewResourceId;
public ImageAndTextAdapter(Context ctx, int viewResourceId,
String[] strings, TypedArray icons) {
super(ctx, viewResourceId, strings);
myCtx = ctx;
mInflater = (LayoutInflater)ctx.getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
mStrings = strings;
mIcons = icons;
mViewResourceId = viewResourceId;
}
#Override
public int getCount() {
return mStrings.length;
}
#Override
public String getItem(int position) {
return mStrings[position];
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
convertView = mInflater.inflate(mViewResourceId, null);
ImageView iv = (ImageView)convertView.findViewById(R.id.option_icon);
iv.setImageDrawable(mIcons.getDrawable(position));
TextView tv = (TextView)convertView.findViewById(R.id.option_text);
tv.setText(mStrings[position]);
return convertView;
}
}
Main acitivity
public class OurWishingWellActivity extends ListActivity implements OnClickListener{
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.list_view_image);
Context ctx = getApplicationContext();
Resources res = ctx.getResources();
String[] options = res.getStringArray(R.array.reg_item_names);
TypedArray icons = res.obtainTypedArray(R.array.reg_icons);
setListAdapter(new ImageAndTextAdapter(ctx, R.layout.list_item,
options, icons));
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
}
Respected Aafag#
You have write onclick of listview like below.
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
super.onListItemClick(l, v, position, id);
Toast.makeText(AdvancedListViewActivity.this, "working", Toast.LENGTH_SHORT).show();
System.out.println("working!!!!!!!!!");
}
Add listener
#Override
public View getView(int position, View convertView, ViewGroup parent) {
convertView = mInflater.inflate(mViewResourceId, null);
ImageView iv = (ImageView)convertView.findViewById(R.id.option_icon);
iv.setImageDrawable(mIcons.getDrawable(position));
TextView tv = (TextView)convertView.findViewById(R.id.option_text);
tv.setText(mStrings[position]);
convertView .setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity();
}
});
return convertView;
}
Related
I have grid view in my application. on click of each image in gridview new activity should start.
now when I click on them, a toast appears.
how I manage setOnClicklistener in my adaptor I can't have startactivity.
enter code here:
public class CustomAdapter extends BaseAdapter {
String [] result;
Context context;
int [] imageId;
private static LayoutInflater inflater=null;
public CustomAdapter(MyActivity mainActivity, String[] prgmNameList, int[] prgmImages) {
// TODO Auto-generated constructor stub
result=prgmNameList;
context=mainActivity;
imageId=prgmImages;
inflater = (LayoutInflater)context.
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return result.length;
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
public class Holder
{
TextView tv;
ImageView img;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
final Holder holder=new Holder();
View rowView;
rowView = inflater.inflate(R.layout.program_list, null);
holder.tv=(TextView) rowView.findViewById(R.id.textView1);
holder.img=(ImageView) rowView.findViewById(R.id.imageView1);
holder.tv.setText(result[position]);
holder.img.setImageResource(imageId[position]);
rowView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(context, "You Clicked "+result[position], Toast.LENGTH_LONG).show();
}
});
return rowView;
}
and here is myactivity:
public class MyActivity extends Activity {
GridView gv;
Context context;
ArrayList prgmName;
public static String [] prgmNameList={"Let Us C","c++","JAVA","Jsp","Microsoft .Net","Android","PHP","Jquery","JavaScript"};
public static int [] prgmImages={R.drawable.images,R.drawable.images1,R.drawable.images2,R.drawable.images3,R.drawable.images4,R.drawable.images5,R.drawable.images6,R.drawable.images7,R.drawable.images8};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
gv=(GridView) findViewById(R.id.gridView1);
gv.setAdapter(new CustomAdapter(this, prgmNameList,prgmImages));
}
#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;
}
}
Add onItemClickListener on GridView
gv.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
if(position==0){
Intent iinent= new Intent(MyActivity.this,secondactivity.class);
startActivity(iinent);
}
}
});
Below is the class where i am not able to get the resource of the image id. I am trying to fetch all the data from the web service. Here i have posted all the classes from where the images are fetched.I have all the images showing it in the gridView. I am not facing problem in that, however when I click on the image of the gridview it shows me the just the xml file which I have called. I guess I am doing wrong somewhere calling the id here in the FullImageAcitivity file.
DefaultGridView.java
public class DefaultGridView extends Activity {
//int position;
public void onBackPressed() {
// TODO Auto-generated method stub
super.onBackPressed();
finish();
}
GridView gridView;
Context context=this;
DisplayImageOptions options;
protected ImageLoader imageLoader = ImageLoader.getInstance();
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.latestphotos);
gridView=(GridView)findViewById(R.id.grid_view);
options = new DisplayImageOptions.Builder()
.showStubImage(R.drawable.stub_image)
.showImageForEmptyUri(R.drawable.image_for_empty_url)
.cacheInMemory()
.cacheOnDisc()
.bitmapConfig(Bitmap.Config.RGB_565)
.build();
gridView.setAdapter(new ImageAdapter());
}
public class ImageAdapter extends BaseAdapter {
LayoutInflater inflater = LayoutInflater.from(context);
public ImageAdapter() {
// TODO Auto-generated constructor stub
imageLoader.init(ImageLoaderConfiguration.createDefault(DefaultGridView.this));
}
#Override
public int getCount() {
return Global.getPhotos_list().size();
}
#Override
public Object getItem(int position) {
return position;
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View view, ViewGroup viewGroup) {
View v = view;
ImageView picture;
TextView name;
if(v == null) {
v = inflater.inflate(R.layout.other, viewGroup, false);
v.setTag(R.id.picture, v.findViewById(R.id.picture));
v.setTag(R.id.text, v.findViewById(R.id.text));
}
picture = (ImageView)v.getTag(R.id.picture);
name = (TextView)v.getTag(R.id.text);
Item item = (Item)items.get(i);
picture.setImageResource(item.getDrawable());
name.setText(item.name);
Log.d("position", position+"");
imageLoader.displayImage(Constant.img_URL+Global.getPhotos_list().get(position).get("photo_name"), picture , options,null,context);
gridView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View v,
int position, long a) {
Intent i = new Intent(getApplicationContext(), FullImageActivity.class);
i.putExtra("idkey", position); // pass the id
startActivity(i);
}
});
return v;
}
}
}
FullImageActivity.java
public class FullImageActivity extends Activity {
Button download, setas;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.full_image);
setas = (Button) findViewById(R.id.setas);
download = (Button)findViewById(R.id.download);
// get intent data
final Intent i = getIntent();
// Selected image id
ImageView imageView = (ImageView) findViewById(R.id.full_image_view);
int id = getIntent().getIntExtra("idkey",-1); //get id
imageView.setImageResource(id);
setas.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
WallpaperManager myWallpaperManager
= WallpaperManager.getInstance(getApplicationContext());
try {
myWallpaperManager.setResource(R.id.full_image_view);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
}
ImageAdapter.java
public class ImageAdapter extends BaseAdapter {
private LayoutInflater inflater;
List<Item> items;
public ImageAdapter(Context context,List<Item> items) {
inflater = LayoutInflater.from(context);
this.items = items;
}
#Override
public int getCount() {
return items.size();
}
#Override
public Object getItem(int i) {
return items.get(i);
}
#Override
public long getItemId(int i) {
return items.get(i).drawable;
}
#Override
public View getView(int i, View view, ViewGroup viewGroup) {
View v = view;
ImageView picture;
TextView name;
if(v == null) {
v = inflater.inflate(R.layout.other, viewGroup, false);
v.setTag(R.id.picture, v.findViewById(R.id.picture));
v.setTag(R.id.text, v.findViewById(R.id.text));
}
picture = (ImageView)v.getTag(R.id.picture);
name = (TextView)v.getTag(R.id.text);
Item item = (Item)items.get(i);
picture.setImageResource(item.getDrawable());
name.setText(item.name);
return v;
}
}
Items.java
public class Item {
String name;
int drawable;
public int getDrawable() {
return drawable;
}
public void setDrawable(int drawable) {
this.drawable = drawable;
}
public Item(String name, int id)
{
this.name= name;
this.drawable = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
In gridView item click
Item item =(Item) items.get(position);
int id = item.getDrawable();
Intent i = new Intent(ActivityName.this, FullImageActivity.class);
i.putExtra("idkey", id); // pass the id
startActivity(i);
Then in FullImageActivity
int id = getIntent().getIntExtra("idkey"); //get id
imageview.setImageResource(id); // set the drawable to imageview
You can move the below to onCreate of DefaultFridView
gridView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View v,
int position, long a) {
Item item =(Item) items.get(position);
int id = item.getDrawable();
Intent i = new Intent(ActivityName.this, FullImageActivity.class);
i.putExtra("idkey", id); // pass the id
startActivity(i);
}
});
Also you need to have the List in DefaultGridView
List<Item> items = new ArrayList<Item>();
Then
items.add(new Item("One", R.drawable.abstact_one));
items.add(new Item("Two", R.drawable.abstract_three));
items.add(new Item("Three", R.drawable.image_two));
items.add(new Item("Four", R.drawable.image_four));
items.add(new Item("Five", R.drawable.image_five));
items.add(new Item("Six", R.drawable.image_nine));
items.add(new Item("Seven", R.drawable.image_ten));
Then
gridView.setAdapter(new ImageAdapter(),items);
Also i don't understand having the ImageAdapter as separate and as a inner class. WHy do you need both
Try this example my be it is usefull
Grieview Display Full Image
I want to display my JSON into gridview, before it.. i displayed my JSON into ListView, and it works. but in BaseAdapter, i don't know how to send my JSON that I have put into ArrayList into Base Adapter
so this is my source code :
Activity :
public class MainActivity extends ListActivity {
List AgenList = new ArrayList();
boolean boolStatusKoneksi=true;
private ProgressDialog Dialog;
protected Context applicationContext;
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new AgenAsyncTask().execute();
GridView gridview = (GridView) findViewById(R.id.gridview);
gridview.setAdapter(new AgenAdapter(this));
}
public class AgenAsyncTask extends AsyncTask<String, String, String>
{
#Override
protected void onPreExecute() {
super.onPreExecute();
Dialog = new ProgressDialog(MainActivity.this);
Dialog.setMessage("Mohon Tunggu sebentar...");
Dialog.setIndeterminate(false);
Dialog.setCancelable(true);
Dialog.show();
}
protected String doInBackground(String... args) {
String url = ("http:10.10.2/selectAgent.htm");
try{
JSONParser j=new JSONParser();
JSONArray jsonArray = j.takeJson(url);;
for(int i =0; i<jsonArray.length(); i++){
JSONObject c = jsonArray.getJSONObject(i);
HashMap<String, String> map = new HashMap<String, String>();
if (c.has("atasan"))
map.put("atasan", c.get("atasan").toString());
if (c.has("nama_agen"))
map.put("nama_agen", c.get("nama_agen").toString());
if (c.has("kode_agen"))
map.put("kode_agen", c.get("kode_agen").toString());
if (c.has("no_aaji"))
map.put("no_aaji", c.get("no_aaji").toString());
if (c.has("jenis"))
map.put("jenis", c.get("jenis").toString());
AgenList.add(map);
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
#Override
protected void onPostExecute(String file_url) {
}
}
This is my BaseAdapter :
public class AgenAdapter extends BaseAdapter {
public AgenAdapter(MainActivity mainActivity) {
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return 0;
}
#Override
public Object getItem(int position) {
// 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) {
LayoutInflater layout = getLayoutInflater();
View view= layout.inflate(R.layout.list_item,parent,false);
TextView ATASAN = (TextView) findViewById(R.id.atasan);
TextView NAMA_AGEN= (TextView) findViewById(R.id.nama_agen);
TextView KODE_AGEN= (TextView) findViewById(R.id.kode_agen);
TextView NO_AAJI= (TextView) findViewById(R.id.no_aaji);
TextView JENIS= (TextView) findViewById(R.id.jenis);
return view;
}
}
}
As you see, i have ArrayList named as = AgenList but i haven't put it into my BaseAdapter.
make a parametrized constructor and pass your array list into its paramerts. so your class will be like
public class AgenAdapter extends BaseAdapter {
List<yourObj> list;
Activity a;
public AgenAdapter(Activity activity,List<yourObj> list) {
this.a=activity;
this.list=list;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return list.size();///////return size of list
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;///// dont return null here
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;/////////return position as itemID
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater layout = getLayoutInflater();
View view= layout.inflate(R.layout.list_item,parent,false);
TextView ATASAN = (TextView) findViewById(R.id.atasan);
TextView NAMA_AGEN= (TextView) findViewById(R.id.nama_agen);
TextView KODE_AGEN= (TextView) findViewById(R.id.kode_agen);
TextView NO_AAJI= (TextView) findViewById(R.id.no_aaji);
TextView JENIS= (TextView) findViewById(R.id.jenis);
return view;
}
}
try this.
gridview.setAdapter(new AgenAdapter(this), AgenList);
and use this line in your onPostExecute() instead of onCreate()
Like this:
#Override
protected void onPostExecute(String file_url) {
gridview.setAdapter(new AgenAdapter(this), AgenList);
}
Have a look at ArrayAdapter. It contains all of the implementation for an Adapter based on an ArrayList.
Make your adapter a subclass of ArrayAdapter, giving a parametrised type. Yours in this case being Map<String, String>
public class ResultAdapter extends ArrayAdapter<Map<String,String>> {
private int mResource;
/**
* #param context
* #param resource
*/
public ResultAdapter(Context context, int resource, ArrayList<Map<String,String>> dataList) {
super(context, resource, dataList);
this.mResource = resource;
}
...
}
Override the getView method of the ArrayAdapter to use to set the data given to the views.
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view;
// If the view is already inflated, reuse it.
// Else inflate the view
if (convertView != null) {
view = convertView;
} else {
//inflate view
final LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(mResource, parent, false);
}
TextView ATASAN = (TextView) findViewById(R.id.atasan);
...
//Get data here
Map<String, String> item = getItem(position);
//Set data to the View's e.g.
ATASAN.setText(item.get("ATASAN"));
...
return view;
}
I'd like to create a spinner with a list of names, and a list of ids from many .jpg's
I have a class with a String[] wich contains the names, and a Integer[] with the ids of the drawables.
My main class is the next
public class Settings extends Activity{
private Button btnGuardar, btnCerrar;
private Spinner spOperadoras, spTarifas;
private Operadoras op = new Operadoras();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.settings);
btnGuardar = (Button)findViewById(R.id.buttonSave);
btnCerrar = (Button)findViewById(R.id.buttonClose);
spOperadoras = (Spinner)findViewById(R.id.spinnerOperador);
spTarifas = (Spinner)findViewById(R.id.spinnerTarifas);
OperadorAdapter adapter = new OperadorAdapter(this, R.layout.custom_spinner, op);
spOperadoras.setAdapter(adapter);
}
}
Then my adapter is:
public class OperadorAdapter extends BaseAdapter implements SpinnerAdapter{
Activity context;
int spLayoutId;
Operadoras operadoras;
public OperadorAdapter(Activity context, int id, Operadoras op){
//super(context, id, op);
this.context = context;
this.operadoras = op;
this.spLayoutId = id;
}
#Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
return getCustomView(position, convertView, parent);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
return getCustomView(position, convertView, parent);
}
public View getCustomView(int position, View convertView, ViewGroup parent) {
View item = convertView;
TextView opName;
ImageView opIcon;
LayoutInflater inflater = context.getLayoutInflater();
item = inflater.inflate(spLayoutId, null);
opName = (TextView) item.findViewById(R.id.nombreOperador);
opIcon = (ImageView) item.findViewById(R.id.imageOperador);
opName.setText(operadoras.getOperador(position).toString());
opIcon.setImageResource(operadoras.getLogo(position));
return item;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return 0;
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
But I have no results in my spinner.
What's the problem???
Thanks.
#Override
public int getCount() {
// TODO Auto-generated method stub
return 0;
}
Return your actual count.
I want to add a new row on clicking add button in a list view in android.
Can someone help me out?
Here is the code I have so far:
public class prsnlhstry<EventArgs> extends Activity implements OnItemClickListener {
public static ArrayList<String> arr_sort_textview1= null;
public static ArrayList<String> arr_sort_textview2=null;
public static ArrayList<String> arr_sort_textview3=null;
public Resources ApptResources= null;
private ContextWrapper mycontext;
tododata todo;
int clickcounter = 0;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ListView lv= (ListView)findViewById(R.id.listview);
todo = new tododata(this);
List<listObject> List = new ArrayList<listObject>();
for(int i = 0; i <= 5; i++){
listObject lo = new listObject();
lo.grade = Integer.toString(i);
lo.reason = Integer.toString(i);
lo.school = Integer.toString(i);
List.add(lo);
}
CustomAdapter ca = new CustomAdapter(this);
lv = (ListView)findViewById(R.id.listview);
lv.setAdapter(ca);
lv.setOnItemClickListener(this);
ca.notifyDataSetChanged();
Button button = (Button) findViewById(R.id.btn);
button.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
Toast.makeText(prsnlhstry.this, "Row Added Successfully", Toast.LENGTH_SHORT).show();
}
});
}
private class listObject {
String grade;
String school;
String reason;
}
public class CustomAdapter extends BaseAdapter {
private LayoutInflater mInflater;
public CustomAdapter(Context context) {
mInflater = LayoutInflater.from(context);
}
#Override
public int getCount() {
return todo.getLength();
}
#Override
public Object getItem(int position) {
return position;
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
String temp = null;
// to reinflate it. We only inflate a new View when the convertView supplied
// by ListView is null.
if (convertView == null) {
convertView = mInflater.inflate(R.layout.grid_item, null);
// Creates a ViewHolder and store references to the views
// we want to bind data to.
holder = new ViewHolder();
holder.textview1 = (TextView) convertView.findViewById(R.id.item2);
holder.textview2 = (TextView) convertView.findViewById(R.id.item3);
holder.textview3 = (TextView) convertView.findViewById(R.id.item4);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.textview1.setText(todo.getgrade(position));
holder.textview2.setText(todo.getschool(position));
holder.textview3.setText(todo.getreason(position));
return convertView;
}
}
static class ViewHolder {
TextView textview1;
TextView textview2;
TextView textview3;
}
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
}
}
Just add the item to your list and refresh the adapter, nothing special.
You need to increment your "tododata" data structure on button click and then call to notifyDataSetChanged() method:
Button button = (Button) findViewById(R.id.btn);
button.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
todo.addNewRow();
ca.notifyDataSetChanged();
Toast.makeText(prsnlhstry.this, "Row Added Successfully", Toast.LENGTH_SHORT).show();
}
});
To compile the above code you will need to define "ca" variable as the final one, or make it class member.
I hope it helps.