Hey i'm trying to build an app in which the user can input some numbers and these numbers
will be shown in a gridview with custom grid view items. If i use the common gridview items of android it works. But if i use my own layout it doesnt work. Only the first element in
the array will be shown. Does anybody have a clou why this doesn't work?
public class GridViewAppActivity extends Activity {
private ArrayList<String> items;
private GridViewAdapter adapter;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
items = new ArrayList<String>();
adapter = new GridViewAdapter(this);
GridView gridView = (GridView) findViewById(R.id.gridView);
gridView.setAdapter(adapter);
final Button addButton = (Button) findViewById(R.id.add_button);
final EditText itemEditText = (EditText) findViewById(R.id.number_field);
// Click listener for Button
addButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
items.add(itemEditText.getText().toString());
itemEditText.setText("");
adapter.notifyDataSetChanged();
}
});
}
public class GridViewAdapter extends BaseAdapter {
private Context mContext;
public GridViewAdapter(Context c) {
mContext = c;
}
public int getCount() {
return items.size();
}
public Object getItem(int position) {
return items.get(position);
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
View v;
if(convertView==null){
LayoutInflater li = getLayoutInflater();
v = li.inflate(R.layout.grid_item, null);
TextView tv = (TextView)v.findViewById(R.id.grid_item_text);
tv.setText(items.get(position));
} else {
v = convertView;
}
return v;
}
}
}
Try this,
public View getView(int position, View convertView, ViewGroup parent) {
View v;
if(convertView == null) {
LayoutInflater li = getLayoutInflater();
v = li.inflate(R.layout.grid_item, null);
} else {
v = convertView;
}
TextView tv = (TextView)v.findViewById(R.id.grid_item_text);
tv.setText(items.get(position));
return v;
}
Related
I am having 5 font styles (Typeface) and want to show user input 5 times into a ListView in different font style.
Below is my code I have tried.
public void ShowTextStyles(String inputText, int color){
final Dialog dialog = new Dialog(EditImageActivity.this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.textstylelayout);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
// Array of strings...
final String[] textStyles={"Gobold Bold.ttf","beyond_wonderland.ttf"};
final String[] mobileArray = {inputText,inputText,inputText,inputText,inputText,inputText,inputText,inputText,inputText,inputText};
ArrayAdapter adapter = new ArrayAdapter<String>(EditImageActivity.this,
R.layout.text_style_list, mobileArray);
ListView listView = (ListView) dialog.findViewById(R.id.mobile_list);
listView.setAdapter(adapter);
// Set an item click listener for ListView
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// Get the selected item text from ListView
String selectedItem = (String) parent.getItemAtPosition(position);
// Display the selected item text on TextView
}
});
dialog.setCancelable(true);
dialog.show();
}
Create custom adapter and use it for your listview:
public class TestAdapter extends BaseAdapter {
private Context context;
private List<String> appData;
String[] fontFiles = {"Gobold Bold.ttf", "beyond_wonderland.ttf"}; // add more fonts if wish more styles
public TestAdapter(Context context, List<String> appData) {
this.context = context;
this.appData = appData;
}
#Override
public int getCount() {
return appData.size();
}
#Override
public Object getItem(int position) {
return appData.get(position);
}
#Override
public long getItemId(int position) {
return 0;
}
private class ViewHolder {
TextView tv_item;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.list_app, null);
holder = new ViewHolder();
holder.tv_item = convertView.findViewById(R.id.tv_item);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.tv_item.setText(appData.get(position));
Typeface typeface;
// change this condition according to your need
if (position == 0){
typeface = Typeface.createFromAsset(context.getAssets(), fontFiles[0]);
}else {
typeface = Typeface.createFromAsset(context.getAssets(), fontFiles[1]);
}
holder.tv_item.setTypeface(typeface);
return convertView;
}
}
For that you need to use BaseAdapter
public class Myadapter extends BaseAdapter {
AssetManager assetManager = getAssets();
LayoutInflater lif;
ImageView sideArrow;
TextView tv;
public Myadapter(Context ctx) {
lif = (LayoutInflater) ctx.getSystemService(LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
return favarets.size();
}
#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 vi = convertView;
if (convertView == null)
vi = lif.inflate(R.layout.inflate, null);
sideArrow = (ImageView) vi.findViewById(R.id.imageViewsidemark);
tv = (TextView) vi.findViewById(R.id.textFav);
tv.setText(favarets.get(position));
final Typeface tvFont = Typeface.createFromAsset(assetManager, "OPTIMA.TTF");
tv.setTypeface(tvFont);
tv.setTextColor(Color.BLACK);
return vi;
}
}
Here is the example how to use customAdapter
https://guides.codepath.com/android/Using-a-BaseAdapter-with-ListView
Solution 2:
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(),
android.R.layout.simple_list_item_1, filled_arr) {
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);
TextView text = (TextView) view.findViewById(android.R.id.text1);
text.setTypeface(typeface);
return view;
}
};
I have dynamically populated ImageViews and Textviews.
Now I have small problem with implementing onClick method on ImageView. On some ImageView click it should switch to fragment and for some click onto activity.
here is my code - my main fragment:
public class MainFragment extends Fragment {
public static String[] gridViewStrings = {
"string1",
"string2",
"string3",
"string4",
"string5",
"string6"
};
public static int[] gridViewImages = {
R.drawable.delivery,
R.drawable.shipping_logs,
R.drawable.meassurement,
R.drawable.takeovers,
R.drawable.settings,
R.drawable.download_data
};
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.main_fragment, parent, false);
gridView = (GridView) view.findViewById(R.id.grid);
gridView.setAdapter(new CustomAndroidGridViewAdapter(getActivity(), gridViewStrings, gridViewImages));
imageView = (ImageView) view.findViewById(R.id.gridview_image);
}
my custom gridview adapter:
public class CustomAndroidGridViewAdapter extends BaseAdapter {
private Context mContext;
private final String[] string;
private final int[] Imageid;
public CustomAndroidGridViewAdapter(Context c,String[] string,int[] Imageid ) {
mContext = c;
this.Imageid = Imageid;
this.string = string;
}
#Override
public int getCount() {
return string.length;
}
#Override
public Object getItem(int p) {
return null;
}
#Override
public long getItemId(int p) {
return 0;
}
#Override
public View getView(final int p, final View convertView, ViewGroup parent) {
View grid;
LayoutInflater inflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
grid = new View(mContext);
grid = inflater.inflate(R.layout.grid, null);
TextView textView = (TextView) grid.findViewById(R.id.gridview_text);
ImageView imageView = (ImageView)grid.findViewById(R.id.gridview_image);
textView.setText(string[p]);
imageView.setImageResource(Imageid[p]);
} else {
grid = (View) convertView;
}
return grid;
}
}
QUESTION: How to implement onClick() on ImageView and switch to fragment and activity?
public DlgOutletListAdapter(Context context, List<WrapperOutlet> wrapperOutletList, View.OnClickListener listener) {
this.context = context;
this.wrapperOutletList = wrapperOutletList;
this.listener = listener; //---> pass in listener....
}
#Override
public int getCount() {
return wrapperOutletList.size();
}
#Override
public Object getItem(int position) {
return wrapperOutletList.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
if (convertView == null) {
convertView = LayoutInflater.from(context).inflate(R.layout.item_dlg_outlet_list, parent, false);
viewHolder = new ViewHolder(convertView);
viewHolder.ll_dlg_outlet_list_container.setOnClickListener(listener);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
if (!wrapperOutletList.isEmpty()) {
WrapperOutlet wrapperOutlet = wrapperOutletList.get(position);
Outlet outlet = wrapperOutlet.getOutlet();
if (outlet != null) {
if (TextUtils.isEmpty(outlet.getTel())){
viewHolder.ivCall.setImageResource(R.drawable.icon_call2);
viewHolder.tvCall.setTextColor(context.getResources().getColorStateList(R.color.medium_gray));
viewHolder.ll_dlg_outlet_list_dialer.setOnClickListener(null);
} else {
viewHolder.ivCall.setImageResource(R.drawable.icon_call);
viewHolder.tvCall.setTextColor(context.getResources().getColorStateList(R.color.fresh_teal));
viewHolder.ll_dlg_outlet_list_dialer.setOnClickListener(listener);
}
}
}
return convertView;
}
Pass in listener. Give you an example above. code below is where you call the adapter in fragment. My fragment implements View.OnClickListener
lv_dlg_outlet_list.setAdapter(new DlgOutletListAdapter(activity, wrapperOutletList, this));
Interface Callback:
public interface LaunchFragmentWithImage{
void launchFragmentWithImage(int imageID);
}
Give the adapter a reference of this class below so it can call the method. Wouldn't recommend giving the adapter itself a reference to the class because its not the adapters responsibility to know who recieves the image, thus violating the Single responsibility principle of OOD, but for the sake of learning it's okay.
public class ClassToLaunchNewFragment extends AppCompatActivity implements
LaunchFragmentWithImage{
#Override //Method from interface we've defined.
public void launchFragmentWithImage(int imageID){
FragmentToLaunch fragmentToLaunch = FragmentToLaunch.newInstance();
Bundle imageData = new Bundle();
imageData.putInt(imageID, "key");
fragmentToLaunch.setArguments(imageData);
getSupportFragmentManager().beginTransaction() //Call replace not add
.replace(containerID, fragmentToLaunch)
.commit();
}
}
In AdapterClass
imageViewVar.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view){
fragmentToLaunchInstanceThatWasPassedIn.launchFragmentWithImage(theImageResource);
}
});
grid = new View(mContext);
grid = inflater.inflate(R.layout.grid, null);
why are you pointing the grid variable to two separate View objects?
if (convertView == null) {
grid = inflater.inflate(R.layout.grid, null);
TextView textView = (TextView) grid.findViewById(R.id.gridview_text);
ImageView imageView = (ImageView)grid.findViewById(R.id.gridview_image);
textView.setText(string[p]);
imageView.setImageResource(Imageid[p]);
imageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
switch(choosedPosition){
case 0:do something;break;
case 1:do something;break;
...
}
}
});
}
It's just an example and can't running.
I checked all the possible solutions to handle the focus of EditText in a ListView.
I have a ListView in a fragment. The adapter of this ListView is composed by an ImageButton, an ImageView and an EditText. I want the EditText focusable.
I tried some solutions, one of these was:
public class HomeFragment extends Fragment {
FileAdapter adapter;
ListView lv;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View view = inflater.inflate(R.layout.fragment_home, container, false);
lv = (ListView)view.findViewById(R.id.listView);
adapter = new FileAdapter(getActivity().getApplicationContext(), list);
lv.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int position, long l) {
if (position == 1){
lv.setItemsCanFocus(true);
lv.setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS);
adapter.nome.requestFocus();
}else{
if (!lv.isFocused()){
lv.setItemsCanFocus(false);
lv.setDescendantFocusability(ViewGroup.FOCUS_BEFORE_DESCENDANTS);
lv.requestFocus();
}
}
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
lv.setDescendantFocusability(ViewGroup.FOCUS_BEFORE_DESCENDANTS);
}
});
lv.setAdapter(adapter);
return view;
}
}
Then in my XML file i have android:descendantFocusability="beforeDescendants" in my ListView.
Here is my FileAdapter class:
public class FileAdapter extends ArrayAdapter<Files> {
LayoutInflater inflater;
EditText nome;
private final Context context;
private ArrayList<Files> list;
public FileAdapter(Context context, ArrayList<Files> list) {
super(context, R.layout.homeadapter, list);
this.context = context;
this.list = list;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
final LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.homeadapter, null);
nome = (EditText) convertView.findViewById(R.id.tvNomeFile);
return convertView;
}
}
Obviously i put here only useful part of my code, as i said before my adapter has also other elements but useless for the question.
The solution i tried doesn't work so i'm searching another.
Yesterday I have the similar problem with my fragment... This is my fragment code:
public static class PlaceholderFragment_MainActivity extends Fragment implements View.OnClickListener {
//Declaration of classes
Floor_Adapter FloorsAdapter;
Floor_List Floors = new Floor_List();
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
//Declaration of controls
View view = inflater.inflate(R.layout.building_view, container, false);
TextView txtvHotelName = (TextView) view.findViewById(R.id.txtvHotelName);
ListView lvFloors = (ListView) view.findViewById(R.id.lvFloors);
txtvHotelName.setText("Hotel");
Floors.setFloorsArray();
FloorsAdapter = new Floor_Adapter(getActivity(),Floors.getFloorsArray(),lvFloors);
lvFloors.setAdapter(FloorsAdapter);
return view;
}
#Override
public void onClick(View view) {
String A;
TextView floor = (TextView)view.findViewById(R.id.txtvFloor);
A = floor.getText().toString();
}
}
And this my adapter code:
public class Floor_Adapter extends ArrayAdapter<Floor_Object>{
//Declaration of atributes
private ArrayList<Floor_Object> Floor_List;
private final Activity context;
private final ListView lvFloors;
public Floor_Adapter(Activity context,ArrayList<Floor_Object> Floors_Array,ListView lvFloors) {
super(context,R.layout.floor_layout,Floors_Array);
this.context = context;
this.Floor_List = Floors_Array;
this.lvFloors = lvFloors;
}
public static class Floor_View{
//Declaration of Atributes
TextView FloorName;
Button btnFloorRow;
ImageView CheckImage;
TextView NoChecks;
ImageView BlockImage;
TextView NoBlocks;
ImageView DirtImage;
TextView NoDirts;
ImageView TroubleImage;
TextView NoTroubles;
ImageButton btnShowTasks;
ImageButton btnShowRooms;
}
#Override
public View getView(int position, View ConvertView, ViewGroup parent) {
//Declaration of Variables
Floor_View rowView;
LayoutInflater inflator = context.getLayoutInflater();
if(ConvertView == null){
rowView = new Floor_View();
ConvertView = inflator.inflate(R.layout.floor_layout,null,true);
rowView.FloorName = (TextView) ConvertView.findViewById(R.id.txtvFloor);
rowView.btnFloorRow = (Button) ConvertView.findViewById(R.id.btnFlooRow);
rowView.CheckImage = (ImageView) ConvertView.findViewById(R.id.ivCheck);
rowView.NoChecks = (TextView) ConvertView.findViewById(R.id.txtvCheckRooms);
rowView.BlockImage = (ImageView) ConvertView.findViewById(R.id.ivBlockRooms);
rowView.NoBlocks = (TextView) ConvertView.findViewById(R.id.txtvBlockedRooms);
rowView.DirtImage = (ImageView) ConvertView.findViewById(R.id.ivDirtRooms);
rowView.NoDirts = (TextView) ConvertView.findViewById(R.id.txtvDirtRooms);
rowView.TroubleImage = (ImageView) ConvertView.findViewById(R.id.ivTroubleRooms);
rowView.NoTroubles = (TextView) ConvertView.findViewById(R.id.txtvTroubleRooms);
rowView.btnShowTasks = (ImageButton) ConvertView.findViewById(R.id.btnShowTasks);
rowView.btnShowRooms = (ImageButton) ConvertView.findViewById(R.id.btnShowRooms);
ConvertView.setTag(rowView);
}
else
{
rowView = (Floor_View) ConvertView.getTag();
}
Floor_Object Floor = Floor_List.get(position);
rowView.FloorName.setText(Floor.getFloorName());
if(Floor.getNoChecks().equals("0")) {
rowView.CheckImage.setVisibility(View.INVISIBLE);
rowView.NoChecks.setVisibility(View.INVISIBLE);
}
else{
rowView.CheckImage.setVisibility(View.VISIBLE);
rowView.NoChecks.setVisibility(View.VISIBLE);
rowView.NoChecks.setText(Floor.getNoChecks());
}
if(Floor.getNoBlocks().equals("0")) {
rowView.BlockImage.setVisibility(View.INVISIBLE);
rowView.NoBlocks.setVisibility(View.INVISIBLE);
}
else{
rowView.BlockImage.setVisibility(View.VISIBLE);
rowView.NoBlocks.setVisibility(View.VISIBLE);
rowView.NoBlocks.setText(Floor.getNoBlocks());
}
if(Floor.getNoDirts().equals("0")) {
rowView.DirtImage.setVisibility(View.INVISIBLE);
rowView.NoDirts.setVisibility(View.INVISIBLE);
}
else{
rowView.DirtImage.setVisibility(View.VISIBLE);
rowView.NoDirts.setVisibility(View.VISIBLE);
rowView.NoDirts.setText(Floor.getNoDirts());
}
if(Floor.getNoTroubles().equals("0")) {
rowView.TroubleImage.setVisibility(View.INVISIBLE);
rowView.NoTroubles.setVisibility(View.INVISIBLE);
}
else{
rowView.TroubleImage.setVisibility(View.VISIBLE);
rowView.NoTroubles.setVisibility(View.VISIBLE);
rowView.NoTroubles.setText(Floor.getNoTroubles());
}
if(Floor.getNoBlocks().equals("0") && Floor.getNoDirts().equals("0") && Floor.getNoTroubles().equals("0")) {
rowView.btnFloorRow.setClickable(false);
rowView.btnShowTasks.setClickable(false);
rowView.btnShowTasks.setImageResource(R.drawable.list_999999_50);
}
else{
rowView.btnFloorRow.setClickable(true);
rowView.btnFloorRow.setOnClickListener(OnShowTasksClickListener);
rowView.btnShowTasks.setClickable(true);
rowView.btnShowTasks.setOnClickListener(OnShowTasksClickListener);
rowView.btnShowTasks.setImageResource(R.drawable.list_3a4b66_50);
}
rowView.btnShowRooms.setOnClickListener(OnShowRoomsClickListener);
return ConvertView;
}
private View.OnClickListener OnShowTasksClickListener = new View.OnClickListener() {
#Override
public void onClick(View v) {
final int position = lvFloors.getPositionForView((View) v.getParent());
}
};
private View.OnClickListener OnShowRoomsClickListener = new View.OnClickListener() {
#Override
public void onClick(View v) {
final int position = lvFloors.getPositionForView((View) v.getParent());
}
};
}
You can see when I work with fragment it's mandatory to create control events in adapter... Just change event of view and that it's your need, I think... I am waiting that sample helps you. If you need more info or help advice me, Good luck!
PD: I think this is your code that you need:
public class HomeFragment extends Fragment {
FileAdapter adapter;
ListView lv;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View view = inflater.inflate(R.layout.fragment_home, container, false);
lv = (ListView)view.findViewById(R.id.listView);
adapter = new FileAdapter(getActivity(), list);
lv.setAdapter(adapter);
return view;
}
}
public class FileAdapter extends ArrayAdapter<Files> {
private final Context context;
private ArrayList<Files> list;
private final ListView Lv;
public FileAdapter(Context context, ArrayList<Files> list,ListView lv) {
super(context, R.layout.homeadapter, list);
this.context = context;
this.list = list;
this.Lv = lv;
}
public static class RowView{
//Declaration of Atributes
EditText Nome;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
RowView rowView;
LayoutInflater inflator = context.getLayoutInflater();
if(ConvertView == null){
rowView = new RowView();
convertView = inflator.inflate(R.layout.homeadapter, null);
rowView.nome = (EditText) convertView.findViewById(R.id.tvNomeFile);
}
rowView.nome.setOnItemSelectedListener(ItemSelecteListener);
return convertView;
}
private View.setOnItemSelectedListener ItemSelecteListener = new View.OnClickListener() {
#Override
public void onItemSelected(View view) {
final int position = lvFloors.getPositionForView((View) v.getParent());
if (position == 1){
lv.setItemsCanFocus(true);
lv.setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS);
}else{
if (!lv.isFocused()){
lv.setItemsCanFocus(false);
lv.setDescendantFocusability(ViewGroup.FOCUS_BEFORE_DESCENDANTS);
lv.requestFocus();
}
}
}
};
}
I have a problem with Android Spinner. I customised an adapter with a BaseAdapter. But when it show in the first time, it not show all items (I have only 6 items). Below is the screenshot
http://pik.vn/2014999186b9-448d-44ed-bd9b-c37484b368c6.png
And when I tap to the Spinner second time it show normally. Below is the screenshot. How can I show full item like that at the first time?
http://pik.vn/201454ed3e5b-4c67-4e9c-b8c6-feaf90c5dfb4.png
Update code:
Below is the adapter:
public class CategoryAdapter extends BaseAdapter implements SpinnerAdapter {
Context mContext;
List<CategoryModel> data;
int selectedCate;
boolean isSpinner;
Spinner spn;
public CategoryAdapter(Context context, List<CategoryModel> data, boolean isSpinner, Spinner spn) {
mContext = context;
selectedCate = 0;
this.data = data;
this.isSpinner=isSpinner;
this.spn = spn;
}
#Override
public int getCount() {
return data.size();
}
#Override
public Object getItem(int position) {
if (position < data.size()) {
return data.get(position);
} else {
return null;
}
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater inflater = LayoutInflater.from(mContext);
convertView = inflater.inflate(R.layout.inflater_category_spinner, parent, false);
}
TextView tvName = (TextView) convertView.findViewById(R.id.inflater_category_spinner_tv);
tvName.setSelected(spn.isSelected());
tvName.setText(data.get(position).getName());
return convertView;
}
#Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = LayoutInflater.from(mContext);
convertView = inflater.inflate(R.layout.inflater_category, parent, false);
ImageView img = (ImageView) convertView.findViewById(R.id.inflater_cate_image);
if (!data.get(position).getName().equals(parent.getContext().getString(R.string.browse))) {
Log.d("postion", "" + position);
UrlImageViewHelper.setUrlDrawable(img, data.get(position).getImage().getUrl());
} else {
img.setImageResource(R.drawable.ic_everything);
}
TextView tvName = (TextView) convertView.findViewById(R.id.inflater_cate_tv_name);
tvName.setText(data.get(position).getName());
return convertView;
}
public void swapView(int position) {
selectedCate = position;
}
}
and below is the code init Spinner in Activity
categoryAdapter = new CategoryAdapter(HomeActivity.this, result,true, mSpnActionbarCenterTitle);
mSpnActionbarCenterTitle.setAdapter(categoryAdapter);
The problem comes from the height of ImageView, it is wrap content and load via lazy loader, and the height of spinner not change when image loaded. My solution is fix the size of ImageView.
I am using a baseadapter to add items in the gridview, I do this in 'onclickevent of alistviewrow. Data is collected and passed to the adapter inarraylist` of custom object.
This is how I am doing it:
#Override
public void onClick(View arg0) {
List<GVShow> lstGVShow = new ArrayList<GVShow>();
//code to construct lstGVShow here..
FragmentTvGuideMain.gridView.setAdapter(new GridViewAdapter(
FragmentTvGuideMain.context.getApplicationContext(),
lstGVShow)); //FragmentTvGuideMain.context = getActivity() in Fragmentactivity
This is my gridviewadapter:
public class GridViewAdapter extends BaseAdapter {
private Context context;
private final List lstGVShow;
public GridViewAdapter(Context context, List<GVShow> lstGVShow) {
this.context = context;
this.lstGVShow = lstGVShow;
}
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View gridView;
if (convertView == null) {
gridView = convertView;//new View(context);
gridView = inflater.inflate(R.layout.gridview_tile_layout,parent, false);
TextView txtShowName = (TextView) gridView.findViewById(R.id.ShowTitle);
TextView txtShowDuration = (TextView) gridView.findViewById(R.id.ShowTime);
ImageButton imgBtnShow = (ImageButton) gridView.findViewById(R.id.image);
GVShow show = lstGVShow.get(position);
txtShowName.setText(show.getShowName());
ImageLoader.getInstance().displayImage(show.getShowImage(), imgBtnShow);
} else {
gridView = (View) convertView;
}
return gridView;
}
#Override
public int getCount() {
return lstGVShow.size();
}
#Override
public Object getItem(int position) {
return null;
}
#Override
public long getItemId(int position) {
return 0;
}
}
This is how I get the child from the gridview and it gives an NPE:
View rlNowPlaying = FragmentTvGuideMain.gridView.getChildAt(0); //done after setting the adapter
FragmentTvGuideMain.gridView.getCount(); //returns the correct number of views added in the gridview
It seems to me like some sort of a non-blocking call to set the views in baseadapter of the gridview. How do I go working around this?