Image not visible in list view - android

I have a custom list view with image and text.Now whenever i open the list view initially the image is visible.Now when i scroll the list the images are being visible.Also i have put condition on which image should be displayed when,but in beginning all the images are same the images are only changed when i scroll the list.I know this issue is related to recycle of the list but i am not able to solve it.Please do help me
Code
public class ShareWithMeAdapter extends BaseAdapter {
private Context context;
private ArrayList<SharedWithMeModel> data;
public ShareWithMeAdapter(Context context, ArrayList<SharedWithMeModel> data) {
this.context = context;
this.data = data;
}
#Override
public int getCount() {
return data.size();
}
#Override
public Object getItem(int i) {
return null;
}
#Override
public long getItemId(int i) {
return 0;
}
#Override
public View getView(int i, View view, ViewGroup viewGroup) {
final ViewHolder holder;
if (view == null) {
holder = new ViewHolder();
view = LayoutInflater.from(context).inflate(R.layout.share_with_me_custom_list, viewGroup, false);
holder.tvTitle = (TextView) view.findViewById(R.id.tv_sharedwithme_title);
holder.tvName = (TextView) view.findViewById(R.id.tv_sharedwithme_name);
holder.tvSharedOn = (TextView) view.findViewById(R.id.tv_sharedwithme_on);
holder.image = (ImageView) view.findViewById(R.id.iv_sharedwithme_image);
view.setTag(holder);
} else {
holder = (ViewHolder) view.getTag();
}
holder.tvTitle.setText(data.get(i).getTitle());
if (data.get(i).getName().equals("")) {
holder.tvName.setText("Shared With " + data.get(i).getName());
} else {
holder.tvName.setText("Shared With " + data.get(i).getName());
}
holder.tvSharedOn.setText(data.get(i).getDate());
if (data.get(i).getExtension().equals(".jpg") || data.get(i).getExtension().equals(".png") || data.get(i).getExtension().equals(".jpeg") || data.get(i).getExtension().equals(".gif") || data.get(i).getExtension().equals(".tiff")) {
holder.image.setBackgroundResource(R.drawable.image);
}
if (data.get(i).getExtension().equals(".docx") || data.get(i).getExtension().equals(".doc")) {
holder.image.setBackgroundResource(R.drawable.docx);
}
if (data.get(i).getExtension().equals(".xls") || data.get(i).getExtension().equals(".xlxs")) {
holder.image.setBackgroundResource(R.drawable.excel);
}
if (data.get(i).getExtension().equals(".pdf")) {
holder.image.setBackgroundResource(R.drawable.pdf);
}
// if (data.get(i).getExtension().equals(".ppt")) {
// holder.image.setBackgroundResource(R.drawable.);
// }
return view;
}
private class ViewHolder {
TextView tvTitle, tvName, tvSharedOn;
ImageView image;
}
}

Try this way,hope this will help you to solve your problem.
public class ShareWithMeAdapter extends BaseAdapter {
private Context context;
private ArrayList<SharedWithMeModel> data;
public ShareWithMeAdapter(Context context, ArrayList<SharedWithMeModel> data) {
this.context = context;
this.data = data;
}
#Override
public int getCount() {
return data.size();
}
#Override
public Object getItem(int i) {
return data.get(i);
}
#Override
public long getItemId(int i) {
return i;
}
#Override
public View getView(int i, View view, ViewGroup viewGroup) {
final ViewHolder holder;
if (view == null) {
holder = new ViewHolder();
view = LayoutInflater.from(context).inflate(R.layout.share_with_me_custom_list, null);
holder.tvTitle = (TextView) view.findViewById(R.id.tv_sharedwithme_title);
holder.tvName = (TextView) view.findViewById(R.id.tv_sharedwithme_name);
holder.tvSharedOn = (TextView) view.findViewById(R.id.tv_sharedwithme_on);
holder.image = (ImageView) view.findViewById(R.id.iv_sharedwithme_image);
view.setTag(holder);
} else {
holder = (ViewHolder) view.getTag();
}
holder.tvTitle.setText(data.get(i).getTitle());
if (data.get(i).getName().equals("")) {
holder.tvName.setText("Shared With " + data.get(i).getName());
} else {
holder.tvName.setText("Shared With " + data.get(i).getName());
}
holder.tvSharedOn.setText(data.get(i).getDate());
if (data.get(i).getExtension().equals(".jpg") || data.get(i).getExtension().equals(".png") || data.get(i).getExtension().equals(".jpeg") || data.get(i).getExtension().equals(".gif") || data.get(i).getExtension().equals(".tiff")) {
holder.image.setImageResource(R.drawable.image);
}else if (data.get(i).getExtension().equals(".docx") || data.get(i).getExtension().equals(".doc")) {
holder.image.setImageResource(R.drawable.docx);
}else if (data.get(i).getExtension().equals(".xls") || data.get(i).getExtension().equals(".xlxs")) {
holder.image.setImageResource(R.drawable.excel);
}else{
holder.image.setImageResource(R.drawable.pdf);
}
return view;
}
private class ViewHolder {
TextView tvTitle;
TextView tvName;
TextView tvSharedOn;
ImageView image;
}
}

try this , hope it might helpfull
replace all
holder.image.setBackgroundResource(R.drawable.filename);
with
holder.image.setImageResource(R.drawable.filename);
or change these methods
#Override
public Object getItem(int position) {
return data.get(position);
}
#Override
public long getItemId(int position) {
return position;
}

Related

Android getView() in BaseAdapter not called (but getCount() is called)

There no errors but the ListView is empty. I have implemented getCount() which returns right number of items in my ArrayList. And the ListView is visibility。
I had call setListAdapter on the ListView,and the xml is match_parent.
This is my Fragment.
mNewsList = infoBean.getNews();
if (mNewsList != null) {
mNewsAdapter = new NewsAdapter();
mListView.setAdapter(mNewsAdapter);
mNewsAdapter.notifyDataSetChanged();
}
} else {//load more
List<HomeBean.NewsBean> news = infoBean.getNews();
if (mNewsList != null) {
mNewsList.addAll(news);
mNewsAdapter.notifyDataSetChanged();
}
This is my Adapter:
private class NewsAdapter extends BaseAdapter {
#Override
public int getCount() {
return mNewsList.size();
}
#Override
public HomeBean.NewsBean getItem(int position) {
return mNewsList.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = View.inflate(MyApplication.getmContext(), R.layout.item_list, null);
holder = new ViewHolder();
holder.ivIcon = convertView.findViewById(R.id.iv_item);
holder.tvTitle = convertView.findViewById(R.id.tv_item_list_title);
holder.content = convertView.findViewById(R.id.tv_item_list_content);
holder.tvTime = convertView.findViewById(R.id.tv_time);
holder.author = convertView.findViewById(R.id.tv_author);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.tvTitle.setText(getItem(position).getTitle());
holder.content.setText(getItem(position).getContent());
SimpleDateFormat dataFormat = new SimpleDateFormat("yyyy-MM-dd");
String itemTime = dataFormat.format(getItem(position).getCreate_at() * 1000);
holder.tvTime.setText(itemTime);
holder.author.setText(getItem(position).getSection_name());
if (getItem(position).getCover_pic().startsWith("http:")) {
picUrl = getItem(position).getCover_pic();
} else {
picUrl = RBConstants.SERVER_PIC + getItem(position).getCover_pic();
}
HttpLoader.getInstance(MyApplication.getmContext()).display(holder.ivIcon, picUrl);
return convertView;
}
}
This is my holder.
class ViewHolder {
public ImageView ivIcon;
public TextView tvTitle;
public TextView tvTime;
public TextView content;
public TextView author;
}
this is because mListView.setAdapter(mNewsAdapter); is never executed and of cource there is no need to call mNewsAdapter.notifyDataSetChanged(); immediatelly after calling mListView.setAdapter(mNewsAdapter);
ListView is old, please switch to RecyclerView.

duplicate selection of Check boxes in android listview

Am using check boxes in listview and am able to get the selected values also but my problem is when select the first position checkbox, automatically selecting 7th positioned checkbox. what will be the problem and please suggest me how to resolve this problem.
public class TransactionAdapter extends BaseAdapter {
LayoutInflater inflator;
String veh_reg_no;
String fuel_qty, total_amt;
public TransactionAdapter(Context context) {
inflator = getLayoutInflater();
}
#Override
public int getCount() {
if (pso_transaction_list.size() != 0)
return pso_transaction_list.size();
return 0;
}
#Override
public Object getItem(int position) {
return position;
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
final Viewholder holder;
if(convertView==null) {
convertView = inflator.inflate(R.layout.pso_transation_layout, parent, false);
holder=new Viewholder();
holder.vT_tl_regnumber = (TextView) convertView.findViewById(R.id.vT_psotl_regnumber);
holder.vT_tl_drivername = (TextView) convertView.findViewById(R.id.vT_psotl_drivername);
holder.vT_tl_quantity = (TextView) convertView.findViewById(R.id.vT_psotl_quantity);
holder.vT_tl_totalCost = (TextView) convertView.findViewById(R.id.vT_psotl_totalCost);
holder.vT_psotl_payment = (TextView) convertView.findViewById(R.id.vT_psotl_payment);
holder.vI_psotl_statusimg = (ImageView) convertView.findViewById(R.id.vI_psotl_statusimg);
holder.vI_pha_Next = (ImageView) convertView.findViewById(R.id.vI_pha_Next);
holder.vI_pha_select = (CheckBox) convertView.findViewById(R.id.vI_pha_select);
holder.vT_psotl_unit = (TextView) convertView.findViewById(R.id.vT_psotl_unit);
holder.vT_pha_rsr = (TextView) convertView.findViewById(R.id.vT_pha_rsr);
holder.vL_psotl_nextlayout = (LinearLayout) convertView.findViewById(R.id.vL_psotl_nextlayout);
holder.vL_psotl_checkboxlayout = (LinearLayout) convertView.findViewById(R.id.vL_psotl_checkboxlayout);
convertView.setTag(holder);
}else{
holder = (Viewholder) convertView.getTag();
holder.vI_pha_select.setOnCheckedChangeListener(null);
}
if (pso_transaction_list.get(position).getPaymentMode().getId() == 1 && pso_transaction_list.get(position).getWFStatus() == 7) {
String pmt_status = "C/A | RCVD ";
holder.vT_psotl_payment.setText(pmt_status);
} else if (pso_transaction_list.get(position).getPaymentMode().getId() == 1) {
holder.vT_psotl_payment.setText("C/A");
} else {
holder.vT_psotl_payment.setText("P/G");
}
if (pso_transaction_list.get(position).getFuelStations().getCountry() != null) {
String[] currency = Getlocationaddress_by_address.currency(pso_transaction_list.get(position).getFuelStations().getCountry(), PsomytransactionlistActivity.this);
holder.vT_pha_rsr.setText(" " + currency[0] + " ");
holder.vT_psotl_unit.setText(" " + currency[1]);
}
holder.vI_pha_select.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked{
if (isChecked) {
selected_list_payment.add(pso_transaction_list.get(position).getPaymentMode().getId() + "");
selected_list.add(pso_transaction_list.get(position).getId() + "");
selecteddetails.add(pso_transaction_list.get(position).getVehicleNo());
payment_status.add(pso_transaction_list.get(position).getWFStatus() + "");
} else {
selected_list.remove(pso_transaction_list.get(position).getId() + "");
selected_list_payment.remove(pso_transaction_list.get(position).getPaymentMode().getId() + "");
selecteddetails.remove(pso_transaction_list.get(position).getVehicleNo());
payment_status.remove(pso_transaction_list.get(position).getWFStatus() + "");
}
}
});
veh_reg_no = pso_transaction_list.get(position).getVehicleNo();
holder.vT_tl_regnumber.setText(veh_reg_no);
holder.vT_tl_drivername.setText(pso_transaction_list.get(position).getDriver().getName());
fuel_qty = " " + pso_transaction_list.get(position).getQuantity() + "";
holder.vT_tl_quantity.setText(fuel_qty);
total_amt = pso_transaction_list.get(position).getTotalPrice() + "";
holder.vT_tl_totalCost.setText(total_amt);
holder.vL_psotl_nextlayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent trans_intent = new Intent(PsomytransactionlistActivity.this, Psotransactiondetails.class);
trans_intent.putExtra("trans_details_position", position);
startActivity(trans_intent);
}
});
return convertView;
}
}
public static class Viewholder{
public TextView vT_tl_regnumber, vT_tl_drivername, vT_tl_quantity, vT_tl_totalCost, vT_psotl_payment, vT_psotl_unit, vT_pha_rsr;
public ImageView vI_psotl_statusimg, vI_pha_Next;
public LinearLayout vL_psotl_nextlayout, vL_psotl_checkboxlayout;
public CheckBox vI_pha_select;
}
Try this way. this worked for me
public class CustomAdapter extends BaseAdapter {
private final LayoutInflater inflater;
private final Context context;
private List<ModelPooja> listData;
public CustomAdapter(Context mainActivity, List<ModelPooja> listData) {
context = mainActivity;
this.listData = listData;
inflater = (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 0;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
convertView = inflater.inflate(R.layout.list_item_poojaselection, null);
holder.tv = (TextView) convertView.findViewById(R.id.list_item_poojaname);
holder.checks = (CheckBox) convertView.findViewById(R.id.list_item_poojacheck);
convertView.setTag(holder);
}else {
holder = (ViewHolder) convertView.getTag();
}
holder.checks.setOnCheckedChangeListener(null);
holder.checks.setFocusable(false);
if (listData.get(position).isselected) {
holder.checks.setChecked(true);
} else {
holder.checks.setChecked(false);
}
holder.checks.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton cb, boolean b) {
if (checkMaxLimit()) {
if (listData.get(position).isselected && b) {
holder.checks.setChecked(false);
listData.get(position).isselected = false;
} else {
holder.checks.setChecked(false);
listData.get(position).isselected = false;
Toast.makeText(context, "Max limit reached", Toast.LENGTH_SHORT).show();
}
} else {
if (b) {
listData.get(position).isselected = true;
} else {
listData.get(position).isselected = false;
}
}
}
});
holder.tv.setText(listData.get(position).getPOOJA_LISTING_NAME());
return convertView;
}
public boolean checkMaxLimit() {
int countermax = 0;
for(ModelPooja item : listData){
if(item.isselected){
countermax++;
}
}
return countermax >= 5;
}
public class ViewHolder {
TextView tv;
public CheckBox checks;
}
}
For Recyclerview try this way
http://android-pratap.blogspot.in/2015/01/recyclerview-with-checkbox-example.html
#Override
public int getViewTypeCount() {
return getCount();
}
#Override
public int getItemViewType(int position) {
return position;
}
Add two functions into your "Adapter" class
Note: If you use RecycleView, just add getItemViewType(int position).

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.

Android Drawer – Change Icon onClick

I have got a simple navigation drawer with a header (see picture).
What I want to do now is whenever the header (listview-item with position 0) is clicked to either change the down-arrow-icon (bottom right corner of header) to an up-arrow-icon or to rotate it so the arrow points up. How can i achieve this with my adapter?
My List-Adapter:
public class MyAdapter extends BaseAdapter {
private String[] _navTitles;
private int[] _navIcons;
private String _nameHeader;
private int _profileHeader;
private String _emailHeader;
public int _arrowHeader;
public MyAdapter(String[] navTitles, int[] navIcons, String nameHeader, String emailHeader, int profileHeader, int arrowHeader) {
_navTitles = navTitles;
_navIcons = navIcons;
_nameHeader = nameHeader;
_emailHeader = emailHeader;
_profileHeader = profileHeader;
_arrowHeader = arrowHeader;
}
#Override
public int getCount() {
return _navTitles.length;
}
#Override
public Object getItem(int position) {
return _navTitles[position];
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
ViewHolder viewHolder = null;
if (view == null) {
if (position == 0) {
view = LayoutInflater.from(parent.getContext()).inflate(R.layout.header, parent, false);
viewHolder = new ViewHolder();
viewHolder.profileView = (CircleImageView) view.findViewById(R.id.circleView);
viewHolder.nameView = (TextView) view.findViewById(R.id.name);
viewHolder.emailView = (TextView) view.findViewById(R.id.email);
viewHolder.arrowView = (ImageView) view.findViewById(R.id.arrow);
viewHolder.holderID = 0;
view.setTag(viewHolder);
} else {
view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_row, parent, false);
viewHolder = new ViewHolder();
viewHolder.textView = (TextView) view.findViewById(R.id.rowText);
viewHolder.imageView = (ImageView) view.findViewById(R.id.rowIcon);
viewHolder.holderID = 1;
view.setTag(viewHolder);
}
} else {
viewHolder = (ViewHolder) view.getTag();
}
if (_navTitles.length > position && position != 0) {
viewHolder.textView.setText(_navTitles[position]);
viewHolder.textView.setTag(_navTitles[position]);
viewHolder.imageView.setImageResource(_navIcons[position]);
viewHolder.imageView.setTag(_navIcons[position]);
}
if (position == 0) {
viewHolder.profileView.setImageResource(_profileHeader);
viewHolder.nameView.setText(_nameHeader);
viewHolder.emailView.setText(_emailHeader);
viewHolder.arrowView.setImageResource(_arrowHeader);
}
return view;
}
public static class ViewHolder {
int holderID;
TextView textView;
ImageView imageView;
CircleImageView profileView;
TextView nameView;
TextView emailView;
ImageView arrowView;
}
}
Thanks for any help!
I solved it by myself. If anybody needs it:
final ViewHolder finalViewHolder = viewHolder;
viewHolder.arrowView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (finalViewHolder.arrowView.getTag().equals("down")) {
finalViewHolder.arrowView.setImageResource(R.drawable.ic_arrow_drop_up_white_48dp);
finalViewHolder.arrowView.setTag("up");
} else if (finalViewHolder.arrowView.getTag().equals("up")) {
finalViewHolder.arrowView.setImageResource(R.drawable.ic_arrow_drop_down_white_48dp);
finalViewHolder.arrowView.setTag("down");
}
}
});

Categories

Resources