OnItemClickListener not working with ListView - android

I have a listView and I am trying to make the items on the listview clickable, so I can go into a details fragment, but for some reason it never goes into the onItemClick method.
I dont have any buttons or similar components on my row, only a couple of custom textviews which extend AppCompatTextView.
`listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getContext(), "dwdwdwdw", Toast.LENGTH_SHORT).show();
selectRow(position);
}
});
listView.setOnScrollListener(new AbsListView.OnScrollListener() {.....`
my fragment_listview:
`<?xml version="1.0" encoding="utf-8"?>
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/swipeRefreshLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/background">
<ListView
android:id="#+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:divider="#null" />
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>`
my row
`<?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="wrap_content"
android:background="#drawable/listview_background"
android:padding="10dp">
<....MainFontTextView
android:id="#+id/nameTextView"
style="#style/black_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:singleLine="true"
android:text="New Text" />
+ 4 more textviews`
adapter code:
public class BasePagingAdapter extends ArrayAdapter<Object> {
protected final static int EMPTY_TYPE = 0;
protected final static int LOADING_TYPE = 1;
protected final static int ITEM_TYPE = 2;
protected ArrayList<?> items;
protected boolean showLoadingRow;
public ArrayList<?> getItems() {
return items;
}
public boolean isShowLoadingRow() {
return showLoadingRow;
}
public void setShowLoadingRow(boolean showLoadingRow) {
this.showLoadingRow = showLoadingRow;
}
public BasePagingAdapter(Context context, int resource, ArrayList<?> items) {
super(context, resource);
this.items = items;
}
protected static class EmptyViewHolder {
TextView emptyTextView;
}
protected static class LoadingViewHolder {
TextView loadingTextView;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
if (getItemViewType(position) == EMPTY_TYPE) {
EmptyViewHolder holder;
if (row == null || !(row.getTag() instanceof EmptyViewHolder)) {
LayoutInflater inflater = LayoutInflater.from(getContext());
row = inflater.inflate(R.layout.row_empty, parent, false);
holder = new EmptyViewHolder();
holder.emptyTextView = row.findViewById(R.id.emptyTextView);
row.setTag(holder);
}
} else if (getItemViewType(position) == LOADING_TYPE) {
LoadingViewHolder holder;
if (row == null || !(row.getTag() instanceof LoadingViewHolder)) {
LayoutInflater inflater = LayoutInflater.from(getContext());
row = inflater.inflate(R.layout.row_loading, parent, false);
holder = new LoadingViewHolder();
holder.loadingTextView = row.findViewById(R.id.loadingTextView);
row.setTag(holder);
}
holder = (LoadingViewHolder) row.getTag();
holder.loadingTextView.setText(String.format(getContext().getString(R.string.list_loading_more), Constants.PAGE_SIZE));
}
return row;
}
#Override
public int getCount() {
if (items.size() > 0) {
if (showLoadingRow) {
return items.size() + 1;
} else {
return items.size();
}
} else {
return 1;
}
}
#Override
public int getViewTypeCount() {
return 3;
}
#Override
public int getItemViewType(int position) {
if (items.size() == 0) {
return EMPTY_TYPE;
} else if (items.size() == position) {
return LOADING_TYPE;
} else {
return ITEM_TYPE;
}
}
#Override
public boolean isEnabled(int position) {
return items.size() != 0 && position != items.size();
}
public void updateItems(ArrayList<?> items) {
this.items = items;
notifyDataSetChanged();
}
}

just apply clicklistner inside "getView".
public class BasePagingAdapter extends ArrayAdapter<Object> {
protected final static int EMPTY_TYPE = 0;
protected final static int LOADING_TYPE = 1;
protected final static int ITEM_TYPE = 2;
protected ArrayList<?> items;
protected boolean showLoadingRow;
public ArrayList<?> getItems() {
return items;
}
public boolean isShowLoadingRow() {
return showLoadingRow;
}
public void setShowLoadingRow(boolean showLoadingRow) {
this.showLoadingRow = showLoadingRow;
}
public BasePagingAdapter(Context context, int resource, ArrayList<?> items) {
super(context, resource);
this.items = items;
}
protected static class EmptyViewHolder {
TextView emptyTextView;
}
protected static class LoadingViewHolder {
TextView loadingTextView;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
EmptyViewHolder holder = new EmptyViewHolder();
holder.yourView.setonClickListner(new onClickListner){
/// put your Code here. ....
}
if (getItemViewType(position) == EMPTY_TYPE) {
if (row == null || !(row.getTag() instanceof EmptyViewHolder)) {
LayoutInflater inflater = LayoutInflater.from(getContext());
row = inflater.inflate(R.layout.row_empty, parent, false);
holder.emptyTextView = row.findViewById(R.id.emptyTextView);
row.setTag(holder);
}
} else if (getItemViewType(position) == LOADING_TYPE) {
LoadingViewHolder holder;
if (row == null || !(row.getTag() instanceof LoadingViewHolder)) {
LayoutInflater inflater = LayoutInflater.from(getContext());
row = inflater.inflate(R.layout.row_loading, parent, false);
holder = new LoadingViewHolder();
holder.loadingTextView = row.findViewById(R.id.loadingTextView);
row.setTag(holder);
}
holder = (LoadingViewHolder) row.getTag();
holder.loadingTextView.setText(String.format(getContext().getString(R.string.list_loading_more), Constants.PAGE_SIZE));
}
return row;
}
#Override
public int getCount() {
if (items.size() > 0) {
if (showLoadingRow) {
return items.size() + 1;
} else {
return items.size();
}
} else {
return 1;
}
}
#Override
public int getViewTypeCount() {
return 3;
}
#Override
public int getItemViewType(int position) {
if (items.size() == 0) {
return EMPTY_TYPE;
} else if (items.size() == position) {
return LOADING_TYPE;
} else {
return ITEM_TYPE;
}
}
#Override
public boolean isEnabled(int position) {
return items.size() != 0 && position != items.size();
}
public void updateItems(ArrayList<?> items) {
this.items = items;
notifyDataSetChanged();
}

I think instead on setting onclicklistener on the listview you should set onclicklistener on the relativeLayout of the my row.xml by assigning it an id first

Related

Select Check Boxes in ListView[( Select multiple checkbox but one by one), Select All at once, Select only one at a time]

I have to Select Check boxes in ListView. Right now when I am selecting the checkbox, some check boxes below got checked automatically.
This is my code:
StudentAdapter.java:
public class StudentAdapter extends BaseAdapter{
private Activity activity;
private List<StudentBean> studentBeanList;
private boolean checked = false;
CheckBox checkBox;
public StudentAdapter(Activity activity, List<StudentBean> studentBeanList){
super();
this.studentBeanList = studentBeanList;
this.activity = activity;
}
#Override
public int getCount() {
return studentBeanList.size();
}
#Override
public Object getItem(int position) {
return studentBeanList.get(position);
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
StudentAdapter.ItemHolder itemHolder= new StudentAdapter.ItemHolder();
if (convertView==null) {
LayoutInflater li = (LayoutInflater)
(activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE));
convertView = li.inflate(
R.layout.row_student, null);
itemHolder.textViewStudent = (TextView) convertView
.findViewById(R.id.tvstudentname);
convertView.setTag(itemHolder);
itemHolder.checkBox = (CheckBox) convertView.findViewById(R.id.checkBox1);
convertView.setTag(itemHolder);
}
else
{
itemHolder = (StudentAdapter.ItemHolder) convertView.getTag();
}
if (studentBeanList != null && !studentBeanList.isEmpty())
{
final StudentBean studentBean = studentBeanList.get(position);
if (studentBean != null) {
if
(itemHolder.textViewStudent != null && studentBean.getStudentname() != null)
{
itemHolder.textViewStudent.setText(studentBean.getStudentname());
}
}
}
return convertView;
}
private class ItemHolder {
TextView textViewStudent;
CheckBox checkBox;
}
#Override
public void notifyDataSetChanged() {
super.notifyDataSetChanged();
// Your code to nofify
}
}
I am inflating below XML..
row_student.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/tvstudentname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:padding="10dp"
android:layout_alignBaseline="#+id/checkBox1"
android:layout_alignBottom="#+id/checkBox1"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_toLeftOf="#+id/checkBox1"
android:layout_toStartOf="#+id/checkBox1" />
<CheckBox
android:id="#+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
</RelativeLayout>
I am helpless what to do.
How can I get Select All, Select only one (I mean when user select one, other dis selects auto), Select All but one by one. Thanks in Advance..
This Is new Code
public class StudentAdapter extends BaseAdapter{
private Activity activity;
private StudentBean studentBean;
private List<StudentBean> studentBeanList;
public StudentAdapter(Activity activity, List<StudentBean> studentBeanList){
super();
this.studentBeanList = studentBeanList;
this.activity = activity;
}
#Override
public int getCount() {
return studentBeanList.size();
}
#Override
public Object getItem(int position) {
return studentBeanList.get(position);
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
StudentAdapter.ItemHolder itemHolder= new StudentAdapter.ItemHolder();
if (convertView==null) {
LayoutInflater li = (LayoutInflater)
(activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE));
convertView = li.inflate(
R.layout.row_student, null);
itemHolder.textViewStudent = (TextView) convertView
.findViewById(R.id.tvstudentname);
convertView.setTag(itemHolder);
itemHolder.checkBox = (CheckBox) convertView.findViewById(R.id.checkBox1);
convertView.setTag(itemHolder);
itemHolder.checkBox.setChecked(studentBean.isChecked());
itemHolder.checkBox.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
studentBean.setChecked(holder.checkBox.isChecked());
}
});
}
else
{
itemHolder = (StudentAdapter.ItemHolder) convertView.getTag();
}
if (studentBeanList != null && !studentBeanList.isEmpty())
{
final StudentBean studentBean = studentBeanList.get(position);
if (studentBean != null) {
if
(itemHolder.textViewStudent != null && studentBean.getStudentname() != null)
{
itemHolder.textViewStudent.setText(studentBean.getStudentname());
}
}
}
return convertView;
}
private class ItemHolder {
TextView textViewStudent;
CheckBox checkBox;
}
#Override
public void notifyDataSetChanged() {
super.notifyDataSetChanged();
// Your code to nofify
}
}
for this you add one Boolean field in StudentBean pojo class and make getter setter for that default Boolean is false.
private boolean checked = false;
public boolean isChecked() {
return checked;
}
public void setChecked(boolean checked) {
this.checked = checked;
}
now add below code in your adapter
itemHolder.checkBox.setChecked(studentBean.isChecked());
itemHolder.checkBox.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
studentBean.setChecked(holder.checkBox.isChecked());
}
});
it's work fine and proper.
Try this:
public class StudentAdapter extends BaseAdapter{
private Activity activity;
private boolean multiSelection = true;
private int selectedPosition = -1;
private List<StudentBean> studentBeanList;
public StudentAdapter(Activity activity, List<StudentBean> studentBeanList){
this.studentBeanList = studentBeanList;
this.activity = activity;
}
#Override
public int getCount() {
return studentBeanList.size();
}
#Override
public Object getItem(int position) {
return studentBeanList.get(position);
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ItemHolder itemHolder;
if (convertView == null) {
LayoutInflater li = (LayoutInflater)
(activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE));
convertView = li.inflate(R.layout.row_student, null);
itemHolder = new StudentAdapter.ItemHolder();
itemHolder.textViewStudent = (TextView) convertView.findViewById(R.id.tvstudentname);
itemHolder.checkBox = (CheckBox) convertView.findViewById(R.id.checkBox1);
convertView.setTag(itemHolder);
} else {
itemHolder = (StudentAdapter.ItemHolder) convertView.getTag();
}
itemHolder.checkBox.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
CheckBox cb = (CheckBox) view;
int pos = (Integer) cb.getTag();
StudentBean studentBean = studentBeanList.get(pos);
studentBean.setChecked(cb.isChecked());
if(!multiSelection && selectedPosition != -1){
StudentBean previousStudentBean = studentBeanList.get(selectedPosition);
previousStudentBean.setChecked(false);
notifyDataSetChanged();
selectedPosition = pos;
}
}
});
if (studentBeanList != null && !studentBeanList.isEmpty()) {
StudentBean studentBean = studentBeanList.get(position);
if (studentBean != null) {
if (itemHolder.textViewStudent != null && studentBean.getStudentname() != null) {
itemHolder.textViewStudent.setText(studentBean.getStudentname());
itemHolder.checkBox.setChecked(studentBean.isChecked());
}
}
}
itemHolder.checkBox.setTag(position);
return convertView;
}
private class ItemHolder {
TextView textViewStudent;
CheckBox checkBox;
}
#Override
public void notifyDataSetChanged() {
super.notifyDataSetChanged();
// Your code to notify
}
public void selectAll() {
multiSelection = true;
for(int i=0; i<studentBeanList.size(); i++){
studentBeanList.get(i).setChecked(true);
}
notifyDataSetChanged();
}
public void changeSelectMode(boolean enableMultiSelection){
multiSelection = enableMultiSelection;
}
}
You need to have something in your Activity/Fragment for "Select All" and "Change Select Mode". I have a blog about ListView, http://programandroidlistview.blogspot.com/
Hope this help!

Replace Recyclerview with LinearLayout

i am having a small problem with memory when i tried to used nested RecyclerView and it crashed because it was not recycling views and as a result it run out of memory. So i have to go with another solution. I have created my own layout which accepts a Adapter like this:
public class CustomTileLayout extends LinearLayout {
private Adapter mAdapter;
private Observer mObserver = new Observer();
public CustomTileLayout(Context context) {
super(context);
}
public CustomTileLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
public Adapter getAdapter() {
return mAdapter;
}
public void setAdapter(Adapter mAdapter) {
if (mAdapter != null)
mAdapter.unregisterDataSetObserver(mObserver);
this.mAdapter = mAdapter;
mAdapter.registerDataSetObserver(mObserver);
mObserver.onChanged();
}
private class Observer extends DataSetObserver {
#Override
public void onChanged() {
final List<View> oldViews = new ArrayList<View>(getChildCount());
for (int i = 0; i < getChildCount(); i++)
oldViews.add(getChildAt(i));
final Iterator<View> iter = oldViews.iterator();
removeAllViews();
for (int i = 0; i < mAdapter.getCount(); i++) {
View convertView = iter.hasNext() ? iter.next() : null;
View newView = mAdapter.getView(i, convertView, CustomTileLayout.this);
addView(newView);
}
super.onChanged();
}
#Override
public void onInvalidated() {
removeAllViews();
super.onInvalidated();
}
}
}
and while i use it like:
tileHolder = (GameCategoriesTileViewHolder) holder;
section = (Section) items.get(position);
tileHolder.customTileLayout.setAdapter(new GameTileAdapter(section.getGames(), context, R.layout.row_small_tile));
Everything works fine so far but there is a problem. The problem is that it looks like this:
while i want it each row to have 3 tiles like this:
If i specify the child layout to be like the result since it is the same item it only populates the 1st tile and the next 2 are blank.
My adapter:
public class GameTileAdapter implements ListAdapter {
private List<Game> games;
private Context context;
private int resLayout = R.layout.row_small_tile;
private boolean isFavoriteFragmentView = false;
private PopupMenu popupMenu;
public GameTileAdapter(List<Game> games, Context context, int resLayout) {
this.games = games;
this.context = context;
this.resLayout = resLayout;
}
#Override
public void registerDataSetObserver(DataSetObserver observer) {
}
#Override
public void unregisterDataSetObserver(DataSetObserver observer) {
}
#Override
public int getCount() {
return games.size();
}
#Override
public Object getItem(int position) {
return games.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public boolean hasStableIds() {
return true;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View itemView = convertView;
SingleItemViewHolder holder;
if (itemView == null){
LayoutInflater vi;
vi = LayoutInflater.from(context);
itemView = vi.inflate(resLayout, parent, false);
holder = new SingleItemViewHolder(itemView);
holder.game_name.setText(games.get(position).getName());
if (games.get(position).getImageUri().isEmpty()){
holder.game_imageuri.setImageResource(R.drawable.ic_image_placeholder);
}else {
Picasso.with(context).load(games.get(position).getImageUri()).into(holder.game_imageuri);
}
holder.itemLineColor.setBackgroundColor(GameManager.getmInstance(context).getGameColor(games.get(position)));
}else {
holder = (SingleItemViewHolder)itemView.getTag();
}
return itemView;
}
#Override
public boolean areAllItemsEnabled() {
return true;
}
#Override
public boolean isEnabled(int position) {
return true;
}
private static class SingleItemViewHolder{
CardView cv;
TextView game_name;
TextView game_is_new;
TextView game_jackpot;
ImageView game_imageuri;
View itemLineColor;
Button favorite_button;
Button menu_button;
SingleItemViewHolder(View itemView) {
cv = (CardView)itemView.findViewById(R.id.root_layout);
game_name = (TextView)itemView.findViewById(R.id.game_name_text);
game_is_new = (TextView)itemView.findViewById(R.id.game_new_ribbon);
game_jackpot = (TextView)itemView.findViewById(R.id.game_jackpot_ribbon);
game_imageuri = (ImageView)itemView.findViewById(R.id.game_imageuri);
itemLineColor = (View)itemView.findViewById(R.id.single_line_color);
favorite_button = (Button)itemView.findViewById(R.id.favButton);
menu_button = (Button)itemView.findViewById(R.id.menu_button_tile);
}
}
#Override
public int getItemViewType(int position) {
return position;
}
#Override
public int getViewTypeCount() {
return games.size();
}
#Override
public boolean isEmpty() {
return games.isEmpty();
}
}
Any ideas of how i can get the result?
You can use a layout for inner RecyclerView and add inflate a layout dynamically.
#Override
public void onBindViewHolder(RecentErrorsAdapter.ViewHolder holder, int position) {
try {
ArrayList<String> innerArrayList = new ArrayList<String>();
innerArrayList = mDataset.get(position).getInnerDataList();
for (int i = 0; i < innerArrayList.size(); i++) {
LayoutInflater inflater = null;
inflater = (LayoutInflater) context.getApplicationContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View mLinearView = inflater.inflate(R.layout.your_layout, null);
/**
* getting id of row.xml
*/
TextView textName = (TextView) mLinearView
.findViewById(R.id.txt_name);
textName.setText(innerArrayList.get(i));
holder.lstProgramName.addView(mLinearView);
}
} catch (Exception e) {
e.printStackTrace();
}
}

How to get selected item from GridView and show in ListView

I have GridView which contain the number of items, If user click on save button then selected value show in ListView, I tried but not got succeied, Can any one help to solve this problem?
MainActivity.java
fillDataMyProperty();
if (myPropertyArrayList != null && myPropertyArrayList.size() > 0) {
myPropertListAdapter = new MyPropertListAdapter(MainActivity.this, myPropertyArrayList, txtSubmit);
gridMyProperty.setAdapter(myPropertListAdapter);
}
My GridAdapter is
public class MyPropertListAdapter extends BaseAdapter {
private Context context;
private ArrayList<ItemModel> arrayList;
private boolean check = false;
private View view;
private TextView txtSubmit;
public MyPropertListAdapter(Context context, ArrayList<ItemModel> arrayList, TextView txtSubmit) {
this.context = context;
this.arrayList = arrayList;
this.txtSubmit = txtSubmit;
}
#Override
public int getCount() {
return arrayList.size();
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
#Override
public int getViewTypeCount() {
return super.getViewTypeCount();
}
#SuppressLint("InflateParams")
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final ViewHolder viewHolder;
if (convertView == null) {
convertView = inflater.inflate(R.layout.grid_item_row, null);
viewHolder = new ViewHolder();
viewHolder.txtItem = (TextView) convertView.findViewById(R.id.txtItem);
viewHolder.rlMain = (RelativeLayout) convertView.findViewById(R.id.rlMain);
viewHolder.imgCheck = (ImageView) convertView.findViewById(R.id.imgCheck);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.txtItem.setText(arrayList.get(position).getItem_name());
if (arrayList.get(position).isCheck() == true) {
viewHolder.txtItem.setBackgroundResource(R.drawable.ic_green_facility);
Glide.with(context).load(R.drawable.ic_facility_check).placeholder(R.mipmap.ic_launcher).error(R.mipmap.ic_launcher).dontAnimate().into(viewHolder.imgCheck);
} else if (arrayList.get(position).isCheck() == false) {
viewHolder.txtItem.setBackgroundResource(R.drawable.ic_white_facility);
viewHolder.imgCheck.setVisibility(View.GONE);
}
viewHolder.rlMain.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
view = v;
if (arrayList.get(position).isCheck() == false) {
Log.d("if---------------------", String.valueOf(arrayList.get(position).isCheck()));
viewHolder.txtItem.setBackgroundResource(R.drawable.ic_green_facility);
viewHolder.imgCheck.setVisibility(View.VISIBLE);
Glide.with(context).load(R.drawable.ic_facility_check).placeholder(R.mipmap.ic_launcher).error(R.mipmap.ic_launcher).dontAnimate().into(viewHolder.imgCheck);
arrayList.get(position).setCheck(true);
check = true;
} else if (arrayList.get(position).isCheck() == true) {
Log.d("else-------------------", String.valueOf(arrayList.get(position).isCheck()));
viewHolder.txtItem.setBackgroundResource(R.drawable.ic_white_facility);
viewHolder.imgCheck.setVisibility(View.GONE);
arrayList.get(position).setCheck(false);
check = false;
}
}
});
txtSubmit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
showItemsDilaog();
}
});
return convertView;
}
public class ViewHolder {
TextView txtItem;
RelativeLayout rlMain;
ImageView imgCheck;
}
private void showItemsDilaog() {
final Dialog mDialog = new Dialog(context);
mDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
mDialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
mDialog.setContentView(R.layout.dialog_list_item);
ImageView imgCancel = (ImageView) mDialog.findViewById(R.id.imgCancel);
ListView listItem = (ListView) mDialog.findViewById(R.id.listItem);
if (arrayList != null && arrayList.size() > 0) {
ItemsAdapter mAdapter = new ItemsAdapter(context, arrayList);
listItem.setAdapter(mAdapter);
}
imgCancel.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mDialog.dismiss();
}
});
mDialog.show();
}}
ItemsAdapter.Java
class ItemsAdapter extends BaseAdapter {
// private String[] mStrings = new String[]{"Elevator", "Air Conditionor", "Electricity", "Houe Keeping", "Garden", "All"};
private ViewHolder viewHolder;
private Context context;
private ArrayList<ItemModel> mArrayList;
public ItemsAdapter(Context context, ArrayList<ItemModel> maintananceModelArrayList) {
this.context = context;
this.mArrayList = maintananceModelArrayList;
}
#Override
public int getCount() {
return mArrayList.size();
}
#Override
public Object getItem(int position) {
return mArrayList.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = null;
if (convertView == null) {
convertView = inflater.inflate(R.layout.item_row, null);
viewHolder = new ViewHolder();
viewHolder.txtItem = (TextView) convertView.findViewById(R.id.txtItem);
viewHolder.lineSeperator = (View) convertView.findViewById(R.id.lineSeperator);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
if (arrayList.get(position).isCheck() == true) {
viewHolder.txtItem.setText(arrayList.get(position).getItem_name());
Log.d("Items:::::::::::::::",arrayList.get(position).getItem_name());
}
if (position == arrayList.size() - 1) {
viewHolder.lineSeperator.setVisibility(View.GONE);
} else {
viewHolder.lineSeperator.setVisibility(View.VISIBLE);
}
return convertView;
}
public class ViewHolder {
TextView txtItem;
View lineSeperator;
}
}

How to avoid default text issue in Custom Spinner Android?

i have done custom spinner with custom adapter in it but getting some default text everytime how to avoid this issue everytime it should only display 0th position text on spinner:
CourseAdapter courseAdapter = new CourseAdapter(mContext);
customViewHolder.spinner.setAdapter(courseAdapter);
while CourseAdapter.java
public class CourseAdapter extends BaseAdapter {
public static final String TAG=CourseAdapter.class.getName();
List<String> courses =new ArrayList<>(); // code to get the courses ArrayList
Context context;
private void fillCourses()
{
for(int i =0;i<10;i++)
{
if(i == 0)
{
courses.add("Choose");
}
else
{
courses.add("courses:"+i);
}
}
}
public CourseAdapter(Context context)
{
this.context = context;
fillCourses();
}
#Override
public int getCount() {
return courses.size();
}
#Override
public Object getItem(int position) {
return courses.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolderFilterItem viewHolder;
if (convertView == null) {
Log.d(TAG, "fillCourses size:" + courses.size());
convertView = LayoutInflater.from(context)
.inflate(R.layout.layout_customspinner, parent, false);
viewHolder = new ViewHolderFilterItem();
viewHolder.textView = (TextView) convertView.findViewById(R.id.txtViewFilterItem);
viewHolder.checkBox = (CheckBox)convertView.findViewById(R.id.checkboxFilterItem);
//store the holder with the view.
convertView.setTag(viewHolder);
return convertView;
}
else
{
Log.d(TAG, "convertview != null:" + courses.size());
viewHolder = (ViewHolderFilterItem) convertView.getTag();
}
if(courses != null && courses.size() > 0) {
String itemText = courses.get(position);
if (itemText != null) {
viewHolder.textView.setText(itemText);
viewHolder.checkBox.setChecked(true);
}
if(position == 0)
{
viewHolder.checkBox.setVisibility(View.GONE);
}
}
return convertView;
}
private class ViewHolderFilterItem{
TextView textView;
CheckBox checkBox;}
}[![[why Filter item text with checkbox appearing ?][1]][1]
you have to use courseAdapter.notifyDataSetChanged() after setting customViewHolder.spinner.setAdapter(courseAdapter);
Hope this helps.

Accelarate listview scrolling

I'm trying to scroll my simple listview, but unfortunately it's not scrolling smoothly.
I can't figure out what seem to be the problem. I'm not doing anything fancy.
public class ArticleRowAdapter extends BaseAdapter {
private static final int TYPE_MAINARTICLE = 0;
private static final int TYPE_ARTICLE = 1;
private static final int TYPE_MAX_COUNT = TYPE_ARTICLE + 1;
private Context context;
private ArrayList<ArticleRow> mData = new ArrayList<ArticleRow>();
private LayoutInflater mInflater;
public ArticleRowAdapter(Context context,ArrayList<ArticleRow> data) {
this.context = context;
this.mData = data;
mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
static class ArticleRowHolder
{
TextView tvTitle;
ImageView ivImage;
TextView tvSubTitle;
}
public void addItem(final ArticleRow item) {
mData.add(item);
notifyDataSetChanged();
}
#Override
public int getItemViewType(int position) {
return position == 0 ? TYPE_MAINARTICLE : TYPE_ARTICLE;
}
#Override
public int getViewTypeCount() {
return TYPE_MAX_COUNT;
}
#Override
public int getCount() {
return mData.size();
}
#Override
public ArticleRow getItem(int position) {
return mData.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ArticleRowHolder holder = null;
int type = getItemViewType(position);
if(convertView == null)
{
holder = new ArticleRowHolder();
switch(type)
{
case TYPE_MAINARTICLE:
convertView = mInflater.inflate(R.layout.mainarticle,parent, false);
break;
case TYPE_ARTICLE:
convertView = mInflater.inflate(R.layout.article,parent , false);
break;
}
holder.tvTitle = (TextView)convertView.findViewById(R.id.articleTitle);
holder.ivImage = (ImageView)convertView.findViewById(R.id.articleImage);
holder.tvSubTitle = (TextView)convertView.findViewById(R.id.articleSubTitle);
convertView.setTag(holder);
}
else
{
holder = (ArticleRowHolder)convertView.getTag();
}
ArticleRow articleRow = mData.get(position);
holder.tvTitle.setText((CharSequence)articleRow.title);
holder.tvSubTitle.setText(Html.fromHtml(articleRow.subTitle));
if(articleRow.imageURL == null)
holder.ivImage.setImageDrawable(this.context.getResources().getDrawable(R.drawable.dunk));
else
{
holder.ivImage.setImageDrawable(this.context.getResources().getDrawable(R.drawable.dunk));
}
return convertView;
}
I can say that the reason of lags are imageViews, thats for sure:
holder.ivImage = (ImageView)convertView.findViewById(R.id.articleImage);
try to not to fill imageViews with images and see what happens :). Also check google's BitmapFun and LazyList - both about images in list/grid views

Categories

Resources