Android: 从适配器获取信息
Android: Getting information out of an Adapter
我有一个连接到 CustomAdapter 的 ListView。在适配器 class 中,我有不同视图的侦听器。当某些内容发生变化时,它会更新
上的 ListItem 的值
currentItem.myListItemMethod()
现在我不知道如何将该信息返回到包含 ListView 的片段中。我试过了
adapter.registerDataSetObserver
在片段中,但它不会对适配器中的任何更改做出反应。仅对片段本身进行更改(例如单击添加新项目的按钮)。
我还想知道如何在适配器 class 中获取对最新 ArrayList 的引用,这样我就可以 return 将其引用到片段中。
我希望我的问题是可以理解的,我是编程新手。
编辑:
这是我的 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;
}
}
您可以在 Adapter Class 中创建一个 interface
,您可以在片段中实现该接口并将其设置为 Adapter 这样,每当适配器发生任何变化时,您都可以通知片段。
Also i want to know how i can get a reference to the most up-to-date
ArrayList within the Adapter class, so i can return it to the Fragment
为此,我们可以使用默认方法getItem(position)
所以在适配器中创建这样的方法class,你可以获得完整的列表
public ArrayList<IncentiveItem> getItemValues() {
ArrayList<IncentiveItem> incentiveList = new ArrayList<IncentiveItem>();
for (int i=0 ; i < getCount() ; i++){
IncentiveItem incentiveItem = getItem(i);
incentiveList.add(incentiveItem);
}
return incentiveList;
}
已编辑
在 Adapter class
中创建一个这样的接口
public interface OnAdapterItemActionListener {
public void onStarItemClicked(int position);
public void onSeekBarChage(int position, int progress);
//.. and you can add more method like this
}
在适配器的构造函数中添加此接口,并将该对象保存在适配器中
public class IncentiveAdapter extends ArrayAdapter<IncentiveItem> {
private OnAdapterItemActionListener mListener;
public IncentiveAdapter(Activity context, ArrayList<IncentiveItem> incentiveList, OnAdapterItemActionListener listener) {
super(context, 0, incentiveList);
mListener = listener;
}
}
现在在你的片段中,你应该像下面这样实现这个接口
public class SettingsWindow extends Fragment implements OnAdapterItemActionListener{
@Overrride
public void onStarItemClicked(int position) {
// You will get callback here when click in adapter
}
@Overrride
public void onSeekBarChage(int position, int progress) {
// You will get callback here when seekbar changed
}
}
并且在创建适配器对象时,您也应该发送接口实现,因为在适配器构造函数中我们期望如此,所以更改此设置,
adapter = new IncentiveAdapter(getActivity(), mIncentiveList, this); // this - Because we implemented in this class
以上代码将完成界面设置,现在我们应该像下面这样在正确的时间触发界面,
当用户点击星形按钮时,我们应该这样触发,
star.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//Your code here
if(mListener != null) { //Just for safety check
mListener.onStarItemClicked(position);// this will send the callback to your Fragment implementation
}
}
});
我有一个连接到 CustomAdapter 的 ListView。在适配器 class 中,我有不同视图的侦听器。当某些内容发生变化时,它会更新
上的 ListItem 的值currentItem.myListItemMethod()
现在我不知道如何将该信息返回到包含 ListView 的片段中。我试过了
adapter.registerDataSetObserver
在片段中,但它不会对适配器中的任何更改做出反应。仅对片段本身进行更改(例如单击添加新项目的按钮)。
我还想知道如何在适配器 class 中获取对最新 ArrayList 的引用,这样我就可以 return 将其引用到片段中。
我希望我的问题是可以理解的,我是编程新手。
编辑:
这是我的 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;
}
}
您可以在 Adapter Class 中创建一个 interface
,您可以在片段中实现该接口并将其设置为 Adapter 这样,每当适配器发生任何变化时,您都可以通知片段。
Also i want to know how i can get a reference to the most up-to-date ArrayList within the Adapter class, so i can return it to the Fragment
为此,我们可以使用默认方法getItem(position)
所以在适配器中创建这样的方法class,你可以获得完整的列表
public ArrayList<IncentiveItem> getItemValues() {
ArrayList<IncentiveItem> incentiveList = new ArrayList<IncentiveItem>();
for (int i=0 ; i < getCount() ; i++){
IncentiveItem incentiveItem = getItem(i);
incentiveList.add(incentiveItem);
}
return incentiveList;
}
已编辑 在 Adapter class
中创建一个这样的接口public interface OnAdapterItemActionListener {
public void onStarItemClicked(int position);
public void onSeekBarChage(int position, int progress);
//.. and you can add more method like this
}
在适配器的构造函数中添加此接口,并将该对象保存在适配器中
public class IncentiveAdapter extends ArrayAdapter<IncentiveItem> {
private OnAdapterItemActionListener mListener;
public IncentiveAdapter(Activity context, ArrayList<IncentiveItem> incentiveList, OnAdapterItemActionListener listener) {
super(context, 0, incentiveList);
mListener = listener;
}
}
现在在你的片段中,你应该像下面这样实现这个接口
public class SettingsWindow extends Fragment implements OnAdapterItemActionListener{
@Overrride
public void onStarItemClicked(int position) {
// You will get callback here when click in adapter
}
@Overrride
public void onSeekBarChage(int position, int progress) {
// You will get callback here when seekbar changed
}
}
并且在创建适配器对象时,您也应该发送接口实现,因为在适配器构造函数中我们期望如此,所以更改此设置,
adapter = new IncentiveAdapter(getActivity(), mIncentiveList, this); // this - Because we implemented in this class
以上代码将完成界面设置,现在我们应该像下面这样在正确的时间触发界面,
当用户点击星形按钮时,我们应该这样触发,
star.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//Your code here
if(mListener != null) { //Just for safety check
mListener.onStarItemClicked(position);// this will send the callback to your Fragment implementation
}
}
});