mirror of
https://github.com/commons-app/apps-android-commons.git
synced 2025-10-27 12:53:55 +01:00
Copy getView() method and move CategoriesAdapter to new class
This commit is contained in:
parent
6a6413bd3e
commit
8008c279c9
2 changed files with 107 additions and 62 deletions
|
|
@ -0,0 +1,107 @@
|
|||
package fr.free.nrw.commons.category;
|
||||
|
||||
|
||||
import android.content.Context;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.BaseAdapter;
|
||||
import android.widget.CheckedTextView;
|
||||
import android.widget.TextView;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.TreeSet;
|
||||
|
||||
import fr.free.nrw.commons.R;
|
||||
|
||||
public class CategoriesAdapter extends BaseAdapter {
|
||||
private Context context;
|
||||
private ArrayList<CategorizationFragment.CategoryItem> items;
|
||||
|
||||
private static final int TYPE_ITEM = 0;
|
||||
private static final int TYPE_SEPARATOR = 1;
|
||||
private TreeSet<Integer> sectionHeader = new TreeSet<Integer>();
|
||||
|
||||
private CategoriesAdapter(Context context, ArrayList<CategorizationFragment.CategoryItem> items) {
|
||||
this.context = context;
|
||||
this.items = items;
|
||||
}
|
||||
|
||||
public int getCount() {
|
||||
return items.size();
|
||||
}
|
||||
|
||||
public Object getItem(int i) {
|
||||
return items.get(i);
|
||||
}
|
||||
|
||||
public ArrayList<CategorizationFragment.CategoryItem> getItems() {
|
||||
return items;
|
||||
}
|
||||
|
||||
public void setItems(ArrayList<CategorizationFragment.CategoryItem> items) {
|
||||
this.items = items;
|
||||
}
|
||||
|
||||
public long getItemId(int i) {
|
||||
return i;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getItemViewType(int position) {
|
||||
// If type is 1, the line is a header, otherwise it is an item
|
||||
return sectionHeader.contains(position) ? TYPE_SEPARATOR : TYPE_ITEM;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getViewTypeCount() {
|
||||
return 2;
|
||||
}
|
||||
|
||||
public View getView(int i, View view, ViewGroup viewGroup) {
|
||||
CheckedTextView checkedView;
|
||||
|
||||
if(view == null) {
|
||||
checkedView = (CheckedTextView) getActivity().getLayoutInflater().inflate(R.layout.layout_categories_item, null);
|
||||
|
||||
} else {
|
||||
checkedView = (CheckedTextView) view;
|
||||
}
|
||||
|
||||
CategorizationFragment.CategoryItem item = (CategorizationFragment.CategoryItem) this.getItem(i);
|
||||
checkedView.setChecked(item.selected);
|
||||
checkedView.setText(item.name);
|
||||
checkedView.setTag(i);
|
||||
|
||||
return checkedView;
|
||||
}
|
||||
|
||||
// TODO: Separator getView reference
|
||||
public View getView(int position, View convertView, ViewGroup parent) {
|
||||
ViewHolder holder = null;
|
||||
int rowType = getItemViewType(position);
|
||||
|
||||
if (convertView == null) {
|
||||
holder = new ViewHolder();
|
||||
switch (rowType) {
|
||||
case TYPE_ITEM:
|
||||
convertView = mInflater.inflate(R.layout.snippet_item1, null);
|
||||
holder.textView = (TextView) convertView.findViewById(R.id.text);
|
||||
break;
|
||||
case TYPE_SEPARATOR:
|
||||
convertView = mInflater.inflate(R.layout.snippet_item2, null);
|
||||
holder.textView = (TextView) convertView.findViewById(R.id.textSeparator);
|
||||
break;
|
||||
}
|
||||
convertView.setTag(holder);
|
||||
} else {
|
||||
holder = (ViewHolder) convertView.getTag();
|
||||
}
|
||||
holder.textView.setText(mData.get(position));
|
||||
|
||||
return convertView;
|
||||
}
|
||||
|
||||
public static class ViewHolder {
|
||||
public TextView textView;
|
||||
}
|
||||
}
|
||||
|
|
@ -264,68 +264,6 @@ public class CategorizationFragment extends Fragment {
|
|||
}
|
||||
}
|
||||
|
||||
private class CategoriesAdapter extends BaseAdapter {
|
||||
private Context context;
|
||||
private ArrayList<CategoryItem> items;
|
||||
|
||||
private static final int TYPE_ITEM = 0;
|
||||
private static final int TYPE_SEPARATOR = 1;
|
||||
private TreeSet<Integer> sectionHeader = new TreeSet<Integer>();
|
||||
|
||||
private CategoriesAdapter(Context context, ArrayList<CategoryItem> items) {
|
||||
this.context = context;
|
||||
this.items = items;
|
||||
}
|
||||
|
||||
public int getCount() {
|
||||
return items.size();
|
||||
}
|
||||
|
||||
public Object getItem(int i) {
|
||||
return items.get(i);
|
||||
}
|
||||
|
||||
public ArrayList<CategoryItem> getItems() {
|
||||
return items;
|
||||
}
|
||||
|
||||
public void setItems(ArrayList<CategoryItem> items) {
|
||||
this.items = items;
|
||||
}
|
||||
|
||||
public long getItemId(int i) {
|
||||
return i;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getItemViewType(int position) {
|
||||
// If type is 1, the line is a header, otherwise it is an item
|
||||
return sectionHeader.contains(position) ? TYPE_SEPARATOR : TYPE_ITEM;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getViewTypeCount() {
|
||||
return 2;
|
||||
}
|
||||
|
||||
public View getView(int i, View view, ViewGroup viewGroup) {
|
||||
CheckedTextView checkedView;
|
||||
|
||||
if(view == null) {
|
||||
checkedView = (CheckedTextView) getActivity().getLayoutInflater().inflate(R.layout.layout_categories_item, null);
|
||||
|
||||
} else {
|
||||
checkedView = (CheckedTextView) view;
|
||||
}
|
||||
|
||||
CategoryItem item = (CategoryItem) this.getItem(i);
|
||||
checkedView.setChecked(item.selected);
|
||||
checkedView.setText(item.name);
|
||||
checkedView.setTag(i);
|
||||
|
||||
return checkedView;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Makes asynchronous calls to the Commons MediaWiki API via anonymous subclasses of
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue