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;
}
}
Related
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.
I am trying to show some words in the ListView.But the problem is when i start to scroll ListView is not populating the next words.It populates randomly the same duplicated words.
DictListAdapter:
public class DictListViewAdapter extends BaseSwipeAdapter {
private Context mContext;
private ArrayList<String> searchResult;
private String word;
private LayoutInflater inflater;
TextView wrd;
public DictListViewAdapter(Context mContext, ArrayList<String>searchResult) {
this.mContext = mContext;
this.searchResult = searchResult;
}
#Override
public int getSwipeLayoutResourceId(int position) {
return R.id.swipe;
}
#Override
public View generateView(final int position, ViewGroup parent,View v) {
if(inflater==null){
inflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
if(v==null) {
v = inflater.inflate(R.layout.listview_item,null);
}
wrd = (TextView) v.findViewById(R.id.word);
final TextView meaning=(TextView)v.findViewById(R.id.meaning);
SwipeLayout swipeLayout = (SwipeLayout)v.findViewById(getSwipeLayoutResourceId(position));
swipeLayout.addSwipeListener(new SimpleSwipeListener() {
#Override
public void onOpen(SwipeLayout layout) {
// YoYo.with(Techniques.Tada).duration(500).delay(100).playOn(layout.findViewById(R.id.trash));
//Toast.makeText(mContext, DatabaseHelper.getSingleMeaning(searchResult.get(position), mContext), Toast.LENGTH_SHORT).show();
String mean=DatabaseHelper.getSingleMeaning(searchResult.get(position),mContext);
meaning.setText(mean);
}
});
swipeLayout.setOnDoubleClickListener(new SwipeLayout.DoubleClickListener() {
#Override
public void onDoubleClick(SwipeLayout layout, boolean surface) {
Toast.makeText(mContext, "DoubleClick", Toast.LENGTH_SHORT).show();
}
});
/*v.findViewById(R.id.delete).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(mContext, "click delete", Toast.LENGTH_SHORT).show();
}
});
*/
word = searchResult.get(position);
wrd.setText(word);
return v;
}
This class extends BaseSwipeAdapter and it has the getView() method like below where it has the implementation of getView():
#Override
public final View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if(v == null){
v = generateView(position, null,convertView);
}
mItemManger.bind(v, position);
fillValues(position, v);
return v;
}
I know that "Every time ListView needs to show a new row on screen, it will call the getView() method from its adapter." That's why i added if(v==null) but not working.But i have done this tweak in my other projects.It worked fine then but in this project it's not working.Any Solution??Thanks in advance.
EDIT: I have tried using ViewHolder but not working.Did i miss anything??
static class ViewHolder {
TextView wrd;
int position;
}
public DictListViewAdapter(Context mContext, ArrayList<String>searchResult) {
this.mContext = mContext;
this.searchResult = searchResult;
}
#Override
public int getSwipeLayoutResourceId(int position) {
return R.id.swipe;
}
#Override
public View generateView(final int position, ViewGroup parent,View v) {
ViewHolder viewHolder;
if(inflater==null){
inflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
if(v==null) {
v = inflater.inflate(R.layout.listview_item,null);
viewHolder = new ViewHolder();
viewHolder.wrd=(TextView) v.findViewById(R.id.word);
viewHolder.position=position;
v.setTag(viewHolder);
}else{
viewHolder=(ViewHolder)v.getTag();
}
viewHolder.wrd.setText(searchResult.get(position));
final TextView meaning=(TextView)v.findViewById(R.id.meaning);
SwipeLayout swipeLayout = (SwipeLayout)v.findViewById(getSwipeLayoutResourceId(position));
swipeLayout.addSwipeListener(new SimpleSwipeListener() {
#Override
public void onOpen(SwipeLayout layout) {
// YoYo.with(Techniques.Tada).duration(500).delay(100).playOn(layout.findViewById(R.id.trash));
//Toast.makeText(mContext, DatabaseHelper.getSingleMeaning(searchResult.get(position), mContext), Toast.LENGTH_SHORT).show();
String mean=DatabaseHelper.getSingleMeaning(searchResult.get(position),mContext);
meaning.setText(mean);
}
});
swipeLayout.setOnDoubleClickListener(new SwipeLayout.DoubleClickListener() {
#Override
public void onDoubleClick(SwipeLayout layout, boolean surface) {
Toast.makeText(mContext, "DoubleClick", Toast.LENGTH_SHORT).show();
}
});
/*v.findViewById(R.id.delete).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(mContext, "click delete", Toast.LENGTH_SHORT).show();
}
});
*/
return v;
}
if you have to use ViewHolder pattern and check the null view inside the generateView methos then you will have to change the code inside BaseSwipeAdapter
#Override
public final View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
// if(v == null){
v = generateView(position, v, parent);
// }
mItemManger.bind(v, position);
fillValues(position, v);
return v;
}
comment this null check and try now.
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()
So my problem is this. I have a listview with a viewflipper in each row. When the data become more and they dont fit in the screen, when i click on a row to enable the viewflipper, it flips the row but also it flips another random row below the screen.( I can see it if I scroll down)
Sometimes if I scroll up and down again the flipped row (the second one) switches back to normal and flipped again.
here is my adapter
public class HistoryBaseAdapter extends BaseAdapter {
private static ArrayList<HistoryResults> searchArrayList;
private Context CONTEXT;
public HistoryBaseAdapter(Context context, ArrayList<HistoryResults> results, boolean length) {
searchArrayList = results;
mInflater = LayoutInflater.from(context);
log_length = length;
CONTEXT = context;
}
#Override
public int getCount() {
return searchArrayList.size();
}
#Override
public Object getItem(int position) {
return searchArrayList.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.history_row_view, null);
holder = new ViewHolder();
holder.flipper = (ViewFlipper) convertView.findViewById(R.id.flipper);
holder.flipper.setInAnimation(AnimationUtils.loadAnimation(CONTEXT, R.anim.slide_in));
holder.flipper.setOutAnimation(AnimationUtils.loadAnimation(CONTEXT, R.anim.slide_out));
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.flipper.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
holder.flipper.showNext();
}
});
return convertView;
}
static class ViewHolder {
ViewFlipper flipper;
}
}
Does anyone know what is wrong??
Actually #Raghunandan that helped!! https://www.youtube.com/watch?v=8MIfSxgsHIs
I just added 2 lines
holder.flipper.setOnClickListener(new OnClickListener() {
boolean hasTransientState = true;
#Override
public void onClick(View v) {
holder.flipper.showNext();
holder.flipper.setHasTransientState(hasTransientState);
hasTransientState = !hasTransientState;
}
});
I'm not sure if it's the more efficient way to do this but it works!
Thanks!!
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();
}
});