Android : TextView showing Package name instead of the actual text - java

Text View in my fragment_main should be showing the String but instead it is showing the package name.
In my Main Activity Class,
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().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 {
ListView main_list;
ArrayAdapter<Cinema> adapter;
ArrayList<Cinema> ItemList = new ArrayList<Cinema>();
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Log.v("Check", "1");
final View rootView = inflater.inflate(R.layout.fragment_main, container, false);
ItemList.addAll(initialization());
ListView main_list = (ListView) rootView.findViewById(R.id.mainlist);
adapter = new ListAdapater(rootView.getContext(), ItemList);
main_list.setAdapter(adapter);
main_list.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
// TODO Auto-generated method stub
Intent i = new Intent(rootView.getContext(), CinemaDetails.class);
Bundle b = new Bundle();
b.putParcelable("Cinema", ItemList.get(position));
i.putExtra("Bundle", b);
startActivity(i);
}
});
return rootView;
}
public ArrayList<Cinema> initialization () {
ArrayList<Cinema> tempItemList = new ArrayList<Cinema>();
ArrayList<Movie> movielistone = new ArrayList<Movie>();
Movie movieone = new Movie(101, "MovieOne", "null", 5, "nULL");
Movie movietwo = new Movie(102, "Movietwo", "null", 3, "nuLL");
movielistone.add(movieone);
movielistone.add(movietwo);
Cinema cinemaone = new Cinema(01, "test", "ygn", 55555, movielistone, 3);
Cinema cinematwo = new Cinema(02, "test2", "ygn", 554555, movielistone, 3);
tempItemList.add(cinemaone);
tempItemList.add(cinematwo);
return tempItemList;
}
}
I use Array Adapter and the code for it is
public class ListAdapater extends ArrayAdapter<Cinema> {
private LayoutInflater inflater;
public ListAdapater(Context context, List<Cinema> itemList) {
super(context, R.layout.row, R.id.txt_cinemaname, itemList );
inflater = LayoutInflater.from(context) ;
// TODO Auto-generated constructor stub
}
#Override
public Cinema getItem(int position) {
// TODO Auto-generated method stub
return super.getItem(position);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
Cinema cinema = this.getItem(position);
TextView txt_name;
TextView txt_movie;
if (convertView == null) {
convertView = inflater.inflate(R.layout.row, null);
txt_name = (TextView) convertView.findViewById(R.id.txt_cinemaname);
txt_movie = (TextView) convertView.findViewById(R.id.txt_moviesonshow);
convertView.setTag(new ItemViewHolder(txt_name, txt_movie));
} else {
ItemViewHolder viewHolder = (ItemViewHolder) convertView.getTag();
txt_name = viewHolder.getName();
txt_movie = viewHolder.getMoviename();
}
txt_name.setText("test");
Log.v("name", txt_name.getText().toString());
txt_movie.setText("");
for (Movie movie : cinema.getMovieonshow()) {
String temp = txt_movie.getText().toString();
txt_movie.setText(temp + movie.getName() + "\n");
}
return super.getView(position, convertView, parent);
}
public static class ItemViewHolder {
private TextView name;
private TextView moviename;
public ItemViewHolder() {}
public ItemViewHolder(TextView name, TextView moviename) {
super();
this.name = name;
this.moviename = moviename;
}
public TextView getName() {
return name;
}
public void setName(TextView name) {
this.name = name;
}
public TextView getMoviename() {
return moviename;
}
public void setMoviename(TextView moviename) {
this.moviename = moviename;
}
}}
I have never had this problem before. I tied to log the Text from the TextView and it shows "test" just like I put it there. Any suggestion? Thanks for reading.

The problem is
return super.getView(position, convertView, parent);
ArrayAdapter, by default, fills the TextView view at the specified resource id (in this case R.id.txt_cinemaname) with the result of item.ToString() (where item is the object at the specified position in the backing array).
Since Cinema does not implement toString(), the default text is produced.
Using return convertView; should fix it.

I can't quite figure out exactly, but why are you passing in R.id.txt_cinemaname in your ListAdapter constructor? The problem looks like you are passing in an Object (that string "com.example... is a pointer to an object, I'm guessing a TextView) rather than a string.
Also, shouldn't you be returning convertView rather than super.getView(position, convertView, parent); in your getView() method?

Related

How to load a new template on a selected grid view item in android

I'm new at android. and I want to load a new template which contains two button on a selected item of grid view object.
Is that possible.
I added a gridview to my project and by using base adapter a template was loaded to each item of gridview. But what I want is that when I clicked an item of gridview, I want to load a new template (layout) to the selected item.
THE PROBLEM WAS SOLVED, followings are the edited codes
Base Adapter
public class KategoriAdapter extends BaseAdapter{
private Context mContext;
private String[] categoryValues;
private Bitmap[] pictures;
//indicate that positon for new template
private int mNewTemplatePos = -1;
public KategoriAdapter(Context context, String[] categoryValues, Bitmap[] pictures) {
this.mContext = context;
this.categoryValues = categoryValues;
this.pictures = pictures;
}
//apply new template to positon
public void useNewTemplate(int pos) {
mNewTemplatePos =pos;
//notiy list that data has changed and the list will refresh ui itself.
notifyDataSetChanged();
}
#Override
public int getCount() {
return categoryValues.length;
}
#Override
public Object getItem(int possition) {
return null;
}
#Override
public long getItemId(int possition) {
return 0;
}
#Override
public View getView(int possition, View convertView, ViewGroup parent) {
final LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
int posId = mNewTemplatePos;
if (convertView == null){
if (mNewTemplatePos ==possition){
convertView = getNewTemplate(inflater,possition);
}else {
convertView = getNormalTemplate(inflater,possition);
}
}else {
if (posId==possition){
convertView = getNewTemplate(inflater,possition);
}else{
convertView = getNormalTemplate(inflater,possition);
}
}
return convertView;
}
private View getNormalTemplate(LayoutInflater inflater, int possition) {
final View grid = inflater.inflate(R.layout.kategoriler_list_item, null);
TextView cName = (TextView) grid.findViewById(R.id.grid_item_ad);
ImageView categoryPictures = (ImageView) grid.findViewById(R.id.grid_item_resim);
cName.setText(categoryValues[possition]);
categoryPictures.setImageBitmap(pictures[possition]);
return grid;
}
private View getNewTemplate(LayoutInflater inflater, int possition) {
final View grid = inflater.inflate(R.layout.kategori_secenek_template, null);
TextView cName = (TextView) grid.findViewById(R.id.grid_item_ad);
cName.setText(categoryValues[possition]);
Button btn_nesne_tani = (Button) grid.findViewById(R.id.btn_nesneleri_taniyalim);
Button btn_cumle_kur = (Button) grid.findViewById(R.id.btn_cumle_kuralim);
btn_nesne_tani.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(mContext,"nesne",Toast.LENGTH_SHORT).show();
}
});
btn_cumle_kur.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(mContext,"cümle",Toast.LENGTH_SHORT).show();
}
});
return grid;
}
}
KategoriActivity.java
.....
final KategoriAdapter adapter = new KategoriAdapter(getApplicationContext(), mKategoriler, kategoriResimleri);
grid=(GridView)findViewById(R.id.gv_kategoriler);
grid.setAdapter(adapter);
grid.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
adapter.useNewTemplate(position);
Toast.makeText(getApplicationContext(), mKategoriler[position].toString(),Toast.LENGTH_SHORT).show();
}
});
}
I have rewrite your KategoriAdapter class:
public class KategoriAdapter extends BaseAdapter {
private Context mContext;
private final String[] categoryValues;
private final Bitmap[] pictures;
//indicate that positon in list are all use new template
private List<Integer> mNewTemplatePos;
public ImageView categoryPictures;
//indicate that this is normal template view
private final String NORMAL_TEMPLATE = "NORMAL_TEMPLATE";
//indicate that this is new template view
private final String NEW_TEMPLATE = "NEW_TEMPLATE";
public KategoriAdapter(Context context, String[] categoryValues, Bitmap[] pictures) {
this.mContext = context;
this.categoryValues = categoryValues;
this.pictures = pictures;
this.mNewTemplatePos = new ArrayList<>();
}
//apply new template to positon
public void useNewTemplate(int pos) {
mNewTemplatePos.add(pos);
//notiy list that data has changed and the list will refresh ui itself.
notifyDataSetChanged();
}
#Override
public int getCount() {
return categoryValues.length;
}
#Override
public Object getItem(int possition) {
return null;
}
#Override
public long getItemId(int possition) {
return 0;
}
#Override
public View getView(int possition, View convertView, ViewGroup parent) {
final LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
if (mNewTemplatePos.contains(possition)) {
convertView = getNewTemplate(inflater, possition);
//use tag to indicate the type of the template
convertView.setTag(NEW_TEMPLATE);
} else {
convertView = getNormalTemplate(inflater, possition);
convertView.setTag(NORMAL_TEMPLATE);
}
} else {
switch ((String) convertView.getTag()) {
case NORMAL_TEMPLATE:
//convertView is the normal template view but you need a new template view in possition
if (mNewTemplatePos.contains(possition))
convertView = getNewTemplate(inflater, possition);
break;
case NEW_TEMPLATE:
//convertView is the new template view but you need a normal template view in possition
if (!mNewTemplatePos.contains(possition))
convertView = getNormalTemplate(inflater, possition);
break;
}
}
return convertView;
}
private View getNormalTemplate(LayoutInflater inflater, int possition) {
View grid = inflater.inflate(R.layout.kategoriler_list_item, null);
TextView cName = (TextView) grid.findViewById(R.id.grid_item_ad);
categoryPictures = (ImageView) grid.findViewById(R.id.grid_item_resim);
cName.setText(categoryValues[possition]);
categoryPictures.setImageBitmap(pictures[possition]);
return grid;
}
private View getNewTemplate(LayoutInflater inflater, int possition) {
// TODO: 31/08/16 inflate you new template view layout here
return youNewTemplateView;
}
}
You should determine wether if current contentView is the right template type in getView() because contentView may be one of the new template in your list when it is not null.It is convenient to use tag to indicate the template type.
When to use useNewTemplate(position)?
Just apply the position that you need to use new template to useNewTemplate() and use it in your onItemClick() method.
grid.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
useNewTemplate(position);
}
});

Dynamically loading data to a new view

First im loading the data dynamically to a grid which is in the PMenu.java, then each item has view more button. once I pressed that I want to load the image, name and its amount in the item description view.
Im using a custom grid to load data to the grid in PMenu.java, and I have placed a button in the custom grid, so that it will navigate to viewmore.java.
I want to know once i press the button then how to load the data to viewmore.java file
PMenu.java fragment
GridView grid;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.menu_grid_main, container, false);
new PMenuAsyncTask(getActivity(), this).execute();
grid = (GridView) view.findViewById(R.id.grid);
return view;
}
#Override
public void onTaskCompleted(JSONArray responseJson) {
try {
List<String> descriptions = new ArrayList<String>();
List<String> imageUrls = new ArrayList<String>();
for (int i = 0; i < responseJson.length(); ++i) {
JSONObject object = responseJson.getJSONObject(i);
if ((object.getString("MainCategoryID")).equals("1")
&& (object.getString("SubCategoryID")).equals("1")) {
Log.i("ImageURL ", object.getString("ImageURL"));
imageUrls.add(object.getString("ImageURL"));
Log.i("Description ", object.getString("Description"));
descriptions.add(object.getString("Description"));
}
}
CustomGrid adapter = new CustomGrid(getActivity(), descriptions,
imageUrls);
grid.setAdapter(adapter);
} catch (JSONException e) {
e.printStackTrace();
}
}
CustomGrid class
public class CustomGrid extends BaseAdapter {
private Context context;
private final List<String> descriptions;
private final List<String> imageUrls;
public CustomGrid(Context c, List<String> descriptions, List<String> imageUrls) {
this.context = c;
this.descriptions = descriptions;
this.imageUrls = imageUrls;
}
#Override
public int getCount() {
return descriptions.size();
}
#Override
public Object getItem(int position) {
return descriptions.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
convertView = LayoutInflater.from(context).inflate(
R.layout.fragment_pizza, parent, false);
holder.ivImage = (ImageView) convertView
.findViewById(R.id.grid_image);
holder.tvHeader = (TextView) convertView
.findViewById(R.id.grid_text);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.tvHeader.setText(descriptions.get(position));
Picasso.with(this.context).load(imageUrls.get(position)).into(holder.ivImage);
Button backButton = (Button) convertView.findViewById(R.id.button1);
backButton.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view) {
Intent next = new Intent(context, viewmore.class);
context.startActivity(next);
next.putExtra("description", descriptions.get(position));
next.putExtra("imageUrl", imageUrls.get(position));
context.startActivity(next);
}
});
return convertView;
}
private class ViewHolder {
private TextView tvHeader;
private ImageView ivImage;
}
}
viewmore.java
public class viewmore extends Activity {
private Context context;
private final List<String> descriptions;
private final List<String> imageUrls;
public viewmore(Context c, List<String> descriptions, List<String> imageUrls) {
this.context = c;
this.descriptions = descriptions;
this.imageUrls = imageUrls;
}
private ActionBar actionBar;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.activity_viewmore);
String description = getIntent().getStringExtra("description");
String imageUrl = getIntent().getStringExtra("imageUrl");
actionBar = getActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setDisplayShowHomeEnabled(false);
actionBar.setDisplayShowTitleEnabled(true);
actionBar.setDisplayUseLogoEnabled(false);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.activity_main_actions, menu);
// Associate searchable configuration with the SearchView
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Take appropriate action for each action item click
switch (item.getItemId()) {
case R.id.action_cart:
return true;
case R.id.action_search:
// search action
return true;
default:
return super.onOptionsItemSelected(item);
}
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
convertView = LayoutInflater.from(context).inflate(
R.layout.activity_viewmore, parent, false);
holder.ivImage = (ImageView) convertView
.findViewById(R.id.grid_image);
holder.tvHeader = (TextView) convertView
.findViewById(R.id.grid_text);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.tvHeader.setText(descriptions.get(position));
Picasso.with(this.context).load(imageUrls.get(position)).into(holder.ivImage);
return convertView;
}
private class ViewHolder {
private TextView tvHeader;
private ImageView ivImage;
}
}
You can use intent.putExtra("key", value) methods before starting the viewmore Activity.
Then in the viewmore Activity, you can get these data from the getIntent().get*Extra("key") methods.
Like:
Intent next = new Intent(context, viewmore.class);
next.putExtra("description", descriptions.get(position));
next.putExtra("imageUrl", imageUrls.get(position));
context.startActivity(next);
Then (in viewmore Activity):
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.activity_viewmore);
String description = getIntent().getStringExtra("description");
String imageUrl = getIntent().getStringExtra("imageUrl");
// Get the TextView using its ID defined in the layout activity_viewmore.xml
TextView tv = (TextView) findViewById(R.id.my_text_view);
tv.setText(description);
ImageView imageView = (ImageView) findViewById(R.id.my_image_view);
Picasso.with(this).load(imageUrl).into(imageView);
}

Android JSON Data not parsing into ListView

Android not parsing JSON data into ListView, I am using this tutorial and just made few changes in ListViewAdapter.java
Like in my new implementation i used ViewHolder, and my code looks like this:
public class ListViewAdapter extends BaseAdapter {
// Declare Variables
Context context;
ArrayList<HashMap<String, String>> data;
ImageLoader imageLoader;
HashMap<String, String> resultp = new HashMap<String, String>();
ViewHolder holder;
public ListViewAdapter(Context context,
ArrayList<HashMap<String, String>> arraylist) {
this.context = context;
data = arraylist;
imageLoader = new ImageLoader(context);
}
#Override
public int getCount() {
return data.size();
}
#Override
public Object getItem(int position) {
return data.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
static class ViewHolder {
public ViewHolder(View convertView) {
// TODO Auto-generated constructor stub
}
TextView rank;
TextView country;
TextView population;
ImageView flag;
}
public View getView(final int position, View convertView, ViewGroup parent) {
// Declare Variables
// Avoid unneccessary calls to findViewById() on each row, which is expensive!
holder = null;
/*
* If convertView is not null, we can reuse it directly, no inflation required!
* We only inflate a new View when the convertView is null.
*/
if (convertView == null) {
convertView = ((Activity) context).getLayoutInflater().inflate(R.layout.listview_item, null);
// Create a ViewHolder and store references to the two children views
holder = new ViewHolder(convertView);
holder.rank = (TextView) convertView.findViewById(R.id.rank);
holder.country = (TextView) convertView.findViewById(R.id.country);
holder.population = (TextView) convertView.findViewById(R.id.population);
// Locate the ImageView in listview_item.xml
holder.flag = (ImageView) convertView.findViewById(R.id.flag);
// The tag can be any Object, this just happens to be the ViewHolder
convertView.setTag(holder);
} else {
// Get the ViewHolder back to get fast access to the TextView
// and the ImageView.
holder = (ViewHolder) convertView.getTag();
}
// Capture position and set results to the TextViews
holder.rank.setText(resultp.get(MainActivity.RANK));
holder.country.setText(resultp.get(MainActivity.COUNTRY));
holder.population.setText(resultp.get(MainActivity.POPULATION));
// Capture position and set results to the ImageView
// Passes flag images URL into ImageLoader.class
imageLoader.DisplayImage(resultp.get(MainActivity.FLAG), holder.flag);
// Capture ListView item click
return convertView;
}
}
Edited: Click on ListItem code
#Override
protected void onPostExecute(Void args) {
// Locate the listview in listview_main.xml
listview = (ListView) findViewById(R.id.listview);
// Pass the results into ListViewAdapter.java
adapter = new ListViewAdapter(MainActivity.this, arraylist);
// Set the adapter to the ListView
listview.setAdapter(adapter);
// Close the progressdialog
mProgressDialog.dismiss();
listview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// code to handle click
}
});
}
But i don't no why i am not getting data into ListView !
The issue is that you are not assigning the HashMap to `resultp that has the information you want to display
public View getView(final int position, View convertView, ViewGroup parent) {
holder = null;
if (convertView == null) {
convertView = ((Activity) context).getLayoutInflater().inflate(R.layout.listview_item, null);
holder = new ViewHolder(convertView);
holder.rank = (TextView) convertView.findViewById(R.id.rank);
holder.country = (TextView) convertView.findViewById(R.id.country);
holder.population = (TextView) convertView.findViewById(R.id.population);
holder.flag = (ImageView) convertView.findViewById(R.id.flag);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
// Here's the change
resultp = data.get(position);
// Here's the change
holder.rank.setText(resultp.get(MainActivity.RANK));
holder.country.setText(resultp.get(MainActivity.COUNTRY));
holder.population.setText(resultp.get(MainActivity.POPULATION));
imageLoader.DisplayImage(resultp.get(MainActivity.FLAG), holder.flag);
return convertView;
}
To attach OnItemClickListener to your ListView, in the Activity that contains the ListView, add the following:
public class MyActivity implements OnItemClickListener{
ListView lv;
#Override
public void onCreate(Bundle savedInstanceState() {
....
....
// lv initialized here
// adapter of lv set here
attachListeners();
}
private void attachListeners() {
....
....
// attach listeners to other views if you like
lv.setOnItemClickListener(this);
}
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// code to handle click
}
}
Or, if you don't want your Activity to implement OnItemClickListener, then,
public class MyActivity {
ListView lv;
#Override
public void onCreate(Bundle savedInstanceState() {
....
....
// lv initialized here
// adapter of lv set here
attachListeners();
}
private void attachListeners() {
....
....
// attach listeners to other views if you like
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// code to handle click
}
});
}
}
First of all try to fix this:
#Override
public Object getItem(int position) {
return data.get(position);
}
#Override
public long getItemId(int position) {
return position;
}

i am trying to fetch the id of the image to display it in full view

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

Listview click is not fire.

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;
}

Categories

Resources