Android ListView item onClick - android

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()

Related

How to access button in different listview rows?

I'm trying to change the color of buttons in the rows of listview by clicking a button. Here I've a listview composed of 25 rows and each rows contains a button. I've used custom adapter for listview to place button in each row. I like to know how to do following things:
Click on a button and its color is changed.
After step (1) the onClickListener also change the color of the left-over buttons in the listview to normal.
The above two steps should happen to all of the buttons means if any of the button is clicked then it should be colored and remaining will turn back to normal.
the listview is like
Button 1
Button 2
Button 3
Button 4
You have to access your Button from Adapter. Follow it -
public class TestAdapter extends BaseAdapter {
private Context ctx;
private List<TestList> list;
public TestAdapter(Context ctx, List<TestList> list){
this.ctx = ctx;
this.list = list;
}
#Override
public int getCount() {
return list.size();
}
#Override
public TestList getItem(int position) {
return list.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater layoutInflater = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.layout_item, parent, false);
}
Button button = (Button) convertView.findViewById(R.id.button);
button.setText(list.get(position));
return convertView;
}
}
Use onclick listener for button in your custom adapter
public class CustomAdapter extends BaseAdapter {
private Context ctx;
private List<CustomList> list;
public CustomAdapter(Context ctx, List<CustomList> list){
this.ctx = ctx;
this.list = list;
}
#Override
public int getCount() {
return list.size();
}
#Override
public CustomList getItem(int position) {
return list.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater layoutInflater = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.layout_item, parent, false);
}
Button button = (Button) convertView.findViewById(R.id.button);
button.setText(list.get(position));
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
\\change color here
}
});
return convertView;
}
}

Butterknife not working with ViewFlipper

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;
}
}

OnItemClick under Fragment is not working

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.

how to get listview item on button click and save to sqlite

I've four textviews and a button in listview. when user clicks on the button i want to get the value of these four textviews and pass to the database. I'm confuse how to save the data of selected item to sqlite.
public class PlannningListViewAdapter extends BaseAdapter{
Context context;
public ArrayList<Planning> planArraylist;
private static LayoutInflater inflater = null;
public Activity planActivity;
public PlannningListViewAdapter(Context context,
ArrayList<Planning> planArraylist) {
this.context = context;
this.planArraylist = planArraylist;
inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
return planArraylist.size();
}
#Override
public Object getItem(int arg0) {
return arg0;
}
#Override
public long getItemId(int arg0) {
return arg0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = inflater.inflate(R.layout.single_lv_item, null);
}
Planning p = planArraylist.get(position);
TextView tvDocCode = (TextView) convertView.findViewById(R.id.Plan_no);
TextView tvDocName = (TextView) convertView.findViewById(R.id.doc_name);
TextView tvmon = (TextView) convertView.findViewById(R.id.mon);
TextView tvAdr = (TextView) convertView.findViewById(R.id.adr);
Button btn_save =(Button)convertView.findViewById(R.id.button1);
btn_save.setOnClickListener(ButtonClickListener);
tvDocCode.setText(p.getDocCode());
tvDocName.setText(p.getDocName());
tvmon.setText(p.getTerrCode());
tvAdr.setText(p.getAdr());
return convertView;
}
private OnClickListener ButtonClickListener = new OnClickListener() {
#Override
public void onClick(View v) {
final int position = mListView.getPositionForView((View) v.getParent());
// Log.v(TAG, "item clicked, row %d", position);
}
};
}
You can use setTag()and getTag() method on your button.
Please look at the code below:
You have to change getItem method:
#Override
public Planning getItem(int arg0) {
return planArraylist.get(arg0);
}
Update your getView method:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
...
Button btn_save =(Button)convertView.findViewById(R.id.button1);
btn_save.setTag(position);
...
}
private OnClickListener ButtonClickListener = new OnClickListener() {
#Override
public void onClick(View v) {
final int position = (int)v.getTag();
Planning planning = getItem(position);
yourDatabaseHelper. getRuntimeExceptionDao(Planning.class).createOrUpdate(planning);
}
};
One more thing. You should implement ViewHolder please read about this pattern, and the best solution for you is use RecyclerView, getting position is already implemented there in RecyclerView.ViewHolder.

how to create trackballevent in android custom adapter?

i am using following code to create custom adapter for listview.
now i want to use trackball click event in it but i dont know how to do that can any one help me out in creating ontracballevent in custom adapter?
i have tried writing few lines but not able to solve it.
public class EfficientAdapter extends BaseAdapter implements Filterable {
private LayoutInflater mInflater;
private Context context;
int pos;
public EfficientAdapter(Context context) {
mInflater = LayoutInflater.from(context);
this.context = context;
}
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder;
convertView = mInflater.inflate(R.layout.adaptor_contentposts, null);
convertView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//click functionality
}
});
MotionEvent event= MotionEvent.CREATOR.createFromParcel(null);
switch (event.getAction())
{
case MotionEvent.ACTION_DOWN:
//display click message
}
convertView.onTrackballEvent(event);
return convertView;
}
class ViewHolder {
TextView textLine;
TextView textLine2;
TextView PostedByAndPostedOn;
ImageButton ImgButton;
}
#Override
public Filter getFilter() {
return null;
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public int getCount() {
return ad_id.length;
}
#Override
public Object getItem(int position) {
return ad_id[position];
}
}
Adapters have nothing to do with "ontracballevent". You either need to subclass ListView or handle it in your activity.

Categories

Resources