I'm pulling data and loading it into a BaseAdapter for my List View. Currently, the items in the listview are the same as the last item added, so the list looks like this:
Item 3
Item 3
Item 3
Instead of:
Item 1
Item 2
Item 3
I've checked over the data sources, and the data that's loaded from the source is unique each time. I think there's somehting wrong with my BaseAdapter, but I'm not sure what it is. Here's my BaseAdapter:
BaseAdapter.java
ArrayList<ThreadListData> myList = new ArrayList<>();
LayoutInflater inflater;
Context context;
public ThreadBaseAdapter(Context context, ArrayList<ThreadListData> myList) {
this.context = context;
inflater = LayoutInflater.from(this.context);
this.myList = myList;
}
#Override
public int getCount() {
return myList.size();
}
#Override
public ThreadListData 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) {
if (convertView == null) {
LayoutInflater inflater = LayoutInflater.from(this.context);
convertView = inflater.inflate(R.layout.listitem_newthread, parent, false);
}
ThreadListData listData = getItem(position);
TextView name = (TextView)convertView.findViewById(R.id.name_newthread);
TextView username = (TextView)convertView.findViewById(R.id.username_newthread);
name.setText(listData.getName());
username.setText(listData.getName());
ParseImageView profile = (ParseImageView)convertView.findViewById(R.id.profilepicture);
BitmapTask task = new BitmapTask(listData.getImage());
task.execute(profile);
return convertView;
}
I'll start with my BaseAdapter, and can show more code if requested. All Help Is Appreciated!
You missed to return the position
Modifiy your getItemId as
#Override
public long getItemId(int position) {
return position;
}
There are 2 things you need to change:
1. You return wrong item position
#Override
public long getItemId(int position) {
return 0;
}
It should be
#Override
public long getItemId(int position) {
return position;
}
2. Your declared a LayoutInflater and create new instance in constructor. But in the getView(), you create another LayoutInflater. Look at updated code below
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
// LayoutInflater inflater = LayoutInflater.from(this.context);
inflater = LayoutInflater.from(this.context);
convertView = inflater.inflate(R.layout.listitem_newthread, parent, false);
}
ThreadListData listData = getItem(position);
TextView name = (TextView)convertView.findViewById(R.id.name_newthread);
TextView username = (TextView)convertView.findViewById(R.id.username_newthread);
name.setText(listData.getName());
username.setText(listData.getName());
ParseImageView profile = (ParseImageView)convertView.findViewById(R.id.profilepicture);
BitmapTask task = new BitmapTask(listData.getImage());
task.execute(profile);
return convertView;
}
Related
I'm trying to make time table application. my layout is gridview.
My layout is now like this : https://user-images.githubusercontent.com/37032956/57445643-c2765180-728d-11e9-9f1a-52e2d93f7e9e.JPG
I can get data and draw in a line.
But i don't know how to locate this cells in particular location.
I want to place this 4 colored text in exact location using day, start time, end time.
This is my code.
public class GridviewAdapter extends BaseAdapter {
private ArrayList<GridViewItem> listData;
LayoutInflater layoutInflater;
public GridviewAdapter (Context context, ArrayList<GridViewItem> listData){
this.listData = listData;
layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
return listData.size();
}
#Override
public Object getItem(int position) {
return listData.get(position); // 수정
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
GridViewHolder gridViewHolder;
if(convertView == null){
convertView = layoutInflater.inflate(R.layout.gridview_item, (ViewGroup) convertView, false);
gridViewHolder = new GridViewHolder();
gridViewHolder.subjectTitle=(TextView) convertView.findViewById(R.id.item_title);
convertView.setTag(gridViewHolder);
} else {
gridViewHolder = (GridViewHolder) convertView.getTag();
}
gridViewHolder.subjectTitle.setText(listData.get(position).getSub_title());
return convertView;
}
static class GridViewHolder{
TextView subjectTitle;
// day, start_time, end_time;
}
}
Here is my grid view adapter.
You can use OnItemClickListener which correspond to the item position.
gridView.setOnItemClickListener(new OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3)
{
//Selected item is at index "position".
}
});
Hey i'm using gridView to show my items. Everything is ok but after six items or scroll some items get repeated.
So i think i have something to do in case the convertItem is not null. But how i load next items
Some short code:
public CustomGridAdapter(Context context, ArrayList<Event> finalResult, double latitude, double longitude) {
this.posLatitude = latitude;
this.posLongitude = longitude;
this.context = context;
this.gridValues = finalResult;
this.app = new Application(context);
}
#Override
public int getCount() {
return gridValues.size();
}
#Override
public Object getItem(int position) {
return null;
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// LayoutInflator to call external grid_item.xml file
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View gridView;
if (convertView == null) {
gridView = new View(context);
gridView = inflater.inflate(R.layout.grid_item, null);
event = gridValues.get(position);
} else {
gridView = convertView;
}
return gridView;
}
ConvertView is by definition being reused. Make sure to change the values of the view item inflating or setting it.
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// LayoutInflator to call external grid_item.xml file
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View gridView;
if (convertView == null) {
gridView = inflater.inflate(R.layout.grid_item, null);
} else {
gridView = convertView;
}
event = gridValues.get(position);
// TODO: set view values based on this event
return gridView;
}
Whats wrong with this adapter, when i scroll down i see repeated rows at the bottom and then when scroll up again i see also repeated rows at the top that were not exist before, and the rest of Data items does not appear
the adapter:
public class ClassesListViewAdapter extends BaseAdapter {
private Context mContext;
ArrayList<String> Data = new ArrayList<>();
public ClassesListViewAdapter(Context context, ArrayList<String> data) {
Data = data;
mContext = context;
}
#Override
public Object getItem(int position) {
return position;
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public int getCount() {
return Data.size();
}
private class ViewHolder{
TextView ClassDataTV;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = ((Activity)mContext).getLayoutInflater();
ViewHolder holder;
if (convertView == null) {
convertView = inflater.inflate(R.layout.classes_list_view_item, parent, false);
holder = new ViewHolder();
holder.ClassDataTV = (TextView) convertView.findViewById(R.id.ClassDataTV);
holder.ClassDataTV.setText(Data.get(position));
convertView.setTag(holder);
}else{
holder=(ViewHolder)convertView.getTag();
}
return convertView;
}
}
and here how i use it:
ArrayList<String> v = new ArrayList<>();
v.add("AAAAAAA");
v.add("WWWWWwW");
v.add("VVVVVVV");
v.add("SSSSSSSSS");
v.add("QQQQQQQQQ");
v.add("YYYYYYYY");
v.add("TTTTTTT");
v.add("UUUUUUUUUU");
v.add("zzzzzzzzzzzz");
v.add("CCCCCCCCCC");
v.add("HHHHHHHHHHH");
v.add("IIIIIIIIII");
v.add("PPPPPPPPP");
mListView.setAdapter(new ClassesListViewAdapter(getActivity(), v));
Put following part of your code outside if-block and it will be fixed :
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = ((Activity)mContext).getLayoutInflater();
ViewHolder holder;
if (convertView == null) {
convertView = inflater.inflate(R.layout.classes_list_view_item, parent, false);
holder = new ViewHolder();
holder.ClassDataTV = (TextView) convertView.findViewById(R.id.ClassDataTV);
convertView.setTag(holder);
}else{
holder=(ViewHolder)convertView.getTag();
}
// initialize your view here
holder.ClassDataTV.setText(Data.get(position));
return convertView;
}
The logic behind ViewHolder pattern tells that you should do it in this way. when you scroll some of reference will not created again and else block called so this cause your list not updated as you expected.
I want to add section headers in the list view in android like the attached image. Can some one help me with the code. I don't want to implement the side section indexer like in contacts.image
This is the custim list adapter that I have created-
public class CustomListAdapter extends BaseAdapter {
private Activity mContext;
private List<String> mList;
private LayoutInflater mLayoutInflater = null;
public CustomListAdapter(Activity context, List<String> list){
mContext = context;
mList = list;
mLayoutInflater = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
return mList.size();
}
#Override
public Object getItem(int pos) {
return mList.get(pos);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
return super.getDropDownView(position, convertView, parent);
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
final ListViewHolder viewHolder;
if (convertView == null) {
LayoutInflater layoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.players_name_with_options, parent, false);
viewHolder = new ListViewHolder(convertView);
convertView.setTag(viewHolder);
} else {
viewHolder = (ListViewHolder) convertView.getTag();
}
// Setting the name of the player on the text view
viewHolder.textView.setText(mList.get(position));
return convertView;
}
// Class that contains the views as the variables
private class ListViewHolder {
TextView textView;
// Constructor of the class
public ListViewHolder(View item) {
// Initialising the text view and the spinner
textView = (TextView) item.findViewById(R.id.list_textView);
}
}
}
I am initializing this ArrayAdapter and then adding the names of the players. I want that when ever the name is added then the section header be created and the player's name should be set below the header like the image that I had attached.
There are many library available for this. Hope the below link will help you to figure out, what you need.
pinned-section-listview
Example 1
Example 2
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.