Android-Studio I get no display in video thumbnail getView method - java

I need to display the video in thumbnails style but i get no display, if anyone know what i'am done wrong please give me an idea to follow.
I get the idea in this tutorial
http:android-er.blogspot.com/2011/05/display-video-thumbnail-in-listview.html
public class RemarksAdapter extends ArrayAdapter<RemarksInformation> {
Context context;
LayoutInflater inflater;
List<RemarksInformation> remarksInformationList;
public RemarksAdapter(Context context, int resourceId, List<RemarksInformation> remarksInformationList) {
super(context, resourceId, remarksInformationList);
this.context = context;
this.remarksInformationList = remarksInformationList;
inflater = LayoutInflater.from(context);
}
static class RemarksHolder{
TextView remarks;
TextView image_name;
TextView image_path;
TextView video_name;
TextView video_path;
TextView date_send;
TextView time_send;
TextView remarks_by;
RelativeLayout image_wrapper;
RelativeLayout video_wrapper;
ImageView thumbnail;
}
#Override
public View getView(final int position, View convertView, final ViewGroup parent) {
RemarksHolder holder = null;
View v = convertView;
if(v == null){
v = inflater.inflate(R.layout.item_view_remarks, null);
holder = new RemarksHolder();
holder.remarks = (TextView)v.findViewById(R.id.txt_remarks);
holder.date_send = (TextView)v.findViewById(R.id.txt_date_send);
holder.time_send = (TextView)v.findViewById(R.id.txt_time_send);
holder.image_name = (TextView)v.findViewById(R.id.image_name);
holder.image_path = (TextView)v.findViewById(R.id.image_path);
holder.video_name = (TextView)v.findViewById(R.id.video_name);
holder.video_path = (TextView)v.findViewById(R.id.video_path);
holder.remarks_by = (TextView)v.findViewById(R.id.remarks_by);
holder.image_wrapper = (RelativeLayout)v.findViewById(R.id.image_wrapper);
holder.video_wrapper = (RelativeLayout)v.findViewById(R.id.video_wrapper);
holder.thumbnail = (ImageView)v.findViewById(R.id.vid);
v.setTag(holder);
}else{
holder = (RemarksHolder)v.getTag();
}
holder.remarks.setText(remarksInformationList.get(position).getRemarks());
holder.image_name.setText(remarksInformationList.get(position).getImage_name());
holder.image_path.setText(remarksInformationList.get(position).getImage_path());
holder.video_name.setText(remarksInformationList.get(position).getVideo_name());
holder.video_path.setText(remarksInformationList.get(position).getVideo_path());
Bitmap bmThumbnail = ThumbnailUtils.createVideoThumbnail(remarksInformationList
.get(position).getVideo_path(), Thumbnails.MICRO_KIND);
holder.thumbnail.setImageBitmap(bmThumbnail); //Get no display
holder.date_send.setText(remarksInformationList.get(position).getDate_send());
holder.time_send.setText(remarksInformationList.get(position).getTime_send());
holder.remarks_by.setText(remarksInformationList.get(position).getRemarks_by());
holder.image_wrapper.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
((ListView) parent).performItemClick(v, position, 0);
}
});
holder.video_wrapper.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
((ListView) parent).performItemClick(v, position, 0);
}
});
return v;
}

Related

Android: Checkbox in listview (how to create OnCheckedChangeListener in Adapter)

I'm creating a To-do list application and I have a question regarding to using checkboxes and its listeners in List Adapter. My single row in listview contains three TextViews and one Checkbox. I want to change background of single row when user "check" the checkbox. I have read that i should put checkbox listener in my adapter class and so I did it. Now is the problem - when i add few rows to my listview and left the checkbox unchecked for all of them all works fine, but when I add a row, check the checkbox and try to add another one I get error
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.View.setBackgroundColor(int)' on a null object reference
Below is code of my adapter. Thank you for any advice. I'm just starting with Android programming so thank you for understanding in advance.
public class ToDoAdapter extends ArrayAdapter<ToDoTask> {
ArrayList<ToDoTask> objects;
Context context;
int resource;
public ToDoAdapter(#NonNull Context context, #LayoutRes int resource, #NonNull ArrayList<ToDoTask> objects) {
super(context, resource, objects);
this.objects = objects;
this.context = context;
this.resource = resource;
}
#Override
public View getView(final int position, View convertView, final ViewGroup parent) {
View view = convertView;
ToDoHolder toDoHolder = null;
if (view == null) {
LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = layoutInflater.inflate(R.layout.row, parent, false);
toDoHolder = new ToDoHolder();
toDoHolder.rowTitle = (TextView) view.findViewById(R.id.rowTitle);
toDoHolder.rowDesc = (TextView) view.findViewById(R.id.rowDesc);
toDoHolder.rowDate = (TextView) view.findViewById(R.id.rowDate);
toDoHolder.rowIsDone = (CheckBox) view.findViewById(R.id.rowCheckBoxDone);
toDoHolder.rowIsDone.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
if(checked){
parent.getChildAt(position).setBackgroundColor(Color.parseColor("#8FE370"));
}
else
parent.getChildAt(position).setBackgroundColor(Color.WHITE);
}
});
view.setTag(toDoHolder);
} else {
toDoHolder = (ToDoHolder) view.getTag();
}
ToDoTask object = objects.get(position);
toDoHolder.rowTitle.setText(object.getTitle());
toDoHolder.rowDesc.setText(object.getDescription());
toDoHolder.rowDate.setText(object.getDate());
toDoHolder.rowIsDone.setChecked(object.getDone());
return view;
}
static class ToDoHolder {
TextView rowTitle;
TextView rowDesc;
TextView rowDate;
CheckBox rowIsDone;
}
}
Below is my MainActivity class which get details of single row element from "AddToDoTask" class.
public class MainActivity extends AppCompatActivity {
private final int requestCode = 1;
ArrayList<ToDoTask> lista = new ArrayList<>();
ToDoAdapter adapter = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button) findViewById(R.id.buttonAdd);
ListView listView = (ListView) findViewById(R.id.listView);
adapter = new ToDoAdapter(this, R.layout.row, lista);
listView.setAdapter(adapter);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(getApplicationContext(), AddToDoTask.class);
startActivityForResult(intent, requestCode);
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
String title, description, date;
Boolean isDone;
if (requestCode == 1) {
if (null != data) {
title = data.getStringExtra("title");
description = data.getStringExtra("description");
date = data.getStringExtra("date");
isDone = data.getBooleanExtra("done", false);
lista.add(new ToDoTask(title, description, date, isDone));
adapter.notifyDataSetChanged();
}
}
}
}
public class ToDoAdapter extends ArrayAdapter<ToDoTask> {
private ArrayList<ToDoTask> objects;
private Context context;
private int resource;
private SparseBooleanArray checkedPositions = new SparseBooleanArray();
public ToDoAdapter(#NonNull Context context, #LayoutRes int resource, #NonNull ArrayList<ToDoTask> objects) {
super(context, resource, objects);
this.objects = objects;
this.context = context;
this.resource = resource;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ToDoHolder toDoHolder;
if (convertView == null) {
LayoutInflater layoutInflater = LayoutInflater.from(context);
convertView = layoutInflater.inflate(R.layout.row, parent, false);
toDoHolder = new ToDoHolder();
toDoHolder.rowTitle = (TextView) convertView.findViewById(R.id.rowTitle);
toDoHolder.rowDesc = (TextView) convertView.findViewById(R.id.rowDesc);
toDoHolder.rowDate = (TextView) convertView.findViewById(R.id.rowDate);
toDoHolder.rowIsDone = (CheckBox) convertView.findViewById(R.id.rowCheckBoxDone);
convertView.setTag(toDoHolder);
} else {
toDoHolder = (ToDoHolder) convertView.getTag();
}
toDoHolder.rowTitle.setTag(position);
toDoHolder.rowIsDone.setTag(convertView);
toDoHolder.rowIsDone.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
View view = (View) compoundButton.getTag();
TextView title = (TextView) view.findViewById(R.id.rowTitle);
int pos = (int) title.getTag();
if (checked) {
checkedPositions.put(pos, true);
view.setBackgroundColor(Color.parseColor("#8FE370"));
} else {
checkedPositions.put(pos, false);
view.setBackgroundColor(Color.WHITE);
}
}
});
ToDoTask object = objects.get(position);
toDoHolder.rowTitle.setText(object.getTitle());
toDoHolder.rowDesc.setText(object.getDescription());
toDoHolder.rowDate.setText(object.getDate());
toDoHolder.rowIsDone.setChecked(object.getDone() || checkedPositions.get(position));
return convertView;
}
private class ToDoHolder {
private TextView rowTitle;
private TextView rowDesc;
private TextView rowDate;
private CheckBox rowIsDone;
}
}
You must add a layout in your row xml file and put layout in toDoHolder and just change the layouts background color. You can access child views like
layout.findViewByID(int ID);

How can I change text within a listview, using a adapter

Can you help me out, I'm learning! ;-)
I have two buttons "+" and "-" and I want them to increase or decrease the amount by one up or one down. How do I make sure that it will only have effect on the right textview using an adapter.
Now all the buttons only effects the first one. I know I have to get the position/id of the array. But I don't know how.
btw
Merk = Brand
Aantal = Amount
public class ListViewDemo2 extends Activity {
private ArrayList<String> data = new ArrayList<String>();
int aantal = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_view_demo2);
ListView lv = (ListView) findViewById(R.id.bestel_lijst);
lv.setAdapter(new MyListAdapter(this, R.layout.list_item, data));
generateListContent();
}
private void generateListContent(){
for (int i =0; i < 55; i++){
data.add("this is row number: " + i);
}
}
#Override
public boolean onCreateOptionsMenu (Menu menu){
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings){
return true;
}
return super.onOptionsItemSelected(item);
}
private class MyListAdapter extends ArrayAdapter<String>{
private int layout;
public MyListAdapter(Context context, int resource, List<String> objects) {
super(context, resource, objects);
layout = resource;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder mainViewholder = null;
if(convertView == null){
LayoutInflater inflater = LayoutInflater.from(getContext());
convertView = inflater.inflate(layout, parent, false);
ViewHolder viewHolder = new ViewHolder();
viewHolder.Merk = (TextView) convertView.findViewById(R.id.Merk);
viewHolder.Aantal = (TextView) convertView.findViewById(R.id.Aantal);
viewHolder.btnup = (Button) convertView.findViewById(R.id.Bup);
viewHolder.btndown = (Button) convertView.findViewById(R.id.Bdown);
viewHolder.btndown.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v){
aantal = aantal-1;
displayAantal(aantal);
}
});
viewHolder.btnup.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v){
aantal = aantal+1;
displayAantal(aantal);
}
});
convertView.setTag(viewHolder);
}
else {
mainViewholder = (ViewHolder)convertView.getTag();
mainViewholder.Merk.setText(getItem(position));
}
return convertView;
}
}
public class ViewHolder {
TextView Merk;
TextView Aantal;
Button btnup;
Button btndown;
}
public void displayAantal(int aantal) {
TextView aantalView = (TextView) findViewById(R.id.Aantal);
aantalView.setText(String.valueOf(aantal));
}
}
your problem seems to be in your displayAantal(int) method, in particular, in this line:
TextView aantalView = (TextView) findViewById(R.id.Aantal);
You do not need to re-initialize that field here, since it is already initialized inside getView():
viewHolder.Aantal = (TextView) convertView.findViewById(R.id.Aantal);
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder mainViewholder = null;
if(convertView == null){
LayoutInflater inflater = LayoutInflater.from(getContext());
convertView = inflater.inflate(layout, parent, false);
ViewHolder viewHolder = new ViewHolder();
viewHolder.Merk = (TextView) convertView.findViewById(R.id.Merk);
viewHolder.Aantal = (TextView) convertView.findViewById(R.id.Aantal);
viewHolder.btnup = (Button) convertView.findViewById(R.id.Bup);
viewHolder.btndown = (Button) convertView.findViewById(R.id.Bdown);
convertView.setTag(viewHolder);
}
else {
mainViewholder = (ViewHolder)convertView.getTag();
}
String str = getItem(position);
mainViewholder.Merk.setText(str);
viewHolder.btndown.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v){
//just handle the data ,then call adapter.notifyDataSetChanged();
}
});
viewHolder.btnup.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v){
//just handle the data ,then call adapter.notifyDataSetChanged();
}
});
return convertView;
}

Get values of edittext in listview

I want get values of edittext in listview.
This is my CustomAdapter.java
public class CustomAdapter extends BaseAdapter {
Context context;
List<RowItem> rowItem;
ImageView imgIcon, add_cart;
TextView txtTitle;
EditText colli, prezzo, quantita;
String colliStr, prezzoStr, quantitaStr;
ArrayList<String> ArrayPrezzo = new ArrayList<String>();
ArrayList<String> ArrayQuantita = new ArrayList<String>();
ArrayList<String> ArrayColli = new ArrayList<String>();
CustomAdapter(Context context, List<RowItem> rowItem) {
this.context = context;
this.rowItem = rowItem;
}
#Override
public int getCount() {
return rowItem.size();
}
#Override
public Object getItem(int position) {
return rowItem.get(position);
}
#Override
public long getItemId(int position) {
return rowItem.indexOf(getItem(position));
}
#Override
public boolean areAllItemsEnabled() {
return false;
}
#Override
public boolean isEnabled(int position) {
return false;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater mInflater = (LayoutInflater) context
.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
convertView = mInflater.inflate(R.layout.single_row, null);
}
imgIcon = (ImageView) convertView.findViewById(R.id.icon);
txtTitle = (TextView) convertView.findViewById(R.id.title);
add_cart = (ImageView) convertView.findViewById(R.id.icon);
colli = (EditText) convertView.findViewById(R.id.editText3);
prezzo = (EditText) convertView.findViewById(R.id.editText2);
quantita = (EditText) convertView.findViewById(R.id.editText);
add_cart.setFocusable(true);
add_cart.setClickable(true);
final RowItem row_pos = rowItem.get(position);
// setting the image resource and title
imgIcon.setImageResource(R.drawable.product);
txtTitle.setText(row_pos.getTitle());
add_cart.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// HERE I WANT GET THE VALUES OF EDITTEXT;
}
});
return convertView;
}
can someone help me please? In ly listview there are:
textview | edittext1 | edittext2 | edittext3 | imageview
I don't know how to get values of edittext at position current when i click the button.
Can you make a example with my code?
Thnank you
UPDATE:
I have edited the listener:
add_cart.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
prezzoStr = prezzo.getText().toString();
Toast.makeText(context, prezzoStr, Toast.LENGTH_SHORT)
.show();
}
});
but prezzoStr is empty. Toast doesn't show nothing.
I think need use row_pos variable for getting the value of a determinate edittext.
Help please
It's not difficult
add_cart.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String colliValue = colli.getText().toString());
String prezzoValue = prezzo.getText().toString());
String quantitaValue = quantita.getText().toString());
}
});
You have to get parent view and then find text view by id:
add_cart.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
TextView prezzo = (TextView) findByIdRecursively(v, R.id.editText2);
String prezzoStr = prezzo.getText().toString();
}
});
...
public View findByIdRecursively(View view, int targetId) {
View result = view.findViewById(targetId);
if (result != null) {
return result;
}
View parent = (View) view.getParent();
if (parent == null) {
return null;
}
return findByIdRecursively(parent, targetId);
}
The view in onClick is the view that was clicked (add_cart in this case)
... or you can do:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
...
add_cart = (ImageView) convertView.findViewById(R.id.icon);
add_cart.setTag(convertView);
add_cart.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
TextView prezzo = (TextView) ((View)v.getTag()).findViewById(R.id.editText2);
String prezzoStr = prezzo.getText().toString();
}
});

how to change particularly one row textview value while click the button in custome listview?

I have a custom list view with base adapter. i have tried to increment and decrement particularly one row textview value. it is working but all the row textview value is changed while click the button. how can i changed particularly one row textview value. plese suggest me.
public class BreakfastListAdapter extends BaseAdapter {
private Context context;
private String[] number;
private int[] imageid;
ImageView plus1, minus1;
TextView value1;
int a = 0;
public BreakfastListAdapter(Context c, String[] number, int[] imageid) {
context = c;
this.imageid = imageid;
this.number = number;
}
#Override
public int getCount() {
return number.length;
}
#Override
public Object getItem(int position) {
return position;
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
// convertView = new View(context);
convertView = inflater.inflate(R.layout.secondadapter, null);
}
TextView textView = (TextView) convertView.findViewById(R.id.text);
ImageView imageView = (ImageView) convertView.findViewById(R.id.imageView1);
plus1 = (ImageView) convertView.findViewById(R.id.plus);
minus1 = (ImageView) convertView.findViewById(R.id.minus);
value1 = (TextView) convertView.findViewById(R.id.value);
textView.setText(number[position]);
imageView.setImageResource(imageid[position]);
value1.setText(String.valueOf(a));
System.out.println(a + "dddddddddddddd");
plus1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
a = a + 1;
value1.setText(String.valueOf(a));
Toast.makeText(context, String.valueOf(a), 1000).show();
System.out.println("######################");
System.out.println(a);
notifyDataSetChanged();
}
});
return convertView;
}
}
You need to keep the value for each row and not one for all the rows.
Change your int a in an int[] a and keep each value of each row.
You will have the following listener:
#Override
public void onClick(View v) {
a[position] = a[position] + 1;
value1.setText(String.valueOf(a[position]));
Toast.makeText(context, String.valueOf(a[position]), 1000).show();
System.out.println("######################");
System.out.println(a[position]);
notifyDataSetChanged();
}
Here is the complete code:
public class BreakfastListAdapter extends BaseAdapter {
private Context context;
private String[] number;
private int[] imageid;
ImageView plus1, minus1;
TextView value1;
int a[];
public BreakfastListAdapter(Context c, String[] number, int[] imageid) {
context = c;
this.imageid = imageid;
this.number = number;
this.a = new int[number.length];
}
#Override
public int getCount() {
return number.length;
}
#Override
public Object getItem(int position) {
return position;
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
// convertView = new View(context);
convertView = inflater.inflate(R.layout.secondadapter, null);
}
TextView textView = (TextView) convertView.findViewById(R.id.text);
ImageView imageView = (ImageView) convertView.findViewById(R.id.imageView1);
plus1 = (ImageView) convertView.findViewById(R.id.plus);
minus1 = (ImageView) convertView.findViewById(R.id.minus);
value1 = (TextView) convertView.findViewById(R.id.value);
textView.setText(number[position]);
imageView.setImageResource(imageid[position]);
value1.setText(String.valueOf(a[position]));
System.out.println(a[position] + "dddddddddddddd");
plus1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
a[position] = a[position] + 1;
value1.setText(String.valueOf(a[position]));
Toast.makeText(context, String.valueOf(a[position]), 1000).show();
System.out.println("######################");
System.out.println(a[position]);
notifyDataSetChanged();
}
});
return convertView;
}
}
You should setText string to perticular row TextView.
Every row is referenced through convertView so we will use it
Modification is :
plus1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
a = a + 1;
// this is change
convertView.value1.setText(String.valueOf(a));
Toast.makeText(context, String.valueOf(a), 1000).show();
System.out.println("######################");
System.out.println(a);
notifyDataSetChanged();
}
We need to set a value to 0 at last :
// reset a value at end of getView() method
a = 0;
So that when next row is displayed, a value is 0 and it starts increment from 0
Try this one remove
a = a + 1;//because this is a global variable in the class
change value1.setText(""+(a+1));
if this's not the problem declare an array/arraylist with size similar to listview size and populate arraylist inside the constructer with all values set to 0.Then inside getview() method
int currentposition=position;
plus1.setTag(currentposition);
value1.setText(arraylist.get(currentposition));
plus1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
int btnvalue=Integer.parseInt(plus1.getTag().toString());
arraylist.set(btnvalue,(Integer.parseInt(arraylist.get(btnvalue)))+1);
notifydatasetchanged();
}
});
Implement ViewHolder Class to maintain a separate copy of their widgets for example
ViewHolder{
TextView t1;
Imageview i1,i2;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
ViewHolder holder;
if (convertView == null) {
convertView = inflater.inflate(R.layout.secondadapter, null);
holder = new ViewHolder();
holder.t1 = (TextView) convertView.findViewById(R.id.text);
holder.i1 =convertView.findViewById(R.id.imageView1);
holder.i2 =convertView.findViewById(R.id.imageView1);
convertView.setTag(holder);
}else
{
ViewHolder holder = (ViewHolder) convertView.getTag();
}
holder.t1.setText(number[position]);
holder.i1.setImageResource(imageid[position]);
holder.t1.setText(String.valueOf(a));
System.out.println(a + "dddddddddddddd");
holdert1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
a = a + 1;
holder.t1.setText(String.valueOf(a));
Toast.makeText(context, String.valueOf(a), 1000).show();
System.out.println("######################");
System.out.println(a);
notifyDataSetChanged();
}
});
return convertView
}

Get parent layout from custom Adapter

Could anybody explain me, how to realize?
I have an activity with listview and footer with some elements(textview).
Listview built with custom adapter. Each listview item has few elements. And my question: how can i change textview in footer, from custom adapter, when i clicking on some listview's element?
Thx a lot!
/**** My adapter ****/
public class MyListAdapter extends ArrayAdapter<Product> implements UndoAdapter {
private final Context mContext;
private HashMap<Product, Integer> mIdMap = new HashMap<Product, Integer>();
ArrayList<Product> products = new ArrayList<Product>();
final int INVALID_ID = -1;
LayoutInflater lInflater;
String imagePath;
public MyListAdapter(Context context, int textViewResourceId, List<Product> prod) {
//super(context, textViewResourceId, prod);
super(prod);
lInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mContext = context;
for (int i = 0; i < prod.size(); i++) {
//add(prod.get(i));
mIdMap.put(prod.get(i),i);
}
}
#Override
public long getItemId(final int position) {
//return getItem(position).hashCode();
Product item = (Product) getItem(position);
return mIdMap.get(item);
}
#Override
public boolean hasStableIds() {
return true;
}
#Override
public View getView(final int position, View convertView, final ViewGroup parent) {
ViewHolder holder = null;;
Product p = getItem(position);
if (convertView == null) {
convertView = lInflater.inflate(R.layout.item, null);
//convertView.setBackgroundResource(R.drawable.rounded_corners);
int currentTheme = Utils.getCurrentTheme(convertView.getContext());
switch (currentTheme) {
case 0:
convertView.setBackgroundResource(R.drawable.rounded_corners);
break;
case 1:
convertView.setBackgroundResource(R.drawable.border);
break;
default:
convertView.setBackgroundResource(R.drawable.rounded_corners);
break;
}
holder = new ViewHolder();
holder.tvDescr = (TextView) convertView.findViewById(R.id.tvDescr);
holder.list_image = (ImageView) convertView.findViewById(R.id.list_image);
holder.products_amount = (TextView) convertView.findViewById(R.id.amountDigits);
holder.products_price = (TextView) convertView.findViewById(R.id.priceDigits);
holder.ivImage = (ImageView) convertView.findViewById(R.id.ivImage);
holder.unit = (TextView) convertView.findViewById(R.id.unit);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
if(p.getProductImageBitmap() != null && p.getProductImageBitmap() != "") {
Log.d("PATH -- ", p.getProductImageBitmap());
ImageLoader imageLoader = ImageLoader.getInstance();
DisplayImageOptions options = new DisplayImageOptions.Builder().cacheInMemory(true)
.resetViewBeforeLoading(true)
.showImageForEmptyUri(R.drawable.ic_launcher)
.showImageOnFail(R.drawable.ic_launcher)
/*.showImageOnLoading(R.id.progress_circular)*/
.build();
imageLoader.displayImage(p.getProductImageBitmap(), holder.list_image, options);
} else {
holder.list_image.setImageResource(R.drawable.ic_launcher);
}
holder.tvDescr.setText(p.getProductName());
holder.ivImage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String deletedItem = getItem(position).getProductName();
MyListAdapter.this.remove(getItem(position));
if (MyListAdapter.this.getCount() > 0) {
Toast.makeText(mContext, deletedItem + " " + mContext.getString(R.string.deleted_item), Toast.LENGTH_SHORT).show();
MyListAdapter.this.notifyDataSetChanged();
} else {
Toast.makeText(mContext,mContext.getString(R.string.sklerolist_empty), Toast.LENGTH_SHORT).show();
}
}
});
//Функционал для большой картинки продукта
//открывается новое активити с большой картинкой
holder.list_image.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
imagePath = getItem(position).getProductImageBitmap();
if(imagePath != null && imagePath != "") {
Pattern normalPrice = Pattern.compile("^file");
Matcher m2 = normalPrice.matcher(imagePath);
if (m2.find()) {
Intent myIntent = new Intent(view.getContext(), ViewImage.class).putExtra("imagePath", imagePath);
view.getContext().startActivity(myIntent);
}
}
}
});
holder.products_price.setText(fmt(p.getProductPrice()));
holder.products_amount.setText(fmt(p.getProductAmount()));
holder.unit.setText(p.getProductUnit());
return convertView;
}
public static String fmt(double d){
if(d == (long) d)
return String.format("%d",(long)d);
else
return String.format("%s",d);
}
static class ViewHolder {
ImageView list_image;
TextView tvDescr;
TextView products_amount;
TextView products_price;
TextView unit;
ImageView ivImage;
ProgressBar circleProgress;
}
#NonNull
#Override
public View getUndoView(final int position, final View convertView, #NonNull final ViewGroup parent) {
View view = convertView;
if (view == null) {
//view = LayoutInflater.from(mContext).inflate(R.layout.undo_row, parent, false);
view = lInflater.inflate(R.layout.undo_row, parent, false);
}
return view;
}
#NonNull
#Override
public View getUndoClickView(#NonNull final View view) {
return view.findViewById(R.id.undo_row_undobutton);
}
public View getHeaderView(final int position, final View convertView, final ViewGroup parent) {
TextView view = (TextView) convertView;
//View view = convertView;
if (view == null) {
//view = (TextView) LayoutInflater.from(mContext).inflate(R.layout.list_header, parent, false);
//view = lInflater.inflate(R.layout.list_header, parent, false);
}
//view.setText(mContext.getString(R.string.header, getHeaderId(position)));
return view;
}
public long getHeaderId(final int position) {
return position / 10;
}
}
Your ListView has a listener for the click events on list elements.
#Override
public void onListItemClick(ListView l, View v, int position, long id) {
// Do something when a list item is clicked
}
But if you want to pas something else back from the adapter to the Activity or the Fragment that contains that ListView and Adapter, you should create a simple interface and set it as listener to your adapter. After that, set click events on your rows from within the adapter, and notify the Activity or Fragment using your own interface.
For example you have the interface defined like this
public interface OnItemClickedCustomAdapter {
public void onClick(ItemPosition position);
}
and in your Adapter class you will have a private member
private OnItemClickedCustomAdapter mListener;
and a method used to set the listener
public void setOnItemClickedCustomAdapter(OnItemClickedCustomAdapter listener){
this.mListener = listener;
}
From your Activity or Fragment where your ListView is defined, and your adapter is set, you will be able to call setOnItemClickedCustomAdapter with this as parameter, and there you go. Your activity will now listen for your events. To trigger an event, just call mListener.onClick() from your custom adapter. You can pass back data you need back to the Activity or Fragment, and from there you have access to your Header or Footer directly, and you can change the text on them.

Categories

Resources