Android : ListFragment loading continuously - android

I am extending my class to ListFragment. Below I posted the relative.
My issue is, I didn't get any error in runtime, but in Output it still loading the page.
Output:
Administration.java:
public class Administration extends ListFragment {
int[] img = { R.drawable.project, R.drawable.user, R.drawable.group,
R.drawable.roles, R.drawable.news, R.drawable.document };
private String item[] = { "Project", "Users", "Group",
"Roles and Permission", "News", "Documents" };
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return super.onCreateView(inflater, container, savedInstanceState);
}
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
getListView().setDividerHeight(2);
getListView().setAdapter(new BindDataAdapter(getActivity(), img, item));
}
#SuppressWarnings("deprecation")
#Override
public void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
for (int a = 0; a < l.getChildCount(); a++) {
l.getChildAt(a).setBackgroundColor(Color.TRANSPARENT);
}
}
}
I didn't use any layout in listFragment.I use the layout in BindDataAdapter.java
BindDataAdapter.java:
public class BindDataAdapter extends BaseAdapter {
Activity mLocal;
int[] imgArray;
String titleA[];
LayoutInflater mLayoutInflater;
public BindDataAdapter(Activity administration, int[] imageArray, String[] title) {
mLocal = administration;
imgArray = imageArray;
titleA = title;
mLayoutInflater = (LayoutInflater) administration
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public int getCount() {
return imgArray.length;
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
private class Holder {
ImageView image;
TextView textView;
}
public View getView(int position, View convertView, ViewGroup parent) {
Holder holder = null;
if (convertView == null) {
convertView = mLayoutInflater.inflate(R.layout.activity_list, null);
holder = new Holder();
holder.image = (ImageView) convertView.findViewById(R.id.iamge);
holder.textView = (TextView) convertView.findViewById(R.id.text1);
convertView.setTag(holder);
} else {
holder = (Holder) convertView.getTag();
}
holder.image.setBackgroundResource(imgArray[position]);
holder.textView.setText(titleA[position]);
return convertView;
}
}
activity_list.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/whitelist"
android:orientation="horizontal"
>
<ImageView
android:id="#+id/iamge"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_margin="4dp"
android:contentDescription="#string/app_name" >
</ImageView>
<TextView
android:id="#+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="20dp"
android:padding="#dimen/padding_medium"
android:paddingLeft="10dp"
android:text="#string/hello_world"
android:textColor="#android:color/black"
android:textStyle="bold"
/>
</LinearLayout>
I didn't know why it has been loading continuously. Any help is mostly appreciated.Thank you.

Probably showing progress-bar is default in-built progress for ListFragment. So use setListShown to dismiss built-in indeterminant progress-bar after setting adapter for ListView as:
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
.....
setListShown(true);
}

Related

Custom Spinner setOnItemSelectedListener is not triggered

I have a spinner and an adapter.
Spinner spinner = findViewById(R.id.spinner);
SpinnerAdapter adapter = new SpinnerAdapter(this, R.layout.spinner_item, getList());
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
{
#Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id)
{
Log.e("PROGRAM", "Selected |" + position + "|");
}
#Override
public void onNothingSelected(AdapterView<?> parentView)
{
}
});
This is my SpinnerAdapter dropdown creation
#Override
public View getDropDownView(final int position, final View convertView, final ViewGroup parent)
{
MyObject object = getItem(position);
View layout = LayoutInflater.from(parent.getContext()).inflate(R.layout.my_object_item, parent, false);
((TextView) layout.findViewById(R.id.name)).setText(object.getName());
return layout;
}
And my_object_item xml is
<?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:id="#+id/item"
android:clickable="true"
android:focusable="true">
<ImageView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="#+id/icon"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/name"/>
</LinearLayout>
For some reason I can not get setOnItemSelectedListener to trigger. Should I be adding listener to every component of my_object_item and from there triggering setOnItemSelectedListener?
As you have suggested above, you can try to do things manually, by adding a listener that listens to clicks on either the TextView or ImageView (the components of each item in the spinner dropdown view), and then update the spinner view according to what was selected.
1) MainActivity.class:------------
public class MainActivity extends AppCompatActivity implements Listener {
private Spinner sp;
private SpinnerAdapter spinnerAdapter;
private String[] name = {
"N1",
"N2",
"N3",
"N4"
};
private int[] icon = {
R.drawable.icon,
R.drawable.icon,
R.drawable.icon,
R.drawable.icon
};
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
sp = (Spinner) findViewById(R.id.sp);
spinnerAdapter = new SpinnerAdapter(getApplicationContext() , name , icon , name[0] , icon[0] , MainActivity.this); // TODO: Every time the activity (spinner) is created, the item selected is changed
//TODO to the initial name[0] and icon[0]. In order to solve this issue you can save the selected item (name and icon) value in sharedPreferences and use the saved value every time the spinner is opened.
sp.setAdapter(spinnerAdapter);
}
#Override
public void clicked(int position) {
if(spinnerAdapter != null){
spinnerAdapter.setCurrentNameIcon(name[position] , icon[position]);
}
}
2) SpinnerAdapter.class:---------------
public class SpinnerAdapter extends BaseAdapter {
private Context mContext;
private LayoutInflater layoutInflater;
private Listener callback;
private String current_name;
private int current_icon;
private String[] name;
private int[] icon;
public SpinnerAdapter(#NonNull Context context , String[] name_ , int[] icon_ , String inital_name_ , int initial_icon_ , Listener l) {
mContext = context;
layoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if(l != null){
callback = l;
}
name = name_;
icon = icon_;
current_name = inital_name_;
current_icon = initial_icon_;
}
public int getCount() {
return name.length;
}
public Object getItem(int position) {
return name[position];
}
public long getItemId(int position) {
return position;
}
#NonNull
public View getView(final int position, View convertView, ViewGroup parent) {
View view = convertView;
final TextView tv;
ImageView iv;
if (convertView == null) {
if (layoutInflater != null) {
view = layoutInflater.inflate(R.layout.my_object_item, parent , false);
}
}
tv = (TextView) view.findViewById(R.id.name);
tv.setText(current_name);
iv = (ImageView) view.findViewById(R.id.icon);
iv.setBackground(mContext.getResources().getDrawable(current_icon));
return view;
}
#Override
public View getDropDownView(final int position, #Nullable View convertView, #NonNull final ViewGroup parent) {
View view = convertView;
final TextView tv;
ImageView iv;
if (convertView == null) {
if (layoutInflater != null) {
view = layoutInflater.inflate(R.layout.my_object_item, parent , false);
}
}
tv = (TextView) view.findViewById(R.id.name);
tv.setText(name[position]);
tv.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(callback != null){
//https://stackoverflow.com/questions/7287195/android-spinner-close?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa
View root = parent.getRootView();
root.dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_BACK));
root.dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_BACK));
callback.clicked(position);
}
}
});
iv = (ImageView) view.findViewById(R.id.icon);
iv.setBackground(mContext.getResources().getDrawable(icon[position]));
iv.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(callback != null){
//https://stackoverflow.com/questions/7287195/android-spinner-close?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa
View root = parent.getRootView();
root.dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_BACK));
root.dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_BACK));
callback.clicked(position);
}
}
});
return view;
}
public void setCurrentNameIcon(String name , int icon){
current_name = name;
current_icon = icon;
this.notifyDataSetChanged();
}
}
3) Listener interface:------------
public interface Listener {
public void clicked(int position);
}
4) main_activity.xml:---------
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent">
<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/sp">
</Spinner>
</android.support.constraint.ConstraintLayout>
5) my_object_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="60dp"
android:id="#+id/item"
android:clickable="true"
android:focusable="true">
<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:background="#drawable/icon"
android:layout_gravity="center_vertical"
android:id="#+id/icon"/>
<TextView
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_gravity="center_vertical"
android:gravity="center"
android:text="HI"
android:id="#+id/name"/>
</LinearLayout>

Custom GridView adapter changing image visibility status

I have a custom gridView and inside it i am having two images in a relative layout . Now when i click on the gridView i want the visibility of my images to change.
MainActivity:
public class Main extends Fragment implements OnMapReadyCallback{
GridView gridview,video_gridview;
GridViewAdapter adapter;
View v;
public Main() {
// Required empty public constructor
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
v = inflater.inflate(R.layout.activity_main,
container, false);
gridview = (GridView)v.findViewById(R.id.sub_notify_gridView);
gridview.setNumColumns(3);
adapter = new GridViewAdapter(getActivity(),FilePathStrings,
FileNameStrings,Image_Status,time);
gridview.setAdapter(adapter);
gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, final int
position, long id) {
//here i need to know which image has been clicked
}
});
return v;
}
MainActivity Layout:
<GridView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight=".35"
android:id="#+id/sub_notify_gridView">
<!--<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/gridViewImageid"/>-->
</GridView>
Cusotm GridView Adapter:
public class GridViewAdapter extends BaseAdapter {
// Declare variables
private Activity context;
private String[] filepath;
private String[] imageStatus;
private String Imgtime;
String[] separated;
private static LayoutInflater inflater = null;
public GridViewAdapter(FragmentActivity activity, String[] fpath, String[] fname, String[] image_status, String time) {
context = activity;
filepath = fpath;
imageStatus = image_status;
Imgtime = time;
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public int getCount() {
return filepath.length;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
View vi = convertView;
if (convertView == null)
vi = inflater.inflate(R.layout.gridview_item, null);
//TextView text = (TextView) vi.findViewById(R.id.text);
final ImageView image = (ImageView) vi.findViewById(R.id.image);
ImageView upload_image = (ImageView) vi.findViewById(R.id.upload_image);
TextView time = (TextView) vi.findViewById(R.id.textView);
time.setText(Imgtime);
image.setScaleType(ImageView.ScaleType.FIT_XY);
image.setPadding(5, 3, 5, 2); //itrb
//text.setText(filename[position]);
Bitmap ThumbImage =ThumbnailUtils.extractThumbnail
(BitmapFactory.decodeFile(filepath[position]),
128, 128);
if(imageStatus[position].equals("0")){
upload_image.setVisibility(View.VISIBLE);
}
if (filepath[position] == null){
image.setImageResource(R.drawable.imageicon);
}
else{
if (position > 4){
image.setImageBitmap(ThumbImage);
}
else{
image.setImageBitmap(ThumbImage);
}
}
return vi;
}
GridView_item Layout File:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:custom="http://schemas.android.com/tools"
android:padding="5dip">
<ImageView
android:id="#+id/image"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:layout_alignBottom="#+id/image"
android:layout_alignRight="#+id/image"
android:layout_alignEnd="#+id/image"
android:paddingRight="5dp"
android:paddingBottom="1.5dp"
android:id="#+id/textView" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingRight="5dp"
android:paddingBottom="1.5dp"
android:id="#+id/upload_image"
android:visibility="gone"
android:src="#drawable/accept_icon"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>

ListView set visible two buttons on long click

I have problem with ListView and ArrayAdapter. I want to show delete button on long press on ListView item. Problem is that I get more than once button showed on long click (depends of how many item i see on display. If I'm in portrait mode, I see, for example first 10 of 15, and when I press long on item at position 3, i will see two buttons, at position 3 and at position 13. If I'm at landscape mode, I see first 4 of 15 items, and when I press long at 2, I will see buttons at position 2, 6, 10, 14.). But, when I press other buttons, I still delete original item (item that long pressed). Here is example of my code.
TicketListAdapter
public class TicketListAdapter extends ArrayAdapter<Ticket> {
Context context;
int layoutResourceId;
TabFragmentAdapter tabMenager;
List<Ticket> data = null;
public TicketListAdapter(Context context, int layoutResourceId, List<Ticket> objects, TabFragmentAdapter tabMenager) {
super(context, layoutResourceId, objects);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.data = objects;
this.tabMenager = tabMenager;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
TicketHolder holder = null;
if (row == null) {
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new TicketHolder();
holder.imgIcon = (ImageView) row.findViewById(R.id.item_icon);
holder.txtTitle = (TextView) row.findViewById(R.id.name);
holder.txtGain = (TextView) row.findViewById(R.id.description);
holder.btnDelete = (Button) row.findViewById(R.id.deleteTicketButton);
holder.btnDelete.setOnClickListener(new DeleteTicketAction((Activity) context, getItemId(position), tabMenager, null, null));
row.setTag(holder);
} else {
holder = (TicketHolder) row.getTag();
}
Ticket ticket = data.get(position);
holder.txtTitle.setText(ticket.ticketName);
holder.txtGain.setText("Possible gain: " + ticket.possibleGain);
holder.imgIcon.setImageResource(StatusHelper.getStatusIconType(ticket.status.status, context));
return row;
}
static class TicketHolder {
ImageView imgIcon;
TextView txtTitle;
TextView txtGain;
Button btnDelete;
}
#Override
public long getItemId(int position) {
return data.get(position).getId();
}
list_ticket_view.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="#+id/item_icon"
android:layout_width="40dp"
android:layout_height="40dp"
android:contentDescription="#null"
android:src="#drawable/checkbox_marked_circle"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_margin="5dp" />
<TextView
android:id="#+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:gravity="center_vertical"
android:textColor="#color/colorPrimaryDark"
android:textStyle="bold"
android:layout_alignParentTop="true"
android:layout_toRightOf="#+id/item_icon"
android:layout_toEndOf="#+id/item_icon"
android:paddingLeft="5dp"
android:paddingTop="5dp"/>
<TextView android:id="#+id/description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/name"
android:layout_toRightOf="#+id/item_icon"
android:layout_toEndOf="#+id/item_icon"
android:paddingLeft="5dp"/>
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="40dp"
android:layout_height="40dp"
android:id="#+id/deleteTicketButton"
android:focusable="false"
android:visibility="gone"
android:layout_marginTop="5dp"
android:background="#drawable/delete_forever"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
</RelativeLayout>
ListFragment
public class AllTicketsFragment extends ListFragment implements OnItemClickListener,
AdapterView.OnItemLongClickListener, FragmentUpdateInterface {
TicketListAdapter arrayAdapter;
TabFragmentAdapter tabMenager;
public AllTicketsFragment() {
// Required empty public constructor
}
public AllTicketsFragment(TabFragmentAdapter tabManager) {
this.tabMenager = tabManager;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_list_view, container, false);
}
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
List<Ticket> tickets = null;
try {
tickets = new GetTicketFromDBTask(getActivity()).execute("all").get();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
arrayAdapter = new TicketListAdapter(getActivity(), R.layout.list_ticket_view, tickets, tabMenager);
setListAdapter(arrayAdapter);
getListView().addFooterView(getLayoutInflater(savedInstanceState).inflate(R.layout.list_footer_view, null), null, false);
getListView().setOnItemClickListener(this);
getListView().setOnItemLongClickListener(this);
}
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(getActivity(), TicketDetailActivity.class);
intent.putExtra("id", id);
startActivityForResult(intent, 1000);
}
#Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
Button deleteButton = (Button) view.findViewById(R.id.deleteTicketButton);
if (deleteButton.getVisibility() == View.VISIBLE) {
deleteButton.setVisibility(View.GONE);
} else {
deleteButton.setVisibility(View.VISIBLE);
}
arrayAdapter.notifyDataSetChanged();
return true;
}
}

Inflated Views inside a ListView are not visible

I'm trying to create a inflated dialog which contains dynamic amount of items.
These Items I inflate aswell.
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
int anzJobViews = getArguments().getInt("param");
View rootView = inflater.inflate(R.layout.dialog_job_chooser, container, false);
//getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
ListView listview = (ListView) rootView.findViewById(R.id.dialog_chooser_listview);
for (int i = 0; i < anzJobViews; i++) {
listview.addFooterView( new JobItemView(context));
Log.i("JobChooser","Item added");
}
return rootView;
}
The View (Contains some simple TextViews)
private void init() {
inflate(getContext(), R.layout.job_item_view, this);
}
When showing the dialog I see Nothing.
The xml layout for the dialog.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_centerHorizontal="true"
android:background="#drawable/round_corners_selected"
android:backgroundTint="#color/washedOutGreen">
<ListView
android:id="#+id/dialog_chooser_listview"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</ListView>
</LinearLayout>
The Items a want to add to the List.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#color/blue_focused">
<TextView
android:id="#+id/tv_jobname_area"
android:text="defaultName"
android:layout_margin="5dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/tv_jobvalue_area"
android:text="0 /h"
android:layout_margin="5dp"
android:layout_toRightOf="#+id/tv_jobname_area"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
Why don't use an adapter to populate your listview? An example from here: http://www.pcsalt.com/android/listview-using-baseadapter-android/
public MyBaseAdapter(Context context, ArrayList myList) {
this.myList = myList;
this.context = context;
inflater = LayoutInflater.from(this.context);
}
#Override
public int getCount() {
return myList.size();
}
#Override
public ListData getItem(int position) {
return myList.get(position);
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
MyViewHolder mViewHolder;
if (convertView == null) {
convertView = inflater.inflate(R.layout.layout_list_item, parent, false);
mViewHolder = new MyViewHolder(convertView);
convertView.setTag(mViewHolder);
} else {
mViewHolder = (MyViewHolder) convertView.getTag();
}
ListData currentListData = getItem(position);
mViewHolder.tvTitle.setText(currentListData.getTitle());
mViewHolder.tvDesc.setText(currentListData.getDescription());
mViewHolder.ivIcon.setImageResource(currentListData.getImgResId());
return convertView;
}
private class MyViewHolder {
TextView tvTitle, tvDesc;
ImageView ivIcon;
public MyViewHolder(View item) {
tvTitle = (TextView) item.findViewById(R.id.tvTitle);
tvDesc = (TextView) item.findViewById(R.id.tvDesc);
ivIcon = (ImageView) item.findViewById(R.id.ivIcon);
}
}
}
Also, if you are in a pre kitkat enviroment, you can only use addFooterView before setting the adapter.
Note: When first introduced, this method could only be called before setting the adapter with setAdapter(ListAdapter). Starting with KITKAT, this method may be called at any time. If the ListView's adapter does not extend HeaderViewListAdapter, it will be wrapped with a supporting instance of WrapperListAdapter.
Reference: http://developer.android.com/reference/android/widget/ListView.html#addFooterView(android.view.View)
use getView function to setup your listview
public View getView (int position, View convertView, ViewGroup list) {
View element;
if (convertView == null) {
element = View.inflate(context, R.layout.items, null);
}
else {
element = convertView;
}
//setup here something
//e.g.
TextView tv = (TextView) v.findViewById(R.id.tv);
tv.setText("myText");
return element;
}

How to drive gridview in Android?

I have written following to show my data into grid view. This is the procedure of my code.
In onCreate()
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_newslist);
// Initializing NewsAdapter in order to loading the list of NEWS items
newsAdapter = new NewsAdapter(this);
// Initializing grid view to show news
gridView = (GridView) findViewById(R.id.news_gridview);
gridView.setAdapter(newsAdapter);
gridView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
Toast.makeText(NewsList.this, "" + position, Toast.LENGTH_SHORT).show();
}
});
Then in onStart() I'll get XML data from server then parse it and final parsed data will be stored in "parsedList" (100% I have data in this variable because I saw it in logcat).
#Override
protected void onStart() {
super.onStart();
String strNewsContent = "***";
...
newsAdapter.setData(parsedList);
newsAdapter.notifyDataSetChanged();
}
Finally this is my "NewsAdapter":
public class NewsAdapter extends BaseAdapter {
private LayoutInflater myInflater;
private NewsFeedItemList itemList;
public NewsAdapter(Context c) {
myInflater = LayoutInflater.from(c);
}
public void setData(NewsFeedItemList newsFeedItemList){
itemList = newsFeedItemList;
Log.i("NewsAdapter", "Parsed list passed to the adapter.");
}
#Override
public int getCount() {
return 0;
}
#Override
public Object getItem(int position) {
return position;
}
#Override
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent){
ViewHolder holder;
if (convertView == null) {
convertView = myInflater.inflate(R.layout.grid_news, null);
holder = new ViewHolder();
holder.ivIcon = (TextView) convertView.findViewById(R.id.news_icon);
holder.tvLabel = (TextView) convertView.findViewById(R.id.news_label);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.ivIcon.setText(itemList.getImage().get(position));
holder.tvLabel.setText(itemList.getTitle().get(position));
Log.i("Position is>>>>:", Integer.toString(position));
return convertView;
}
static class ViewHolder {
TextView ivIcon;
TextView tvLabel;
}
}
The format of "grid_news" is like this
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/news_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="#string/menu_contentdescription"
android:layout_centerHorizontal="true" />
<TextView
android:id="#+id/news_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:textColor="#ffffff"
android:textSize="15sp"
android:textStyle="bold"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:paddingBottom="10dip" />
</RelativeLayout>
I have no idea why I can't see the result in my emulator/device.
I think it is because of getCount() returning 0; shouldn't it be itemList.size()? in news NewsAdapter.

Categories

Resources