我有一個ListView連接到CustomAdapter。在Adapter類中,我有不同Views的偵聽器。當有新的變化,過來Android:從適配器獲取信息
currentItem.myListItemMethod()
現在我不知道如何獲得這些信息反饋到保存的ListView的片段更新列表項的值。我試過
adapter.registerDataSetObserver
在片段中,但它不會對適配器內的任何更改做出反應。僅適用於片段本身的更改(如單擊添加新項目的按鈕)。
另外我想知道如何獲得對Adapter類中最新的ArrayList的引用,所以我可以將它返回給Fragment。
我希望我的問題是可以理解的,我是編程新手。
編輯:
這裏我片段使用ListView
public class SettingsWindow extends Fragment {
private ArrayList<IncentiveItem> mIncentiveList;
private OnFragmentInteractionListener mListener;
ListView incentiveList;
IncentiveAdapter adapter;
public SettingsWindow() {
// Required empty public constructor
}
public static SettingsWindow newInstance(ArrayList<IncentiveItem> incentiveList) {
SettingsWindow fragment = new SettingsWindow();
Bundle args = new Bundle();
args.putSerializable("Incentive List", incentiveList);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mIncentiveList = (ArrayList<IncentiveItem>) getArguments().getSerializable("Incentive List");
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View inf = inflater.inflate(R.layout.fragment_settings_window, container, false);
incentiveList = (ListView) inf.findViewById(R.id.incentive_list_xml);
adapter = new IncentiveAdapter(getActivity(), mIncentiveList);
incentiveList.setAdapter(adapter);
return inf;
}
// TODO: Rename method, update argument and hook method into UI event
public void onButtonPressed(Uri uri) {
if (mListener != null) {
mListener.onFragmentInteraction(uri);
}
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof OnFragmentInteractionListener) {
mListener = (OnFragmentInteractionListener) context;
} else {
throw new RuntimeException(context.toString()
+ " must implement OnFragmentInteractionListener");
}
}
@Override
public void onDetach() {
super.onDetach();
mListener = null;
}
/**
* This interface must be implemented by activities that contain this
* fragment to allow an interaction in this fragment to be communicated
* to the activity and potentially other fragments contained in that
* activity.
* <p>
* See the Android Training lesson <a href=
* "http://developer.android.com/training/basics/fragments/communicating.html"
* >Communicating with Other Fragments</a> for more information.
*/
public interface OnFragmentInteractionListener {
// TODO: Update argument type and name
void onFragmentInteraction(Uri uri);
}
public void addIncentive() {
mIncentiveList.add(new IncentiveItem());
adapter.notifyDataSetChanged();
}
}
這是適配器
public class IncentiveAdapter extends ArrayAdapter<IncentiveItem> {
public IncentiveAdapter(Activity context, ArrayList<IncentiveItem> incentiveList) {
super(context, 0, incentiveList);
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
View listItemView = convertView;
if (listItemView == null) {
listItemView = LayoutInflater.from(getContext()).inflate(R.layout.list_item, parent, false);
}
final IncentiveItem currentItem = getItem(position);
//We get references for the Views within the Incentive Item
final ImageView star = (ImageView) listItemView.findViewById(R.id.star_xml);
final EditText description = (EditText) listItemView.findViewById(R.id.incentive_text_xml);
SeekBar seekBar = (SeekBar) listItemView.findViewById(R.id.seekbar_xml);
final TextView percentage = (TextView) listItemView.findViewById(R.id.seekbar_percentage_xml);
star.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (currentItem.getActiveOrInactive() == false) {
currentItem.setActive();
star.setImageResource(R.drawable.ic_star_active);
} else {
currentItem.setInActive();
star.setImageResource(R.drawable.ic_star_inactive);;
}
}
});
description.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
currentItem.setText(description.getText().toString());
}
@Override
public void afterTextChanged(Editable editable) {
}
});
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean b) {
percentage.setText("" + progress + "%");
currentItem.setProbabilityInPercent(progress);
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
return listItemView;
}
}
請張貼相關的適配器和片段代碼。 – Stuckzilla
我真的不知道要發佈哪些部分要誠實。 –
發佈整個文件絕不會出錯。重新編輯你不想看到的任何敏感內容。 – Stuckzilla