我创建了一个类PickedDropsAdapter,它扩展了ArrayAdapter,用于列出列表中的对象的listView
添加和删除基本上工作正常,但getView中的位置始终为0,我只是无法弄清楚,为什么。
我发现了一些类似的问题,但没有一个答案真的有帮助
也许重要的是,listView是一个Fragment,它再次出现在一个有两页的ViewPager中。
PickedDropsAdapter.java
public class PickedDropsAdapter extends ArrayAdapter {
private LayoutInflater myInflater;
private List<PickedDrop> pickedList;
public PickedDropsAdapter(Context context, int resource, List objects){
super(context, resource, objects);
myInflater = LayoutInflater.from(context);
setData(objects);
}
public void setData(List list){
this.pickedList = list;
}
@Override
public int getPosition(Object item) {
return super.getPosition(item);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
TextView profileName;
if (convertView == null) {
convertView = myInflater.inflate(R.layout.picked_drop, null);
profileName = (TextView)convertView.findViewById(R.id.profileName);
Log.i("TAG", "Name in adapter: " + pickedList.get(position).name);
Log.i("TAG", "Postion in adapter: " + position);
profileName.setText(pickedList.get(position).name);
pickedList.get(position).makeActive(convertView);
}
return convertView;
}
}
PickedDrop
public class PickedDrop extends Fragment {
int id, listLoc;
String name;
RelativeLayout pickedLayout;
PickedDropsFrag parentFrag = null;
public static PickedDrop newInstance(String nameParam, int idParam) {
final PickedDrop fragment = new PickedDrop();
fragment.name = nameParam;
Log.i("TAG", "Drop name: " + fragment.name);
fragment.id = idParam;
return fragment;
}
public PickedDrop() {
// Required empty public constructor
}
public void openDrop(){
MainActivity mainActivity = (MainActivity)parentFrag.getActivity();
mainActivity.openDrop(this);
}
public void openDrop(int i){
listLoc = i;
MainActivity mainActivity = (MainActivity)parentFrag.getActivity();
mainActivity.openDrop(this);
}
public void makeActive(View convertView){
pickedLayout = (RelativeLayout)convertView.findViewById(R.id.pickedLayout);
}
}
PickedDropsFrag的一部分(包含listView的片段)
public class PickedDropsFrag extends Fragment {
private OnFragmentInteractionListener mListener;
PickedDropsAdapter pickedAdapter;
ListView pickedListView;
List<PickedDrop> pickedList;
public PickedDropsFrag() {
// Required empty public constructor
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
pickedListView = (ListView)getView().findViewById(R.id.pickedListView);
pickedListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
PickedDrop drop = (PickedDrop) adapterView.getItemAtPosition(i);
drop.openDrop(i);
}
});
pickedList = new ArrayList<>();
pickedAdapter = new PickedDropsAdapter(
getActivity(),
R.layout.picked_drop,
pickedList
);
pickedListView.setAdapter(pickedAdapter);
}
public void addToPicked(PickedDrop drop){
drop.parentFrag = this;
//pickedList.add(drop);
pickedAdapter.add(drop);
drop.listLoc = pickedList.size() - 1;
//pickedAdapter.notifyDataSetChanged();
drop.openDrop();
}
这应该是重要的一切,我希望我没有忘记任何事情。
答案 0 :(得分:1)
更新适配器中的以下方法:
public class PickedDropsAdapter extends ArrayAdapter {
@Override
public int getCount() {
return pickedList.size();
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
TextView profileName;
if (convertView == null) {
convertView = myInflater.inflate(R.layout.picked_drop, null);
}
profileName = (TextView)convertView.findViewById(R.id.profileName);
Log.i("TAG", "Name in adapter: " + pickedList.get(position).name);
Log.i("TAG", "Postion in adapter: " + position);
profileName.setText(pickedList.get(position).name);
pickedList.get(position).makeActive(convertView);
return convertView;
}
}