i'm using this library for my project, everything is works fine(swiping elements), but my questions is - is it possible to start swiping animation by click on some ListView element?
and second question:
how can i implement timedSwipeUndo for custom object?
here is my code for ListView
final ArrayAdapter<Product> adapter = new MyListAdapter(this,R.layout.text_view,productsDef);
SimpleSwipeUndoAdapter simpleSwipeUndoAdapter = new SimpleSwipeUndoAdapter(adapter, this, new MyOnDismissCallback(adapter));
AlphaInAnimationAdapter animAdapter = new AlphaInAnimationAdapter(simpleSwipeUndoAdapter);
animAdapter.setAbsListView(listView);
assert animAdapter.getViewAnimator() != null;
animAdapter.getViewAnimator().setInitialDelayMillis(INITIAL_DELAY_MILLIS);
listView.setAdapter(animAdapter);
/* Enable drag and drop functionality */
listView.enableDragAndDrop();
listView.setDraggableManager(new TouchViewDraggableManager(R.id.list_row_draganddrop_touchview));
listView.setOnItemMovedListener(new MyOnItemMovedListener(adapter));
listView.setOnItemLongClickListener(new MyOnItemLongClickListener(listView));
/* Enable swipe to dismiss */
listView.enableSimpleSwipeUndo();
/* Add new items on item click */
//listView.setOnItemClickListener(new MyOnItemClickListener(listView));
In Doc is - for timedUndoAdapter use TimedUndoAdapter. but when i trying create adapter i have error:
Error:(249, 51) error: constructor TimedUndoAdapter in class TimedUndoAdapter cannot be applied to given types;
required: V,Context,OnDismissCallback
found: ArrayAdapter<Product>,DefaultProductsListUserEdit,DefaultProductsListUserEdit.MyOnDismissCallback
reason: inferred type does not conform to declared bound(s)
inferred: ArrayAdapter<Product>
bound(s): BaseAdapter,UndoAdapter
where V is a type-variable:
V extends BaseAdapter,UndoAdapter declared in constructor <V>TimedUndoAdapter(V,Context,OnDismissCallback)
i can't use TimedAdapter for custom object or i must override some method?
here is myAdapter:
public class MyListAdapter extends ArrayAdapter<Product> implements UndoAdapter {
private final Context mContext;
HashMap<Product, Integer> mIdMap = new HashMap<Product, Integer>();
ArrayList<Product> products = new ArrayList<Product>();
final int INVALID_ID = -1;
LayoutInflater lInflater;
public MyListAdapter(Context context, int textViewResourceId, List<Product> prod) {
//super(context, textViewResourceId, prod);
super(prod);
lInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mContext = context;
for (int i = 0; i < prod.size(); i++) {
//add(prod.get(i));
mIdMap.put(prod.get(i),i);
}
}
#Override
public long getItemId(final int position) {
//return getItem(position).hashCode();
Product item = (Product) getItem(position);
return mIdMap.get(item);
}
#Override
public boolean hasStableIds() {
return true;
}
#Override
public View getView(final int position, final View convertView, final ViewGroup parent) {
//TextView view = (TextView) convertView;
View view = convertView;
if (view == null) {
//view = (TextView) LayoutInflater.from(mContext).inflate(R.layout.list_row, parent, false);
view = lInflater.inflate(R.layout.item, parent, false);
}
Product p = getItem(position);
//view.setText(getItem(position).getProductName());
((TextView) view.findViewById(R.id.tvDescr)).setText(p.getProductName());
return view;
}
#NonNull
#Override
public View getUndoView(final int position, final View convertView, #NonNull final ViewGroup parent) {
View view = convertView;
if (view == null) {
//view = LayoutInflater.from(mContext).inflate(R.layout.undo_row, parent, false);
view = lInflater.inflate(R.layout.undo_row, parent, false);
}
return view;
}
#NonNull
#Override
public View getUndoClickView(#NonNull final View view) {
return view.findViewById(R.id.undo_row_undobutton);
}
public View getHeaderView(final int position, final View convertView, final ViewGroup parent) {
TextView view = (TextView) convertView;
//View view = convertView;
if (view == null) {
//view = (TextView) LayoutInflater.from(mContext).inflate(R.layout.list_header, parent, false);
//view = lInflater.inflate(R.layout.list_header, parent, false);
}
//view.setText(mContext.getString(R.string.header, getHeaderId(position)));
return view;
}
public long getHeaderId(final int position) {
return position / 10;
}
}
and class MyOnDismissCallback(adapter):
private class MyOnDismissCallback implements OnDismissCallback {
private final ArrayAdapter<Product> mAdapter;
#Nullable
private Toast mToast;
MyOnDismissCallback(final ArrayAdapter<Product> adapter) {
mAdapter = adapter;
}
#Override
public void onDismiss(#NonNull final ViewGroup listView, #NonNull final int[] reverseSortedPositions) {
for (int position : reverseSortedPositions) {
mAdapter.remove(position);
}
if (mToast != null) {
mToast.cancel();
}
mToast = Toast.makeText(
DefaultProductsListUserEdit.this,
"REMOVED ITEM",
Toast.LENGTH_LONG
);
mToast.show();
}
}
Thx in advance!
Related
I just created a listview and I can see all the rows at my listview exept to the first one. anyone got any idea why this is happend. my adapter code is:
public class ChatAdapter extends ArrayAdapter<Message> {
public ChatAdapter(Context context, int resource, List<Message> items) {
super(context, resource, items);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi = LayoutInflater.from(getContext());
v = vi.inflate(R.layout.chat_raw, null);
}
Message message = getItem(position);
Log.d("GET VIEW MESSAGE: ", message.getText());
if (message != null) {
TextView tvName = (TextView) v.findViewById(R.id.tvChatName);
TextView tvMessage = (TextView) v.findViewById(R.id.tvChatMessage);
if (tvName != null) {
tvName.setText(message.getUserName());
}
if (tvMessage != null) {
tvMessage.setText(message.getText());
}
}
return v;
}
}
I must say that at line: Log.d("GET VIEW MESSAGE: ", message.getText()); I see all the messages and also the 1st one but at the listview I don't see the 1st one.
edit:
Just change the code according to what you seggest but I still have the same problem, I can't see the 1st row. my adapter code is:
public class ChatAdapter extends ArrayAdapter<Message> {
private Context context;
public ChatAdapter(Context context, int resource, List<Message> items) {
super(context, resource, items);
this.context = context;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
ViewHolder viewHolder=null;
if(convertView==null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.chat_raw, null);
viewHolder = new ViewHolder(row);
row.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) row.getTag();
}
Message message = getItem(position);
if(message!=null) {
viewHolder.tvMessage.setText(message.getText());
viewHolder.tvName.setText(message.getUserName());
}
return row;
}
public class ViewHolder {
TextView tvName;
TextView tvMessage;
public ViewHolder(View view) {
tvName = (TextView) view.findViewById(R.id.tvChatName);
tvMessage = (TextView) view.findViewById(R.id.tvChatMessage);
}
}
}
and my code is:
public class ChatFragment extends Fragment {
private ListView lvChat;
private Button btChatSend;
private EditText etChatMessage;
private ArrayList<Message> alMessage;
private int groupID, userId;
private String userName;
private RequestQueue requestQueue;
private Gson gson;
public ChatFragment() {
// Required empty public constructor
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
alMessage = new ArrayList<>();
groupID = ((DrawAndChat) getActivity()).getGroupId();
userName = ((DrawAndChat) getActivity()).getUserName();
userId = ((DrawAndChat) getActivity()).getUserId();
requestQueue = Volley.newRequestQueue(getActivity());
getAllMessages();
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.chat_fragment, container, false);
lvChat = (ListView) view.findViewById(R.id.lvChat);
etChatMessage = (EditText) view.findViewById(R.id.etChatMessage);
btChatSend = (Button) view.findViewById(R.id.btChatSend);
lvChat.setAdapter(new ChatAdapter(getActivity(), R.layout.chat_raw, alMessage));
btChatSend.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
sendMessage();
}
});
return view;
}
I have a list in which i can assigned the values statically.
private List<ListData> mDataList = Arrays.asList(
new ListData("Arun"),
new ListData("Jega"),
new ListData("Kabilan"),
new ListData("Karthick"),
new ListData("Joushva"),
new ListData("Niranjana"),
new ListData("Paramesh"),
new ListData("Prabha"),new ListData("Test1"),new ListData("Test2") );
The problem is now i got a scenario where i should get these value dynamically from web apim and i have to set it to the List variable mDataList.
Please help me to clear this. Thanks in advance.
Here is my complete code.
public class ReportFragment extends Fragment {
View ParentView;
private static final int HIGHLIGHT_COLOR = 0x999be6ff;
// list of data items
private List<ListData> mDataList = Arrays.asList(
new ListData("Arun"),
new ListData("Jega"),
new ListData("Kabilan"),
new ListData("Karthick"),
new ListData("Joushva"),
new ListData("Niranjana"),
new ListData("Paramesh"),
new ListData("Prabha"),new ListData("Test1"),new ListData("Test2")
);
// declare the color generator and drawable builder
private ColorGenerator mColorGenerator = ColorGenerator.MATERIAL;
private TextDrawable.IBuilder mDrawableBuilder;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
ParentView= inflater.inflate(R.layout.report_fragment, container, false);
init();
return ParentView;
}
public void init(){
mDrawableBuilder = TextDrawable.builder()
.round();
// init the list view and its adapter
final ListView listView = (ListView) ParentView.findViewById(R.id.listView1);
listView.setAdapter(new SampleAdapter());
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
}
});
}
private class SampleAdapter extends BaseAdapter {
#Override
public int getCount() {
return mDataList.size();
}
#Override
public ListData getItem(int position) {
return mDataList.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
if (convertView == null) {
convertView = View.inflate(getActivity() , R.layout.list_item_layout, null);
holder = new ViewHolder(convertView);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
ListData item = getItem(position);
// provide support for selected state
updateCheckedState(holder, item);
holder.imageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// when the image is clicked, update the selected state
ListData data = getItem(position);
data.setChecked(!data.isChecked);
updateCheckedState(holder, data);
}
});
holder.textView.setText(item.data);
return convertView;
}
private void updateCheckedState(ViewHolder holder, ListData item) {
if (item.isChecked) {
holder.imageView.setImageDrawable(mDrawableBuilder.build(" ", 0xff616161));
holder.view.setBackgroundColor(HIGHLIGHT_COLOR);
holder.checkIcon.setVisibility(View.VISIBLE);
}
else {
TextDrawable drawable = mDrawableBuilder.build(String.valueOf(item.data.charAt(0)), mColorGenerator.getColor(item.data));
holder.imageView.setImageDrawable(drawable);
holder.view.setBackgroundColor(Color.TRANSPARENT);
holder.checkIcon.setVisibility(View.GONE);
}
}
}
private static class ViewHolder {
private View view;
private ImageView imageView;
private TextView textView;
private ImageView checkIcon;
private ViewHolder(View view) {
this.view = view;
imageView = (ImageView) view.findViewById(R.id.imageView);
textView = (TextView) view.findViewById(R.id.textView);
checkIcon = (ImageView) view.findViewById(R.id.check_icon);
}
}
private static class ListData {
private String data;
private boolean isChecked;
public ListData(String data) {
this.data = data;
}
public void setChecked(boolean isChecked) {
this.isChecked = isChecked;
}
}
}
create a method to add items in your adapter class, this method will add item to
itemlist. this will add items dynamically on your list or grid.
e.g
public void addItem(Item item){
itemList.add(item);
notifydatasetchanged();}
create a adapter class and constructor, in constructor just initialize the item arraylist and create extra method called addItem() this method will add item to your arraylist. after that you just have to call notifyDatasetchanged and your new item will be added.
I'm using a GridView inside an ExpandableListView
I have a function that highlights an item when it is clicked, now I'm trying to implement a button that when pressed it will unselect all selected items, but it is unselecting only the last view clicked
public class GridAdapter extends BaseAdapter {
private Context mContext;
private ArrayList<Filhos> child;
public ArrayList<CadastraEscolas> escola;
private ArrayList<ArrayList<Filhos>> filhos = new ArrayList();
public GridAdapter(Context context, ArrayList<CadastraEscolas> groups, ArrayList<Filhos> childValues, int groupPosition) {
mContext = context;
child = childValues;
escola = groups;
posicaoEscola = groupPosition;
}
#Override
public int getCount() {
return child.size();
}
#Override
public Object getItem(int position) {
return position;
}
#Override
public long getItemId(int arg0) {
return 0;
}
#Override
public View getView(final int position, View convertView, final ViewGroup parent) {
holder = null;
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.child_item, null);
holder = new ViewHolder();
final TextView idAluno = (TextView) convertView.findViewById(R.id.idcrianca);
final TextView nomeAluno = (TextView) convertView.findViewById(R.id.name);
convertView.setTag(holder);
final View finalConvertView = convertView;
convertView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (list.size() > 0) {
isChecked = filhos.get(posicaoEscola).get(position).isChecked();
if (!isChecked) {
selecao(true, position, nomeAluno, 0xFFFFFFFF, finalConvertView, View.VISIBLE);
} else {
selecao(false, position, nomeAluno, 0xFFD5672B, finalConvertView, View.GONE);
}
ex.findViewById(R.id.notificar).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(final View v) {
limpaSelecao(false, position, nomeAluno, 0xFFD5672B, parent, View.GONE);
}
});
}
});
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.text.setText(child.get(position).getNome());
return convertView;
}
static class ViewHolder {
TextView text;
}
public void selecao(boolean check, int position, TextView nomeAluno, int color, View v, int visibility) {
filhos.get(posicaoEscola).get(position).setChecked(check);
nomeAluno.setTextColor(color);
v.findViewById(R.id.overlay).setVisibility(visibility);
v.findViewById(R.id.overlayText).setVisibility(visibility);
}
public void limpaSelecao(boolean check, int position, TextView nomeAluno, int color, ViewGroup v, int visibility) {
for (int x = 0; x < group.size(); x++) {
for (int j = 0; j < group.get(x).getalunos().size(); j++) {
if(filhos.get(x).get(j).isChecked()){
v.getChildAt(j).findViewById(R.id.loadingPanel).findViewById(R.id.overlay).setVisibility(View.GONE);
}
nomeAluno.setTextColor(color);
}
}
}
}
Layout:
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#drawable/nomealuno_main"
android:layout_below="#+id/child">
<View
android:id="#+id/overlay"
android:layout_width="125dp"
android:layout_height="50dp"
android:background="#color/bgOverlay"
android:visibility="gone"/>
</RelativeLayout>
here is what is happening:
when I select all the items from A to B like that:
And hit clear, it will only remove the selected children from the last group that I clicked:
I need to remove them all when I click on Clear, all the children no matter in what group they are have to be removed.
ExpandableAdapter:
public class ExpandListTest extends BaseExpandableListAdapter {
public static final int CHOICE_MODE_MULTIPLE = AbsListView.CHOICE_MODE_MULTIPLE;
public static final int CHOICE_MODE_MULTIPLE_MODAL = AbsListView.CHOICE_MODE_MULTIPLE_MODAL;
/**
* No child could be selected
*/
public static final int CHOICE_MODE_NONE = AbsListView.CHOICE_MODE_NONE;
/**
* One single choice per group
*/
public static final int CHOICE_MODE_SINGLE_PER_GROUP = AbsListView.CHOICE_MODE_SINGLE;
/**
* One single choice for all the groups
*/
public static final int CHOICE_MODE_SINGLE_ABSOLUTE = 10001;
private Context context;
public static ArrayList<CadastraEscolas> groups;
private ArrayList<ArrayList<Filhos>> child = new ArrayList();
private HashMap<String, GPSEscolas> aMap = new HashMap<String, GPSEscolas>();
private HashMap<String, GPSEscolas> HashTask = new HashMap<String, GPSEscolas>();
public static ArrayList<Filhos> listchild;
private GridAdapter adapter;
private SecurePreferences Sessao;
public static CustomGridView gridView;
private SparseArray<SparseBooleanArray> checkedPositions;
private static final String LOG_TAG = ExpandListAdapter.class.getSimpleName();
public ExpandListTest(Context context, ArrayList<CadastraEscolas> groups, HashMap<String, GPSEscolas> data, SecurePreferences mSessao, HashMap<String, GPSEscolas> hashTask) {
this.context = context;
this.groups = groups;
this.aMap = data;
this.Sessao = mSessao;
checkedPositions = new SparseArray<SparseBooleanArray>();
child = new ArrayList();
if (groups != null) {
for (int i = 0; i < groups.size(); i++) {
child.add(i, groups.get(i).getalunos());
}
}
}
#Override
public Object getChild(int groupPosition, int childPosition) {
return child.get(childPosition);
}
#Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
#Override
public View getChildView(final int groupPosition, final int childPosition,
boolean isLastChild, View convertView, final ViewGroup parent) {
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) context
.getSystemService(context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.gridview, null);
}
listchild = new ArrayList<Filhos>();
for (int j = 0; j < groups.get(groupPosition).getalunos().size(); j++) {
listchild.add(child.get(groupPosition).get(j));
}
gridView = (CustomGridView) convertView.findViewById(R.id.GridView_toolbar);
gridView.setExpanded(true);
adapter = new GridAdapter(context, groups, listchild, child, groupPosition, aMap, HashTask, Sessao);
gridView.setAdapter(adapter);// Adapter
gridView.setChoiceMode(CustomGridView.CHOICE_MODE_MULTIPLE);
return convertView;
}
#Override
public int getChildrenCount(int nGroup) {
return 1;
}
#Override
public Object getGroup(int groupPosition) {
return groups.get(groupPosition);
}
#Override
public int getGroupCount() {
return groups.size();
}
#Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
#Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
CadastraEscolas group = (CadastraEscolas) getGroup(groupPosition);
if (convertView == null) {
LayoutInflater inf = (LayoutInflater) context
.getSystemService(context.LAYOUT_INFLATER_SERVICE);
convertView = inf.inflate(R.layout.group_item, null);
}
ExpandableListView mExpandableListView = (ExpandableListView) parent;
mExpandableListView.expandGroup(groupPosition);
TextView tv = (TextView) convertView.findViewById(R.id.group_name);
tv.setText(group.getNome_fantasia());
return convertView;
}
#Override
public boolean hasStableIds() {
return true;
}
#Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
Android ViewGroups can contain any number of Views, but Views can only have one parent ViewGroup, if you try to add a View that already has a parent, you will get this exception (from ViewGroup.addViewInner()):
throw new IllegalStateException("The specified child already has a parent. " +
"You must call removeView() on the child's parent first.");
This means that the structure of your layouts is that of a tree (not a graph), and so you can use any tree traversal algorithm to iterate through every View of your layout.
The following is one of the possible algorithms, which is a post-order traversal.
It goes through the root element, takes the first child and makes a recursive call for the algorithm, then its second, third etc...when there are no children left to go through, it calls your deselect function for the node.
For the tree in the diagram, nodes will be unselected in this order:
A,C,E,D,B,H,I,G, F
public void onClickTheButton(View view) {
unselectall(R.layout.your_layout);
}
public void unselectAll(View view) {
if(view instanceof ViewGroup) {
for(int ii = 0 ; ii<(ViewGroup)view.getChildrenCount(); ii++) {
unselectAll((ViewGroup)view.getChildAt(ii));
}
}
unselect(view);
}
You can find many other ways to do it here: https://en.wikipedia.org/wiki/Tree_traversal
It is a must to learn how those algorithms work if you want to ease your programming experience.
change your getView() method as follows.
#Override
public View getView(final int position, View convertView, final ViewGroup parent) {
holder = null;
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.child_item, null);
holder = new ViewHolder();
final TextView idAluno = (TextView) convertView.findViewById(R.id.idcrianca);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.text.setText(child.get(position).getNome());
final View finalConvertView = convertView;
convertView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (list.size() > 0) {
isChecked = filhos.get(posicaoEscola).get(position).isChecked();
if (!isChecked) {
selecao(true, position, nomeAluno, 0xFFFFFFFF, finalConvertView, View.VISIBLE);
} else {
selecao(false, position, nomeAluno, 0xFFD5672B, finalConvertView, View.GONE);
}
ex.findViewById(R.id.notificar).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(final View v) {
limpaSelecao(false, position, nomeAluno, 0xFFD5672B, parent, View.GONE);
}
});
}
});
return convertView;
}
I've been trying set custom ListView adapter for navigation drawer it derived from BaseAdapter so when I set OnItemClickListener it doesn't work on either ListView.OnItemClickListener and AdapterView.OnItemClickListener.
Googled around but all examples derive from ArrayAdapter since BaseAdapter desired.
Can anyone help me on this?
NavigationDrawer fragment onCreateView
drawerItems = new ArrayList<DrawerItem>();
drawerItems.add(DrawerItem.create(1, getString(R.string.ideas), getDrawable(R.drawable.ic_action_diamond)));
drawerItems.add(DrawerItem.create(2, getString(R.string.categories), getDrawable(R.drawable.ic_action_categories)));
drawerItems.add(DrawerItem.create(3, getString(R.string.priorities), getDrawable(R.drawable.ic_action_priorities)));
mDrawerListAdapter = MenuAdapter.newInstance(getActivity(), drawerItems);
mDrawerListView.setItemsCanFocus(true);
mDrawerListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Log.d(TAG, String.valueOf(position));
selectItem(position);
}
});
mDrawerListView.setAdapter(mDrawerListAdapter);
mDrawerListView.setItemChecked(mCurrentSelectedPosition, true);
My custom adapter
public class MenuAdapter extends BaseAdapter {
private static final String TAG = "AD:MENU";
private final LayoutInflater mLayoutInflater;
private List<DrawerItem> items;
private Context context;
public MenuAdapter(Context ctx, List<DrawerItem> items) {
this.items = items;
this.context = ctx;
this.mLayoutInflater = LayoutInflater.from(ctx);
}
public static MenuAdapter newInstance(Context ctx, List<DrawerItem> items) {
return new MenuAdapter(ctx, items);
}
#Override
public int getCount() {
return items == null ? 0 : items.size();
}
#Override
public Object getItem(int position) {
return items.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View v, ViewGroup parent) {
DrawerItem item = (DrawerItem) getItem(position);
ViewHolder holder;
if (v == null) {
v = mLayoutInflater.inflate(R.layout.drawable_item, parent, false);
holder = new ViewHolder();
if (v != null) {
holder.title = (TextView) v.findViewById(R.id.menu_title);
holder.title.setTag(item.id);
v.setTag(holder);
}
} else {
holder = (ViewHolder) v.getTag();
}
holder.title.setText(item.title);
holder.title.setCompoundDrawablesWithIntrinsicBounds(item.icon, null, null, null);
Log.d(TAG, item.title);
return v;
}
public class ViewHolder {
TextView title;
}
}
here is the code for custom listview using BaseAdapter in android its working fine:
public class CustomListAdapter extends BaseAdapter {
private ArrayList<TaskClass> _listData;
Context _c;
public CustomListAdapter(Context context, ArrayList<TaskClass> listData) {
_listData = listData;
_c = context;
}
#Override
public int getCount() {
return _listData.size();
}
#Override
public Object getItem(int position) {
return _listData.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
public View getView(int position,View convertView,ViewGroup parent) {
View v = convertView;
if (v == null)
{
LayoutInflater layoutInflator = (LayoutInflater) _c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = layoutInflator.inflate(R.layout.listview_row, null);
}
TextView titleText = (TextView)v.findViewById(R.id.holdTitleText);
TextView catText = (TextView)v.findViewById(R.id.holdCatText);
TextView descText = (TextView)v.findViewById(R.id.holdDescText);
TextView dateText = (TextView)v.findViewById(R.id.holdDateText);
//CheckBox checkBoxForEachItem = (CheckBox)v.findViewById(R.id.)
TaskClass taskClassInstance = _listData.get(position);
titleText.setText(taskClassInstance.getTitle());
catText.setText(taskClassInstance.getTaskCategory());
descText.setText(taskClassInstance.getDescription());
dateText.setText(taskClassInstance.getTaskDate());
return v;
}
}
and in activity i m binding listview with custom adapter :
listViewInstance.setAdapter(new CustomListAdapter(getApplicationContext(),taskClasslistInstance));
where "taskclasslistinstance" is my arraylist conatining data from DB its working fine
now i need to write function for listitemclick so that when user click on any listitem i can get rowid of that listitem record from Database.so after getting rowwid I can delete records from listview and from db and can edit informations
Well, we don't know about your TaskClass, but I expect you want something like this:
listViewInstance.setOnItemClickListener(new OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> parent, View view, int pos, long id) {
TaskClass taskClass = taskClasslistInstance.get(pos);
Log.d(TAG, "Clicked on: " + taskClass)
// Do stuff with taskClass
}});
Here a small example from my project:
list = (ListView) rootView.findViewById(R.id.list);
adapter = new LazyAdapterAlbum(getActivity(), songs);
list.setAdapter(adapter);
list.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent i = new Intent(getActivity(), AlbumActivity.class);
Bundle args = new Bundle();
args.putString(AlbumActivity.ALBUM_NAME, adapter.getItem(position).getName());
i.putExtras(args);
startActivity(i);
}
});
You have to set an onClickListener on your ListView.
you can also set in getView method like below:
public View getView(int position,View convertView,ViewGroup parent) {
View v = convertView;
if (v == null)
{
LayoutInflater layoutInflator = (LayoutInflater) _c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = layoutInflator.inflate(R.layout.listview_row, null);
}
TextView titleText = (TextView)v.findViewById(R.id.holdTitleText);
TextView catText = (TextView)v.findViewById(R.id.holdCatText);
TextView descText = (TextView)v.findViewById(R.id.holdDescText);
TextView dateText = (TextView)v.findViewById(R.id.holdDateText);
//CheckBox checkBoxForEachItem = (CheckBox)v.findViewById(R.id.)
TaskClass taskClassInstance = _listData.get(position);
titleText.setText(taskClassInstance.getTitle());
catText.setText(taskClassInstance.getTaskCategory());
descText.setText(taskClassInstance.getDescription());
dateText.setText(taskClassInstance.getTaskDate());
v.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
//Perform an action when this list item is clicked
}
});
return v;
}