I have ListView with items in my fragment. Every item has counter for example
Item 1 - 4 Item 2 - 5 On the top of the fragment I have textView with sum of all elements : sum - 9. I can delete Item with click on image on the item row. In adapter (getView()) it goes like this :
final LinearLayout delete = (LinearLayout)dialog.findViewById(R.id.delete_layout);
delete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mParts.remove(part);
notifyDataSetChanged();
dialog.dismiss();
}
});
The item is being removed from list but counter doesn't change value - for example Item 2 was removed but counter still shows 5. I update it in onResume() method and it works but only if I leave fragment and come back to it. I try to write update method in fragment and use it in adapter - I created constructor with fragment :
public PartAdapter(Context context, int resource, List objects, InvActivity mActivity,InvFragment mFragment) {
super(context, resource,objects);
this.mParts = objects;
mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.mActivity=mActivity;
this.mFragment = mFragment;
}
but when I call fragment.something I get null point exception :/ How can I refresh fragment/counter textView from adapter ? I also tried to set fragment again but it doesn't work.
Part of my adapter (it's quite long)
public class PartAdapter extends ArrayAdapter {
private OnUpdateListener listener;
public interface OnUpdateListener {
void onUpdate(String text);
}
public void setOnUpdateListner(OnUpdateListener listener) {
this.listener = listener;
}
private List<Part>mParts;
private LayoutInflater mInflater;
private TextView mPartName;
public CheckBox mState;
private ImageView mActionArrow;
public static final String TAG = PartAdapter.class.getSimpleName();
private static final int CAM_REQUEST = 1313;
private InvActivity mActivity;
private AdapterCallback mAdapterCallback;
private TextView mCounter;
private ImageView mDelete;
private InvFragment mFragment;
public boolean shouldScan = true;
private final byte[] pal = new byte[]{
(byte) 0xDA, (byte) 0xAD, // const
(byte) 0x04, (byte) 0x74, (byte) 0x31, // com
(byte) 0xe7, (byte) 0x64 //last
};
private final byte[] readId = new byte[]{
(byte) 0xDA, (byte) 0xAD, // const
(byte) 0x04, (byte) 0x6f, (byte) 0x69, // com
(byte) 0xec, (byte) 0x6e //last
};
private UsbManager usbManager;
private UsbDevice device;
public List<String> codes;
public PartAdapter(Context context, int resource, List objects, InvActivity mActivity,AdapterCallback callback) {
super(context, resource,objects);
this.mParts = objects;
mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.mActivity=mActivity;
this.mAdapterCallback = callback;
}
public PartAdapter(Context context, int resource, List objects, InvActivity mActivity,InvFragment mFragment) {
super(context, resource,objects);
this.mParts = objects;
mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.mActivity=mActivity;
this.mFragment = mFragment;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
if(convertView == null)
convertView=mInflater.inflate(R.layout.part_item,parent,false);
mState = (CheckBox) convertView.findViewById(R.id.state_chb);
mDelete = (ImageView)convertView.findViewById(R.id.delete_car);
mDelete.setVisibility(View.INVISIBLE);
final Part party = mParts.get(position);
if(party.isScan() || party.getType().contentEquals("2")) {
mState.setChecked(true);
} else if (!party.isScan()){
mState.setChecked(false);
}
if(party.getType().contentEquals("2"))
mDelete.setVisibility(View.VISIBLE);
if(mParts.get(position).getClass().isInstance(new Part())) {
final Part part = (Part) mParts.get(position);
mPartName = (TextView)convertView.findViewById(R.id.car_name_tv);
mState = (CheckBox) convertView.findViewById(R.id.state_chb);
mPartName.setText(part.getName());
mActionArrow = (ImageView)convertView.findViewById(R.id.arrow_action);
mCounter = (TextView)convertView.findViewById(R.id.car_count_tv);
mCounter.setText(part.getQuantity());
if(!part.isScan()) {
mState.setChecked(false);
} else if(part.isScan() || part.getType().contentEquals("2")) {
mState.setChecked(true);
}
mActionArrow.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final Dialog dialog = new Dialog(getContext());
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.dialog_context_menu);
final LinearLayout delete = (LinearLayout)dialog.findViewById(R.id.delete_layout);
delete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int totalSum = 5;
int partValue = 2;
mParts.remove(part);
notifyDataSetChanged();
dialog.dismiss();
if(listener != null){
listener((totalSum - partValue).toString());
}
}
});
LinearLayout photo = (LinearLayout)dialog.findViewById(R.id.photo_layout);
photo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mActivity.dispatchTakePictureIntent();
}
});
LinearLayout note = (LinearLayout)dialog.findViewById(R.id.note_layout);
note.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
final Dialog noteDialog = new Dialog(getContext());
noteDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
noteDialog.setContentView(R.layout.dialog_note);
final EditText noteET = (EditText)noteDialog.findViewById(R.id.note_et);
if(!part.getNote().contentEquals("null"))
noteET.setText(part.getNote());
Button save = (Button)noteDialog.findViewById(R.id.save_button);
save.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
part.setNote(noteET.getText().toString());
noteDialog.dismiss();
}
});
noteDialog.show();
Button cancel = (Button) noteDialog.findViewById(R.id.cancel_button);
cancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
noteDialog.dismiss();
}
});
}
});
LinearLayout values = (LinearLayout)dialog.findViewById(R.id.values_layout);
values.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final Dialog noteDialog = new Dialog(getContext());
noteDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
noteDialog.setContentView(R.layout.dialog_note);
final EditText noteET = (EditText)noteDialog.findViewById(R.id.note_et);
noteET.setInputType(InputType.TYPE_CLASS_NUMBER);
Button save = (Button)noteDialog.findViewById(R.id.save_button);
save.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
part.setQuantity(noteET.getText().toString());
noteDialog.dismiss();
dialog.dismiss();
}
});
noteDialog.show();
Button cancel = (Button) noteDialog.findViewById(R.id.cancel_button);
cancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
noteDialog.dismiss();
dialog.dismiss();
}
});
}
});
dialog.show();
}
});
}
return convertView;
}
and from my activity :
public void onUpdate(String value){
mCountTV.setText(value);
}
private void createAdapter() {
adapter.setOnUpdateListner(this);
}
}
You can follow a process like below.
In your PartAdapter set a listener which will implemented in Fragment used to update value of sum text view.
class PartAdapter {
private OnUpdateLitener listener;
public interface OnUpdateListener {
void onUpdate(String text);
}
public void setOnUpdateListner(OnUpdateListener listener) {
this.listener = listener;
}
// in your getView() method
final LinearLayout delete = (LinearLayout)dialog.findViewById(R.id.delete_layout);
delete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mParts.remove(part);
notifyDataSetChanged();
if(listener != null){
listener.onUpdate(String.valueOf(totalSum - partValue));
}
dialog.dismiss();
}
});
}
In your fragment
class MyFragment {
private OnUpdateListener listener;
void onAttach(Activity activity){
if(!(activity instaceof PartAdapter.OnUpdateListener)){
throw new IllegalStateException("Must implement OnUpdateListener");
}
listener = (PartAdapter.OnUpdateListener) activity;
}
private void createAdapter() {
PartAdapter adapter = new PartAdapter(getContext(), R.layout.part_item, mParts, mActivity, new AdapterCallback() {
@Override
public void onMethodCallback() {
Log.e(TAG, "onMethodCallback: ");
}
});
adapter.setOnUpdateListener(this);
mPartsLV.setAdapter(adapter);
}
}
In your activity
class MainActivity implements PartAdapter.OnUpdateListener {
public void onUpdate(String value){
textView.setText(value);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With