从适配器刷新片段

时间:2016-08-11 09:00:34

标签: android adapter

我的ListView包含片段中的项目。每个项目都有计数器,例如

项目1 - 4 项目2 - 5 在片段的顶部我有textView与所有元素的总和:sum - 9.我可以通过点击项目行上的图像删除项目。在适配器(getView())中它是这样的:

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();
                    }
                });

该项目正在从列表中删除但计数器不会更改值 - 例如项目2已被删除但计数器仍然显示5.我在onResume()方法中更新它并且它工作但只有我离开片段并返回它。我尝试在片段中编写update方法并在适配器中使用它 - 我用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;
    }

但是当我调用fragment.something时我得到零点异常:/如何从适配器刷新片段/计数器textView?我也尝试再次设置片段,但它不起作用。

我的适配器的一部分(很长)

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;
}

和我的活动:

public void onUpdate(String value){
        mCountTV.setText(value);
    }

private void createAdapter() {
    adapter.setOnUpdateListner(this);
}

}

1 个答案:

答案 0 :(得分:1)

您可以按照以下流程进行操作。

PartAdapter设置一个将在Fragment中实现的侦听器,用于更新sum文本视图的值。

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();
                }
            });
}

在你的片段中

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);
    }
}

在您的活动中

class MainActivity implements PartAdapter.OnUpdateListener {

    public void onUpdate(String value){
            textView.setText(value);
    }
}