Android animation not working in BaseAdapter - java

i am trying to use Pulsator4Droid.
https://github.com/booncol/Pulsator4Droid
It works outside of a BaseAdapter, although inside the adapter it does not work.
I have tried it in multiple basic BaseAdapters and none of them work, although XML animations do... Any ideas why this might be happening?
home_menu_grid_pulse_item.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="wrap_content"
android:orientation="vertical"
android:gravity="center"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:paddingLeft="10dp"
android:paddingRight="10dp">
<pl.bclogic.pulsator4droid.library.PulsatorLayout
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/pulsator"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:pulse_count="4"
app:pulse_duration="7000"
app:pulse_repeat="0"
app:pulse_color="#FF0000"
app:pulse_interpolator="Linear"
app:pulse_startFromScratch="true"
android:layout_gravity="center_horizontal"
android:foregroundGravity="center_horizontal"
android:gravity="center_horizontal">
</pl.bclogic.pulsator4droid.library.PulsatorLayout>
</LinearLayout>
HomeMenuGridAdapter.java :
package com.au.test.helpers.common;
import android.annotation.SuppressLint;
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.ImageView;
import android.widget.TextView;
import com.au.test.R;
import java.util.List;
import pl.bclogic.pulsator4droid.library.PulsatorLayout;
#SuppressLint("InflateParams")
public class HomeMenuGridAdapter extends BaseAdapter {
private final Activity context;
private List<HomeMenuItem> array = null;
public HomeMenuGridAdapter(Context mContext, List<HomeMenuItem> _list) {
context = (Activity) mContext;
array = _list;
}
public int getCount() {
return array.size();
}
public Object getItem(int position) {
return array.get(position);
}
public long getItemId(int position) {
return position;
}
public View getView(final int position, View convertView, ViewGroup arg2) {
if (convertView == null) {
LayoutInflater inflater = context.getLayoutInflater();
convertView = inflater.inflate(R.layout.home_menu_grid_pulse_item, null);
}
// Get Controls
PulsatorLayout pulsator = (PulsatorLayout) convertView.findViewById(R.id.pulsator);
pulsator.start();
return convertView;
}
}

modify your getView method like following. you should use setTag and getTag approach to achieve this.
public View getView(final int position, View convertView, ViewGroup arg2) {
if (null == convertView) {
LayoutInflater inflater = context.getLayoutInflater();
convertView = inflater.inflate(R.layout.home_menu_grid_pulse_item, null);
}
// Get Controls
PulsatorLayout pulsator = (PulsatorLayout) convertView.findViewById(R.id.pulsator);
pulsator.start();
//set tag on converview with your key
convertView.setTag("yourKEY");
return convertView;
}
to get that access of this control outside of adapter you need to use getTag like following in your activity.
View v = yourcontrolobject.findViewWithTag("yourKEY");
if (v != null) {
PulsatorLayout obj= (PulsatorLayout) v.findViewById(R.id.pulsator);
// do whatever you wnat to do now with this obj.
}

put this code in your Adapter class, it's working properly...
Have a look at this
public class MyAdapter extends BaseAdapter {
private Context mContext;
public MyAdapter(Context context) {
mContext = context;
}
#Override
public int getCount() {
return 10;
}
#Override
public Object getItem(int i) {
return i;
}
#Override
public long getItemId(int i) {
return i;
}
#Override
public View getView(int i, View view, ViewGroup viewGroup) {
final ViewHolder viewHolder;
if(view == null) {
viewHolder = new ViewHolder();
view = LayoutInflater.from(mContext).inflate(R.layout.row_file, viewGroup, false);
viewHolder.pulsator = (PulsatorLayout) view.findViewById(R.id.pulsator);
viewHolder.tvCount = (TextView) view.findViewById(R.id.tv_count);
view.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) view.getTag();
}
viewHolder.pulsator.start();
viewHolder.tvCount.setText("My listview row No-> " + i);
return view;
}
public static class ViewHolder {
PulsatorLayout pulsator;
TextView tvCount;
}
}

Turns out it was the width and height of the XML view.
Programatically setting the width and heigh doesn't work with pulseView.setIconHeight(200) and pulseView.setIconWidth(200), neither does wrap_content or match_parent on the View (even when the parent is full screen :/), i had to hard-code an actual size..
I made my View's height and width 200px and it works... Not very elegant but it's good enough :)

Related

How can i create a custom ListView with my xml layout file?

Java code
list view layout #1
How can i insert change the graphics of my ListView adding this layout file to my list view? What should i write in my java code for making each row of my ListView just like my layout file. Thank you all guys !!
First of all you need to create the xml for the custom row you want(custom_row.xml):
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="8dp"
android:id="#+id/text" /> </LinearLayout>
Then you need to create your custom adapter:
public class CustomAdapter extends BaseAdapter {
Context context;
List<String> textArray;
LayoutInflater inflater;
public RutinaAdapter(Context context, List<String> textarray) {
this.context = context;
inflater = LayoutInflater.from(context);
this.textArray = textarray;
}
#Override
public int getCount() {
return textArray.size();
}
#Override
public Object getItem(int position) {
return textArray.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewGroup vg;
if (convertView != null) {
vg = (ViewGroup) convertView;
} else {
vg = (ViewGroup) inflater.inflate(R.layout.custom_row, null);
}
String text = textArray.get(position);
final TextView text = ((TextView) vg.findViewById(R.id.text));
return vg;
} }
Then you need to add the adapter to the ListView:
list = (ListView) view.findViewById(R.id.list);
CustomAdapter adapter = new CustomAdapter(getContext(),textArray);
list.setAdapter(adapter1);
Add info to you textArray, and notify the adapter data changed, and thats it.

How to display two string arrays (that are not located in res) in a list view

I want to display String[] arrayTitles on top of String[] arrayDescriptions, which should be sub items, in the list view. How am I supposed to do this? Posting a working version of this code would be much appreciated.
ListView listview = (ListView)findViewById(R.id.listview);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_map_list);
String[] arrayTitles = formattedTitles.split(",");
String[] arrayDescriptions = formattedDescriptions.split(",");
for (int i = 0; i < arrayLocations.length; i++) {
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>
(context, android.R.layout.simple_list_item_1, arrayTitles);
listview.setAdapter(arrayAdapter);
}
}
I am posting answer using Custom Adapter, you can just create one class CustomAdapter.java
package com.example.your.package.name;
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 CustomAdapter extends BaseAdapter {
Context mContext;
String[] arrayTitles;
String[] arrayDescriptions;
public CustomAdapter(Context mContext,String[] arrayTitles,String[] arrayDescriptions)
{
this.mContext = mContext;
this.arrayTitles = arrayTitles;
this.arrayDescriptions = arrayDescriptions;
}
#Override
public int getCount() {
return arrayTitles.length;
}
#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) {
View v = convertView;
if (v == null) {
LayoutInflater vi;
vi = LayoutInflater.from(parent.getContext());
v = vi.inflate(R.layout.item_list_row, null);
}
TextView tvTitle = (TextView) v.findViewById(R.id.txtTitle);
TextView tvDesc = (TextView) v.findViewById(R.id.txtDesc);
tvTitle.setText(arrayTitles[position]);
tvDesc.setText(arrayDescriptions[position]);
return v;
}
}
create one item_list_row.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">
<TextView
android:id="#+id/txtTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Title"
android:padding="5dp"
android:textStyle="bold"/>
<TextView
android:id="#+id/txtDesc"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Description"
android:paddingLeft="5dp"
android:textStyle="normal"/>
</LinearLayout>
and replace ArrayAdapter with CustomAdapter in your Activity(having ListView)
your onCreate method will look like
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_view);
ListView listview = (ListView)findViewById(R.id.listview);
String[] arrayTitles = formattedTitles.split(",");
String[] arrayDescriptions = formattedDescriptions.split(",");
CustomAdapter arrayAdapter = new CustomAdapter(ListViewActivity.this, arrayTitles,arrayDescriptions);
listview.setAdapter(arrayAdapter);
}
I think It will work fine, If it will not work let me suggest what else you want, I will edit my answer
Thank you
Use two types view
I posting working code below main logic in getview().
public class FriendsList extends BaseAdapter{
private Context context;
private final int Separater_Type = 0;
private final int List_Type = 1;
ArrayList< FriendsInfo> list=new ArrayList<FriendsInfo>();
private ActiveList mActiveList = null;
ImageLoader imageLoader = MyApplication.getInstance().getImageLoader();
public FriendsList(Context context,ArrayList<FriendsInfo> list,ActiveList activeList){
this.context=context;
mActiveList = activeList;
this.list=list;
}
#Override
public int getCount() {
return list.size();
}
#Override
public Object getItem(int position) {
return list.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public int getItemViewType(int position) {
return list.get(position).getType();
}
ViewHolder holder = null;
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
int rowType;
rowType = list.get(position).type;
if (convertView == null) {
holder = new ViewHolder();
switch (rowType){
case Separater_Type:
convertView = View.inflate(context, R.layout.activelistheader, null);
holder.headerName = (TextView) convertView.findViewById(R.id.list_separator);break;
case List_Type:
convertView = View.inflate(context, R.layout.rowlist, null);
holder.avatar = (CircleImageView)convertView.findViewById(R.id.avatarimage);
holder.headerName = (TextView) convertView.findViewById(R.id.name);
holder.selectMark = (ImageView) convertView.findViewById(R.id.select_img);
//holder.select_img = (ImageView)convertView.findViewById(R.id.select_img);
break;
}
convertView.setTag(holder);
}
else{
holder = (ViewHolder) convertView.getTag();
}
if(list.get(position).type ==0){
holder.headerName.setText(list.get(position).name);
}else{
}
return convertView;
}
private class ViewHolder {
CircleImageView avatar;
TextView headerName;
ImageView selectMark;
}
#Override
public int getViewTypeCount() {
return 2;
}
#Override
public boolean areAllItemsEnabled() {
return false;
}
#Override
public boolean isEnabled(int position) {
return true;
}
}

Adapter's getItem(position) and setBackgroundResource

Basically I've a View-pager where I can swipe between 1-6 pictures, using an adapter for that.
In order to "keep track" of the pictures, I implemented a selector which is represented by Image-views inflated using an adapter.
What's next, I'm using setOnPageChangeListener, more exactly the onPageSelected(int position) method to see what's the current Image-view and change its background so it can be differentiated by the rest of them. BUT this doesn't work, although it targets the proper image, the resource is not changed.
Here is some code for a better understanding:
This is where I set the size of the adapter
int poop = profile.getImages().size();
for (int i = 0; i < poop; i++) {
ImageView image = new ImageView(v.getContext());
imageList.add(image);
}
selectorAdapter = new SelectorAdapter(imageList, (android.support.v4.app.FragmentActivity) v.getContext());
selector_view.setAdapter(selectorAdapter);
This is the adapter:
public class SelectorAdapter extends BaseAdapter {
private List<ImageView> mArray = new ArrayList<ImageView>();
private FragmentActivity mContext;
public SelectorAdapter(List<ImageView> mArray, FragmentActivity mContext) {
this.mArray = mArray;
this.mContext = mContext;
}
#Override
public int getCount() {
return mArray.size();
}
#Override
public Object getItem(int position) {
return mArray.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater mInflater = (LayoutInflater)
mContext.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
convertView = mInflater.inflate(R.layout.selector_item, null);
}
return convertView;
}
}
Layout for the adapter:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/selector_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/p_photo_unmarked"
android:layout_marginLeft="5dp"/>
And finally where the magic's supposed to happen:
profilePic.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
#Override
public void onPageSelected(int position) {
if (selectorAdapter.getItem(position) instanceof ImageView) {
((ImageView) selectorAdapter.getItem(position)).setBackgroundResource((R.drawable.p_photo_marked));
Toast.makeText(getActivity().getApplicationContext(), "position" + position, Toast.LENGTH_SHORT).show();
}
Change the line code
((ImageView) selectorAdapter.getItem(position)).setBackgroundResource((R.drawable.p_photo_marked));
to
((ImageView) selectorAdapter.getItem(position)).setBackground(getResources().getDrawable(R.drawable.p_photo_marked))
EDITED:
((ImageView)((SimpleTabPagerAdapter) selector_view.getAdapter()).getItem(position)).setBackgroundResource((R.drawable.p_photo_marked));

Start Timer in GridView

I have a problem with the GridView.
I would like to have a Grid with pictures and under each of them should be a timer. When I click one of the images, there should start the timer bellow of it.
How can I adapt TextViews and Images to a Gridview and let the TextView change every second.
I changed my code now to this
MainActivity
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.Toast;
import java.util.ArrayList;
public class MainActivity extends Activity implements Runnable{
/** The Constant INTERVALL. */
private static final int INTERVALL = 1000;
private ImageAdapter mAdapter;
private ArrayList<String> listText;
private ArrayList<Integer> listImage;
private GridView gridView;
/** The handler. */
private Handler handler = new Handler();
public boolean timerRuns = false;
public int time ;
/**
* Called when the activity is first created.
*/
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.grid);
//GridView gridview = (GridView) findViewById(R.id.gridview);
//gridview.setAdapter(new ImageAdapter(this));
prepareList();
// prepared arraylist and passed it to the Adapter class
mAdapter = new ImageAdapter(this, listText, listImage);
// Set custom adapter to gridview
gridView = (GridView) findViewById(R.id.gridView);
gridView.setAdapter(mAdapter);
// Implement On Item click listener
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position,
long arg3) {
Toast.makeText(MainActivity.this, mAdapter.getItem(position), Toast.LENGTH_SHORT).show();
}
});
}
#Override
public final void run() {
chronometer();
}
public void startTimer(View v) {
timerRuns = true;
time = 5*60;
update();
this.handler.postDelayed(this, INTERVALL);
}
public void stopTimer(View v) {
timerRuns = false;
handler.removeCallbacks(this);
}
public void chronometer(){
time = time -1;
update();
handler.postDelayed(this, INTERVALL);
}
private void update(){
updateScreen();
}
private void updateScreen(){
//TextView tvChronometer = (TextView)findViewById(R.id.tvChronometer);
//tvChronometer.setText(Integer.toString(time));
}
public void prepareList()
{
listText = new ArrayList<String>();
listText.add("Sample");
listText.add("Brazil");
listText.add("Canada");
listText.add("China");
listText.add("France");
listText.add("Germany");
listText.add("Iran");
listText.add("Italy");
listText.add("Japan");
listText.add("Korea");
listText.add("Mexico");
listText.add("Netherlands");
listText.add("Portugal");
listText.add("Russia");
listText.add("Saudi Arabia");
listText.add("Spain");
listText.add("Turkey");
listText.add("United Kingdom");
listText.add("United States");
listImage = new ArrayList<Integer>();
listImage.add(R.drawable.sample_0);
listImage.add(R.drawable.sample_3);
listImage.add(R.drawable.sample_3);
listImage.add(R.drawable.sample_7);
listImage.add(R.drawable.sample_1);
listImage.add(R.drawable.sample_6);
listImage.add(R.drawable.sample_2);
listImage.add(R.drawable.sample_7);
listImage.add(R.drawable.sample_4);
listImage.add(R.drawable.sample_5);
listImage.add(R.drawable.sample_0);
listImage.add(R.drawable.sample_5);
listImage.add(R.drawable.sample_0);
listImage.add(R.drawable.sample_2);
listImage.add(R.drawable.sample_1);
listImage.add(R.drawable.sample_4);
listImage.add(R.drawable.sample_3);
listImage.add(R.drawable.sample_1);
listImage.add(R.drawable.sample_6);
}
}
The ImageAdapter
`public class ImageAdapter extends BaseAdapter {
private ArrayList<String> listText;
private ArrayList<Integer> listImage;
private Activity activity;
public ImageAdapter(Activity activity,ArrayList<String> listText, ArrayList<Integer> listImage) {
super();
this.listText = listText;
this.listImage = listImage;
this.activity = activity;
}
#Override
public int getCount() {
return listText.size();
}
#Override
public String getItem(int position) {
return listText.get(position);
}
#Override
public long getItemId(int position) {
return 0;
}
public static class ViewHolder
{
public ImageView imgView;
public TextView txtView;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder view;
LayoutInflater inflator = activity.getLayoutInflater();
if(convertView==null)
{
view = new ViewHolder();
convertView = inflator.inflate(R.layout.element, null);
view.txtView = (TextView) convertView.findViewById(R.id.eText);
view.imgView = (ImageView) convertView.findViewById(R.id.eImage);
convertView.setTag(view);
}
else
{
view = (ViewHolder) convertView.getTag();
}
view.txtView.setText(listText.get(position));
view.imgView.setImageResource(listImage.get(position));
return convertView;
}
}`
my gridview
<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/gridView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:columnWidth="110dp"
android:numColumns="auto_fit"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:stretchMode="columnWidth"
android:gravity="center"
/>
and my element.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="5dp" >
<ImageView
android:id="#+id/eImage"
android:layout_width="120dp"
android:layout_height="120dp"
android:src="#color/Bisque" >
</ImageView>
<TextView
android:id="#+id/eText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:textSize="15sp" >
</TextView>
I hope you can help me.
1. Creating a gridview with text and image in each cell
Take a look at the documentation for GridView:
http://developer.android.com/guide/topics/ui/layout/gridview.html
The key method there is the public View getView(int position, View convertView, ViewGroup parent)
That method returns a view for each cell in the grid. You can create an XML layout that includes the TextView and ImageView. In the getView method for the adapter inflate the XML layout, attach an onClickListener so you can start the timer and return it as a view.
2. Updating the TextView from the grid
Keep in mind that a gridview contains contains multiple views, each of which can come from the same resource so this: TextView tvChronometer = (TextView)findViewById(R.id.tvChronometer); is not going to work in a gridview.
What you need to do is to get the specific view from the cell that you need (or iterate through all of them) and update the view. Something like this:
//First get the view for the cell assuming it's a composite view
View cellView = (TextView) gridview.getChildAt(position);
//Then get the actual TextView from that grid cell
TextView tvChronometer = cellView.findViewById(R.id.tvChronometer);
Although the best way in my opinion is to save a reference to the TextView when you a creating/instantiating it in the adapter.

Android ImageAdapter with Gridview in Fragment

I have an adapter with gridview that works as an Activity. I am trying to place it in a Fragment now and converted things but it does not work. When I include the IconFragmentSystem in my Activity I get a force close when I try to open the Activity.
I know the Activity works because I can use other Fragments and everything is okay so I know my issue lies within this file.
package com.designrifts.ultimatethemeui;
import com.designrifts.ultimatethemeui.R;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;
import java.util.ArrayList;
public class IconFragmentSystem extends Fragment implements AdapterView.OnItemClickListener{
private static final String RESULT_OK = null;
public Uri CONTENT_URI;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.main, container, false);
super.onCreate(savedInstanceState);
int iconSize=getResources().getDimensionPixelSize(android.R.dimen.app_icon_size);
GridView gridview = (GridView) getActivity().findViewById(R.id.icon_grid);
gridview.setAdapter(new IconAdapter(this, iconSize));
gridview.setOnItemClickListener(this);
CONTENT_URI=Uri.parse("content://"+iconsProvider.class.getCanonicalName());
return view;
}
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
String icon=adapterView.getItemAtPosition(i).toString();
Intent result = new Intent(null, Uri.withAppendedPath(CONTENT_URI,icon));
setResult(RESULT_OK, result);
finish();
}
private void setResult(String resultOk, Intent result) {
// TODO Auto-generated method stub
}
private void finish() {
// TODO Auto-generated method stub
}
private class IconAdapter extends BaseAdapter{
private Context mContext;
private int mIconSize;
public IconAdapter(Context mContext, int iconsize) {
super();
this.mContext = mContext;
this.mIconSize = iconsize;
loadIcon();
}
public IconAdapter(IconFragmentSystem iconssystem, int iconSize) {
// TODO Auto-generated constructor stub
}
#Override
public int getCount() {
return mThumbs.size();
}
#Override
public Object getItem(int position) {
return mThumbs.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) {
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(mIconSize, mIconSize));
} else {
imageView = (ImageView) convertView;
}
imageView.setImageResource(mThumbs.get(position));
return imageView;
}
private ArrayList<Integer> mThumbs;
////////////////////////////////////////////////
private void loadIcon() {
mThumbs = new ArrayList<Integer>();
final Resources resources = getResources();
final String packageName = getActivity().getApplication().getPackageName();
addIcon(resources, packageName, R.array.systemicons);
}
private void addIcon(Resources resources, String packageName, int list) {
final String[] extras = resources.getStringArray(list);
for (String extra : extras) {
int res = resources.getIdentifier(extra, "drawable", packageName);
if (res != 0) {
final int thumbRes = resources.getIdentifier(extra,"drawable", packageName);
if (thumbRes != 0) {
mThumbs.add(thumbRes);
}
}
}
}
}
}
I have tried different ways to implement this but all have failed and I could really use help pointing me in the right direction.
This is my xml in layout
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<GridView
android:id="#+id/gridview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:columnWidth="90dp"
android:numColumns="auto_fit"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:stretchMode="columnWidth"
android:gravity="center"
/>
</RelativeLayout>
I was able to fix the issue
I changed
GridView gridview = (GridView) getActivity().findViewById(R.id.icon_grid);
to
GridView gridview = (GridView) view.findViewById(R.id.icon_grid);
and I also changed
gridview.setAdapter(new IconAdapter(this, iconSize));
to
gridview.setAdapter(new IconAdapter(getActivity(), iconSize));
Try this.
gridView = (GridView) rootView.findViewById(R.id.gridView);
gridView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view,int pos, long l) {
// do something...
}
});
For more info, see this link:
Android gridview in fragment adapter constructor missing
GridView gridview = (GridView) getActivity().findViewById(R.id.icon_grid);
should be
GridView gridview = (GridView) container.findViewById(R.id.icon_grid);
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.main, container, false);
int iconSize = getResources().getDimensionPixelSize(android.R.dimen.app_icon_size);
GridView gridview = (GridView) view.findViewById(R.id.icon_grid);
gridview.setAdapter(new IconAdapter(this, iconSize));
gridview.setOnItemClickListener(this);
CONTENT_URI = Uri.parse("content://"+iconsProvider.class.getCanonicalName());
return view;
}
Your going to want to remove the call to super, that is a leftover from you activity, the fragment does not call this method.

Categories