I am using Base Adapter for displaying image in grid view. The working fine when images are fixed IMAGE_URLS directly but am try with geting url from list and assign into IMAGE_URLS that shows non static variable cannot be referenced from a static context. I don't know how to solve this issue. please help to solve this issue
public class ImageGridFragment extends AbsListViewBaseFragment {
public static final int INDEX = 1;
String description="";
ArrayList<String> img = new ArrayList<String>();
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fr_image_grid, container, false);
listView = (GridView) rootView.findViewById(R.id.grid);
img.clear();
Bundle bundle = this.getArguments();
description = bundle.getString("description");
String[] separated= description.split(",");
for(int i=0;i<separated.length;i++)
{
img.add(separated[i]);
}
ImageGalleryFragment.imageLoader.init(ImageLoaderConfiguration.createDefault(getActivity()));
((GridView) listView).setAdapter(new ImageAdapter(getActivity()));
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
startImagePagerActivity(position);
}
});
return rootView;
}
private static class ImageAdapter extends BaseAdapter {
String[] IMAGE_URLS = img.toArray(new String[img.size()]);
private LayoutInflater inflater;
private DisplayImageOptions options;
ImageAdapter(Context context) {
inflater = LayoutInflater.from(context);
options = new DisplayImageOptions.Builder()
.showImageOnLoading(R.drawable.ic_stub)
.showImageForEmptyUri(R.drawable.ic_empty)
.showImageOnFail(R.drawable.ic_error)
.cacheInMemory(true)
.cacheOnDisk(true)
.considerExifParams(true)
.bitmapConfig(Bitmap.Config.RGB_565)
.build();
}
#Override
public int getCount() {
return IMAGE_URLS.length;
}
#Override
public Object getItem(int position) {
return null;
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
View view = convertView;
if (view == null) {
view = inflater.inflate(R.layout.item_grid_image, parent, false);
holder = new ViewHolder();
assert view != null;
holder.imageView = (ImageView) view.findViewById(R.id.image);
holder.progressBar = (ProgressBar) view.findViewById(R.id.progress);
view.setTag(holder);
} else {
holder = (ViewHolder) view.getTag();
}
ImageLoader.getInstance()
.displayImage(IMAGE_URLS[position], holder.imageView, options, new SimpleImageLoadingListener() {
#Override
public void onLoadingStarted(String imageUri, View view) {
holder.progressBar.setProgress(0);
holder.progressBar.setVisibility(View.VISIBLE);
}
#Override
public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
holder.progressBar.setVisibility(View.GONE);
}
#Override
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
holder.progressBar.setVisibility(View.GONE);
}
}, new ImageLoadingProgressListener() {
#Override
public void onProgressUpdate(String imageUri, View view, int current, int total) {
holder.progressBar.setProgress(Math.round(100.0f * current / total));
}
});
return view;
}
}
static class ViewHolder {
ImageView imageView;
ProgressBar progressBar;
}
}
Change the class from private static to "private class ImageAdapter".
Related
I'm new to Android and I'm still learning. Could You please help me?
I'm trying to set clickable List in ListFragment with custom ArrayAdapter. It works with normal ArrayAdapter but not with custom. Here is my CustomArrayAdapter class:
public class CustomListAdapter<T> extends ArrayAdapter<T> {
private List<Integer> imageIds;
private List<String> titleList;
private List<String> descriptionList;
private List<String> createdList;
private final Context activityContext;
private static final String DATE_FORMAT_8 = "yyyy-MM-dd HH:mm:ss";
SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT_8);
public CustomListAdapter(#NonNull Context context, #NonNull List<T> objects) {
super(context, R.layout.row_layout, objects);
this.activityContext = context;
imageIds = new ArrayList<>();
titleList = new ArrayList<>();
descriptionList = new ArrayList<>();
createdList = new ArrayList<>();
}
#NonNull
#Override
public View getView(int position, #Nullable View convertView, #NonNull ViewGroup parent) {
View rowView = convertView;
if (convertView == null)
rowView = LayoutInflater.from(getContext()).inflate(R.layout.row_layout, parent, false);
ImageView ivNote = rowView.findViewById(R.id.ivNote);
TextView tvTitle = rowView.findViewById(R.id.tvTitle);
TextView tvDescription = rowView.findViewById(R.id.tvDescription);
TextView tvCreated = rowView.findViewById(R.id.tvCreated);
ivNote.setImageResource(imageIds.get(position));
tvTitle.setText(titleList.get(position));
tvDescription.setText(descriptionList.get(position));
tvCreated.setText(createdList.get(position));
return rowView;
}
list setters etc
And here is my ListFragment class:
private List<TaskDTO> taskDTOS;
TasksItemSelected tasksItemSelected;
public interface TasksItemSelected {
void onTaskItemSelected(int index);
}
public TasksFragment() {
// Required empty public constructor
}
#Override
public void onAttach(#NonNull Context context) {
super.onAttach(context);
tasksItemSelected = (TasksItemSelected) context;
}
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
if (getArguments() != null) {
taskDTOS = (List<TaskDTO>) getArguments().getSerializable("tasksList");
}
View view = inflater.inflate(R.layout.fragment_tasks, container, false);
return view;
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
if (taskDTOS != null && taskDTOS.size() > 0) {
setListAdapter(getCustomListAdapter(getContext(),taskDTOS));
}
}
private CustomListAdapter getCustomListAdapter(Context activity, List<TaskDTO> taskDTOS) {
CustomListAdapter customListAdapter = new CustomListAdapter(activity, taskDTOS);
customListAdapter.setImageIds(taskDTOS);
customListAdapter.setTitleList(taskDTOS);
customListAdapter.setDescriptionList(taskDTOS);
customListAdapter.setCreatedList(taskDTOS);
return customListAdapter;
}
#Override
public void onListItemClick(#NonNull ListView l, #NonNull View v, int position, long id) {
super.onListItemClick(l, v, position, id);
tasksItemSelected.onTaskItemSelected(position);
}
}
When I set new ArrayAdapter, list items become clickable and new activity runs, but nothing happens when tapping list items in the above case.
I want to search with a text on this list
class listAdpter extends BaseAdapter {
ArrayList<Listittem2> listA = new ArrayList<Listittem2>();
public listAdpter(ArrayList<Listittem2> listA) {
this.listA = listA;
}
#Override
public int getCount() {
return listA.size();
}
#Override
public Object getItem(int position) {
return listA.get(position).name;
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater layoutInflater = getLayoutInflater();
View view = layoutInflater.inflate(R.layout.tahaaaarow, null);
TextView center = (TextView) view.findViewById(R.id.textView10);
TextView location = (TextView) view.findViewById(R.id.textView15);
ImageView img = (ImageView) view.findViewById(R.id.imageView22);
center.setText(listA.get(position).name);
location.setText(listA.get(position).city+" - "+listA.get(position).street);
Picasso.with(CompanytwoDetalies.this).load("http://zaidproject.16mb.com/feedsup/" + listA.get(position).img).transform(new CircleTransform()).into(img);
return view;
}
}
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);
}
});
I want to list the JSON data received with this adapter, however am getting an error that says that he is not managing to find the view, The error I get in the log is "java.lang.IllegalArgumentException: view must not be null"
public class categoriaAdapter extends ArrayAdapter {
private List<categoriaModel> categoriaModelList;
private int resource;
private LayoutInflater inflater;
public categoriaAdapter(Context context, int resource, List<categoriaModel> objects) {
super(context, resource, objects);
categoriaModelList = objects;
this.resource = resource;
inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if (convertView == null){
holder = new ViewHolder();
convertView = inflater.inflate(resource, null);
holder.imgCategoria = (ImageView)convertView.findViewById(R.id.ivIcon);
holder.catTitulo = (TextView)convertView.findViewById(R.id.tvMovie);
holder.catDesc = (TextView)convertView.findViewById(R.id.tvTagline);
holder.catID = (TextView)convertView.findViewById(R.id.tvYear);
convertView.setTag(holder);
} else{
holder = (ViewHolder) convertView.getTag();
}
final ProgressBar progressBar = (ProgressBar)convertView.findViewById(R.id.progressBar);
ImageLoader.getInstance().displayImage(categoriaModelList.get(position).getImage(), holder.imgCategoria, new ImageLoadingListener() {
#Override
public void onLoadingStarted(String imageUri, View view) {
progressBar.setVisibility(View.VISIBLE);
}
#Override
public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
progressBar.setVisibility(View.GONE);
}
#Override
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
progressBar.setVisibility(View.GONE);
}
#Override
public void onLoadingCancelled(String imageUri, View view) {
progressBar.setVisibility(View.GONE);
}
});
holder.catTitulo.setText(categoriaModelList.get(position).getNome());
holder.catDesc.setText(categoriaModelList.get(position).getDescricacao());
holder.catID.setText(categoriaModelList.get(position).getIdeCategoria());
return convertView;
}
class ViewHolder{
ImageView imgCategoria;
TextView catTitulo;
TextView catDesc;
TextView catID;
}
}
And my
Error logs
Please check your layout(xml) and see if R.id.ivIcon is an imageView or if it is present in the resource layout. Make sure imgCategoria is not null and is properly initialised before calling displayImage.
Also initialize ImageLoader configuration. i.e.
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(context).build();
ImageLoader.getInstance().init(config);
ImageLoader.getInstance().displayImage(categoriaModelList.get(position).getImage(), holder.imgCategoria, new ImageLoadingListener() {
#Override
public void onLoadingStarted(String imageUri, View view) {
progressBar.setVisibility(View.VISIBLE);
}
#Override
public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
progressBar.setVisibility(View.GONE);
}
#Override
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
progressBar.setVisibility(View.GONE);
}
#Override
public void onLoadingCancelled(String imageUri, View view) {
progressBar.setVisibility(View.GONE);
}
});
Hope it helps you..
Check whether you included the right item layout. I had same issue turns out I was using item layout of different activity. And I had duplicated the adapter code.
I am writing an Android application that is utilizing the ExpandableListView feature, and for some reason, I can't seem to get the view to populate. I was originally using a regular ListView, one which populated with the correct information. I then refactored the code to utilize the ExpandableListView. Here are all relevant snippets from my project:
SeasonsFragment.java (Which is called properly in MainActivity)
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRetainInstance(true);
ArrayList<Season> seasons = ((App)getActivity().getApplication()).getSeasonsList();
ArrayList<RowItem> rowItems = new ArrayList<>();
for (int i = 0; i < seasons.size(); i++) {
RowItem items = new RowItem(seasons.get(i));
rowItems.add(items);
}
CustomAdapter adapter = new CustomAdapter(getActivity(), rowItems);
View rootView = inflater.inflate(R.layout.seasons_fragment, container, false);
ExpandableListView list_view = (ExpandableListView) rootView.findViewById(R.id.list);
list_view.setAdapter(adapter);
return inflater.inflate(R.layout.seasons_fragment, container, false);
}
CustomAdapter.java
public class CustomAdapter extends BaseExpandableListAdapter {
Context context;
List<RowItem> rowItem;
private HashMap<String, List<Sport>> listDataChildren = new HashMap<>();
public CustomAdapter(Context context, List<RowItem> rowItem) {
Log.i("Custom Adapter", "Created");
this.context = context;
this.rowItem = rowItem;
for (RowItem item : rowItem){
Season season = item.getSeason();
listDataChildren.put(item.getText(), season.getSports());
}
}
#Override
public int getGroupCount() {
return rowItem.size();
}
#Override
public int getChildrenCount(int groupPosition) {
return listDataChildren.get(rowItem.get(groupPosition)).size();
}
#Override
public Object getGroup(int groupPosition) {
return rowItem.get(groupPosition);
}
#Override
public Object getChild(int groupPosition, int childPosition) {
return listDataChildren.get(rowItem.get(groupPosition)).get(childPosition);
}
#Override
public long getGroupId(int groupPosition) {
Log.i("Group ID", "Getting group ID");
return groupPosition;
}
#Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
#Override
public boolean hasStableIds() {
return true;
}
#Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
Log.i("Group View", "Group view called");
if (convertView == null) {
LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
convertView = mInflater.inflate(R.layout.row_item, null);
}
final RowItem row_pos = rowItem.get(groupPosition);
TextView txtTitle = (TextView) convertView.findViewById(R.id.title);
txtTitle.setText(row_pos.getText());
Button button = (Button) convertView.findViewById(R.id.button); //TODO: Check memory for subscription status
if (row_pos.isSubscribed()){
row_pos.setSubscribed(false);
button.setBackgroundResource(R.drawable.checkmark3);
} else {
row_pos.setSubscribed(true);
button.setBackgroundResource(R.drawable.ic_add_black_24dp);
}
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {/*
if (item.isSubscribed()){
item.setSubscribed(false); //TODO: This isn't working properly.
v.setBackgroundResource(R.drawable.checkmark3);
} else {
item.setSubscribed(true);
v.setBackgroundResource(R.drawable.ic_add_black_24dp);
}*/
}
});
return convertView;
}
#Override
public View getChildView(int groupPosition, final int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.list_item, null);
}
TextView txtListChild = (TextView) convertView
.findViewById(R.id.lblListItem);
txtListChild.setText("TEST");
return convertView;
}
#Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return false;
}
}
All Layout XMLs were inflated properly in the ListView, but nothing is inflating in the ExpandableListView. The method getGroupView isn't even being called. Any help would be appreciated.
Change
return inflater.inflate(R.layout.seasons_fragment, container, false);
to
return rootView;