I don't know why but my onItemClickmethod is not even calling. Well, it was working fine before adding the button(add button see adapter) on each row.
Please help me to find the bug,thanks
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Init();
//People list click
chatPeoples.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Log.i("click", "Clicking ");
PeopleObject p = peopleObjList.get(position);
String chattingToName = p.getPersonName();
String chattingToDeviceID = p.getRegId();
Intent intent = new Intent(getActivity(), ChatActivityNew.class);
intent.putExtra("chattingFrom",MyUsername);
intent.putExtra("chattingToName", chattingToName);
intent.putExtra("chattingToDeviceID", chattingToDeviceID);
startActivity(intent);
}
});
searchIcon.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (eSearch.getVisibility() == View.INVISIBLE) {
eSearch.setVisibility(View.VISIBLE);
}
}
});
// setMyUsername();
//getPeopleList();
}
Adapter Class
public class ChatAdapter extends BaseAdapter {
private Activity context;
private ArrayList<PeopleObject> chatPeoplesUsername;
public ChatAdapter(Activity cont, ArrayList<PeopleObject> cUsername ){
this.context = cont;
this.chatPeoplesUsername = cUsername;
}
static class ViewHolder {
protected TextView txUsername;
protected Button addFriend;
}
#Override
public int getCount() {
return chatPeoplesUsername.size();
}
#Override
public Object getItem(int position) {
return null;
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder = null;
if (convertView == null) {
LayoutInflater inflator = context.getLayoutInflater();
convertView = inflator.inflate(R.layout.chat_people_row, null);
viewHolder = new ViewHolder();
viewHolder.txUsername = (TextView) convertView
.findViewById(R.id.person_username);
viewHolder.addFriend = (Button)convertView.findViewById(R.id.btn_add_friend);
viewHolder.addFriend.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(context, "hm ..", Toast.LENGTH_SHORT).show();
}
});
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
if (chatPeoplesUsername != null) {
PeopleObject h = chatPeoplesUsername.get(position);
viewHolder.txUsername.setText(h.getPersonName());
}
return convertView;
}
}
You have added button on you every listItem right?
then just put this line in you button's xml file and all will work fine
android:focusable="false"
so button's xml should be like this
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="false" />
Reason behind this is button will take focus of all things when you click on listItem so there method will be called not list's click.
Move your click listner to getView() method
convertView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.i("click", "Clicking ");
//remaining code
}
});
You can not use clickable components like button and OnClickListener together.in such case your OnClickListener wont get called.
Related
This is a bit strange. Butterknife seems to be working with all other Views except my ViewFlipper. My ViewFlipper is on a ListView item. When you tap the ListView item, it flips over to display additional options. This doesnt happen anymore when I use #BindView to initialize my ViewFlipper, and the app doesnt crash, so no relevant logs. Code:
public class DepartmentListAdapter extends BaseAdapter {
Context context;
String[] departments;
#BindView(R.id.view_flipper_2) ViewFlipper flipper;
#BindView(R.id.departmentName) TextView departmentName;
#BindView(R.id.departmentOverviewButton) Button overViewButton;
#BindView(R.id.organizationalStructureButton) Button structureButton;
public DepartmentListAdapter(Context context, String[] departments){
this.context = context;
this.departments = departments;
}
#Override
public int getCount() {
return departments.length;
}
#Override
public Object getItem(int position) {
return departments[position];
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view;
if(convertView == null){
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.department_list_item, parent, false);
}else{
view = convertView;
}
ButterKnife.bind(this, view);
//this gets set
departmentName.setText(departments[position]);
flipper.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//this effect is gone, not sure onClick is even being called
AnimationFactory.flipTransition(flipper, AnimationFactory.FlipDirection.TOP_BOTTOM, 90);
}
});
overViewButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(context, "DO", Toast.LENGTH_SHORT).show();
}
});
structureButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(context, "DS", Toast.LENGTH_SHORT).show();
}
});
Animation animation = AnimationUtils.loadAnimation(
context, R.anim.listview_slide);
view.startAnimation(animation);
return view;
}
}
I am developing an Android app with a lot of ListView's. i created my own ListAdapter but i am not so happy with the OnItemClickListener. What is the best way to add a listener on an item if every item has other functionality?
You may try this, I'm using this Custom Adapter & OnClickListener()
public class CustomListAdapter extends BaseAdapter {
private Activity activity;
private LayoutInflater inflater;
private List<Jobs> jobsItems;
public CustomListAdapter(Activity activity, List<Jobs> jobsItems) {
this.activity = activity;
this.jobsItems = jobsItems;
}
#Override
public int getCount() {
return jobsItems.size();
}
#Override
public Object getItem(int location) {
return jobsItems.get(location);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (inflater == null)
inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null)
convertView = inflater.inflate(R.layout.custom_list_row_no_preview, null);
Button submit = (Button) convertView.findViewById(R.id.btnSubmit);
serial.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
convertView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
return convertView;
}
}
Similarly you can add any TextView, EditText or Button into your ListView and set onCickListener()
Firstly, sorry for my english, I am french.
So, I have a CustomArrayAdapter (ReservationAdapter), and I would remove an item from this class. I have tried many things, but they didn't work.
public class ReservationAdapter extends ArrayAdapter<Reservation> {
public ReservationAdapter(Context context, ArrayList<Reservation> reservations) {
super(context, 0, reservations);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// Get the data item for this position
Reservation reservation = getItem(position);
// Check if an existing view is being reused, otherwise inflate the view
ReservationHolder holder;
if (convertView == null) {
holder = new ReservationHolder();
LayoutInflater inflater = LayoutInflater.from(getContext());
convertView = inflater.inflate(R.layout.reservation_fragment, parent, false);
holder.date = (EditText) convertView.findViewById(R.id.dateReserv);
holder.imgDelete = (ImageButton) convertView.findViewById(R.id.less);
convertView.setTag(holder);
} else {
holder = (ReservationHolder) convertView.getTag();
}
// Populate the data into the template view using the data object
holder.date.setText(reservation.date);
holder.imgDelete.setOnClickListener(deleteReservListener);
// Return the completed view to render on screen
return convertView;
}
private View.OnClickListener deleteReservListener = new View.OnClickListener() {
#Override
public void onClick(View v) {
//Where I try to remove an item
notifyDataSetChanged();
}
};
public static class ReservationHolder {
private EditText date;
private ImageButton imgDelete;
}
You have to remove the item from reservations and then call notifyDataChanged().
Before of all you have to implement a OnItemClickListener on your ListView (outside of your adapter, of course):
AdapterView.OnItemClickListener itemCLickListener = new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
reservations.remove(i);
((ReservationAdapter)listView.getAdapter()).notifyDataChanged();
}
}
e voilĂ
It works, merci !
Meanwhile, I'm still looking for a solution, and have find this :
holder.date.setText(reservation.date);
holder.imgDelete.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
remove(reservation);
notifyDataSetChanged();
}
});
I just put the OnClickListener into the GetView method.
Thanks again ! :)
Add position vatiable in your ReservationHolder, and also construcror as:
public ReservationHolder(int position)
{
this.position = position;
}
public int getCurrentPosition()
{
return position;
}
and in getView(): ReservationHolder holder = new ReservationHolder(position);
Change your method:
private View.OnClickListener deleteReservListener = new View.OnClickListener() {
#Override
public void onClick(View v) {
//Where I try to remove an item
notifyDataSetChanged();
}
};
to:
private View.OnClickListener deleteReservListener = new View.OnClickListener() {
#Override
public void onClick(View v) {
//Where I try to remove an item
ReservationHolder holder = (ReservationHolder)v.getTag();
reservations.remove(holder.getCurrentPosition());
notifyDataSetChanged();
}
};
One of my activities requires a list view with custom number pickers in each row.
I created the number picker as suggested here. When I click on the buttons, however, the counter doesn't increment or decrement at all.
The following is the code found at my Custom ListView Adapter.
public class MaterialListViewAdapter extends BaseAdapter
{
ViewHolder holder;
int counter = 0;
private ArrayList<MaterialClass> data;
public static LayoutInflater inflater = null;
public static Dialog dialog;
String materialName;
public MaterialListViewAdapter(Context applicationContext,
int materialdialogcontent, ArrayList<MaterialClass> materials)
{
this.data = materials;
inflater = (LayoutInflater)applicationContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount()
{
return data.size();
}
#Override
public Object getItem(int position)
{
return position;
}
#Override
public long getItemId(int position)
{
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
View vi = convertView;
//ViewHolder holder = new ViewHolder();
if(vi == null)
{
holder = new ViewHolder();
vi = inflater.inflate(R.layout.materialdialogcontent, null);
//Initialize Buttons and TextViews.
holder.num.setText("0");
holder.add.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
counter++;
holder.num.setText("" + counter);
}
});
holder.sub.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
counter--;
holder.sub.setText("" + counter);
}
});
//holder.pk = (NumberPicker)vi.findViewById(R.id.npMaterialAmount);
vi.setTag(holder);
}
else
{
holder = (ViewHolder)vi.getTag();
}
holder.txt.setText(data.get(position).getName());
//holder.pk.setMaxValue(20);
//holder.pk.setMinValue(0);
return vi;
}
private class ViewHolder
{
TextView txt;
Button add;
Button sub;
TextView num;
//NumberPicker pk;
}
}
When I try debugging to see what's going on, counter doesn't even initialize with '0'.
I tried initializing the counter in the getView() method, however in order to do so I need to set the counter to final, and then another error shows up saying
The final local variable counter cannot be assigned, since it is defined in an enclosing type.
Any suggestions?
Updated for additional code:
public class Material extends Activity
{
ArrayList<String> materialList;
ListView lv;
Button btnConfirm;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main_material);
lv = (ListView)findViewById(R.id.list);
btnConfirm = (Button)findViewById(R.id.btnConfirm);
materialList = new ArrayList<String>();
Intent i = getIntent();
materialList = i.getStringArrayListExtra("materialList");
if(materialList != null)
{
Toast t = Toast.makeText(getApplicationContext(), materialList.get(0).toString(), Toast.LENGTH_LONG);
t.show();
}
else
{
//FILL HERE LATER
}
ArrayList<MaterialClass> materials = new ArrayList<MaterialClass>();
for(String temp : materialList)
{
MaterialClass m = new MaterialClass();
m.setName(temp);
materials.add(m);
}
MaterialListViewAdapter adapter = new MaterialListViewAdapter(getApplicationContext(), R.layout.materialdialogcontent, materials);
lv.setAdapter(adapter);
}
}
You should implement the onItemClickListener for your listView . You are using listener in adapter class but practically those numbers are nothing but items of that listView, so that to operate on those buttons (listView's items) you've not defined onItemClickListener. So after setting adapter write
lv.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> listView, View item, int position, long id) {
// here you are getting position of clicked item
}
}
UPDATE:
I've gone through your code again and I found these:
1.you are writing this line:
MaterialListViewAdapter adapter = new MaterialListViewAdapter(getApplicationContext(), R.layout.materialdialogcontent, materials);
but I didn't see any constructor in your Adapter.
2.As you are extending BaseAdapter, you must override getCount() , getItem(int position) and getItemId(int position) method as like getView() . There you are getting issue.
Try this :
public class MaterialListViewAdapter extends BaseAdapter
{
ViewHolder holder;
int counter = 0;
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
View vi = convertView;
//ViewHolder holder = new ViewHolder();
if(vi == null)
{
holder = new ViewHolder();
vi = inflater.inflate(R.layout.materialdialogcontent, null);
//Initialize Buttons and TextViews.
//holder.pk = (NumberPicker)vi.findViewById(R.id.npMaterialAmount);
vi.setTag(holder);
}
else
{
holder = (ViewHolder)vi.getTag();
}
holder.txt.setText(data.get(position).getName());
//holder.pk.setMaxValue(20);
//holder.pk.setMinValue(0);
holder.add.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
counter++;
holder.num.setText("" + counter);
}
});
holder.sub.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
counter--;
holder.num.setText("" + counter);
}
});
return vi;
}
private class ViewHolder
{
TextView txt;
Button add;
Button sub;
TextView num;
//NumberPicker pk;
}
}
I have a problem with clicking on the item in a custom ListView which contains ToggleButton, Button, TextView and Spinner in each row. The question: How can I do that when I click each view individually, different action is called for each view? My code:
public class CustomListAdapter extends BaseAdapter {
Context _context;
private ArrayList<String> _list;
public CustomListAdapter(Context context,
ArrayList<String> listItems) {
_context= context;
_list = listItems;
}
public int getCount(){
return _list.size();
}
public long getItemId(int position){
return position;
}
public Object getItem(int position){
return _list.get(position);
}
public View getView(int position,View convertView,ViewGroup parent)
{
ViewContainer container = new ViewContainer();
if(convertView == null)
{
LayoutInflater inflater = LayoutInflater.from(_context);
convertView = inflater.inflate(R.layout.elements,parent, false);
convertView.setTag(container);
convertView.setClickable(true);
container.listIcon = (ToggleButton)convertView.findViewById(R.id.togle_item);
container.listText = (TextView)convertView.findViewById(R.id.text_listitem);
container.spinner=(Spinner)convertView.findViewById(R.id.spinner1);
container.but=(Button)convertView.findViewById(R.id.button1);
convertView.setOnClickListener(new OnClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3)
{
**How can I do that when I click each view individually, different action is called for each view???**
}
public void onClick(View v) {
}
}
);
}
container = (ViewContainer) convertView.getTag();
ToggleButton myListIcon = container.listIcon;
myListIcon.setChecked(true);
Button MyButton=container.but;
TextView myListText = container.listText;
myListText.setText(_list.get(position));
Spinner MySpinner=container.spinner;
MySpinner.setAdapter(adapter);
container = null;
return convertView;
}
private class ViewContainer
{
private ToggleButton listIcon;
private TextView listText;
private Spinner spinner;
private Button but;
}
}
i assume, you have button,textview and .. on a Listview. and you need to write action for button on listview.
this is the coding format:
.....
public View getView(int position, View convertView, ViewGroup parent) {
View view;
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.calllog_row, null);
button1 = (Button) view.findViewById(R.id.button1);
button1.setOnClickListener(this);
}
else {
view = convertView;
}
Button1 = (Button) view.findViewById(R.id.Button1);
Button1.setTag(position); // this is important
return view;
}
public void onClick(View v) {
Integer position = (Integer) v.getTag();
switch (v.getId()) {
case R.id.button1:
Log.e("click position ", "" + position);
break;
}
......
if my assumption is wright my coding will be helpful to you.
you need to modify your getView like i show below code.
#Override
public View getView(int position, View convertView, ViewGroup arg2) {
ViewContainer container;
if(convertView == null)
{
LayoutInflater inflater = LayoutInflater.from(_context);
convertView = inflater.inflate(R.layout.elements,parent, false);
container = new ViewContainer();
//convertView.setClickable(true);
container.listIcon = (ToggleButton)convertView.findViewById(R.id.togle_item);
container.listText = (TextView)convertView.findViewById(R.id.text_listitem);
container.spinner=(Spinner)convertView.findViewById(R.id.spinner1);
container.but=(Button)convertView.findViewById(R.id.button1);
convertView.setTag(container);
}else{
container=convertView.getTag();
}
//Now for set Click event for your Induavidual item in list you can do like below
//use postion to set id of each item
container.but.setId(position);
container.but.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
//This will give you position of item
int id=v.getId();
}
});
//same you can do for spinner and textview and ToggleButton ,use appropriate method that you want to use
return convertView;
}
Instead of adding a OnClickListener to the convertView add listeners for every View from the row for which you need to have different actions:
//...
container.listIcon.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
//action for the toggle Button
}
});
container.but.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
//action for the simple Button
}
});
container.spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent,
View view, int pos, long id) {
//something was selected on the spinner
}
public void onNothingSelected(AdapterView parent) {
// for interface
}
});
//...
vh.orderButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// ---Starting new intent
Intent in = new Intent(activity.getApplicationContext(), ActivityOrder.class).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); ;
// ---sending product no to next activity
in.putExtra("product_category", productNo);
in.putExtra("business_id", businessId);
// ---starting new activity and expecting some response back
activity.getApplicationContext().startActivity(in);
Toast.makeText(activity, productNo + " " + businessId, Toast.LENGTH_SHORT).show();
}
});