i have created a custom listview with an ImageView, two TextViews and a Button
now i am facing issue when i try to set onClicklistner to that button
i want different method to be done for every button
here is my code for customlistview class
i have used temporary onclicklistner for that button which shows the toast "Bought"
what i want to do is that after clicking the button i have to return the price of the food.
class CustomListView extends ArrayAdapter {
public CustomListView(Context context, String[] resource) {
super(context,R.layout.custom_view , resource);
}
Toast toast= null;
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater MyInflater = LayoutInflater.from(getContext());
View CustomView = MyInflater.inflate(R.layout.custom_view, parent, false);
String SingleItem= (String) getItem(position);
final TextView text =(TextView)CustomView.findViewById(R.id.Itemname);
final ImageView Image= (ImageView)CustomView.findViewById(R.id.icon);
final TextView Pricetag= (TextView)CustomView.findViewById(R.id.PriceTextView);
text.setText(SingleItem);
switch (SingleItem)
{
case "Chicken":
Image.setImageResource(R.drawable.desert1);
Pricetag.setText("Rs 300");
break;
case "soap":
Image.setImageResource(R.drawable.desert2);
Pricetag.setText("Rs 300");
break;
case "Fish":
Image.setImageResource(R.drawable.fish);
Pricetag.setText("Rs 100");
break;
default:
Image.setImageResource(R.drawable.myimage);
Pricetag.setText("Rs 0.00");
break;
}
final Button Buybutton= (Button)CustomView.findViewById(R.id.BuyButton);
toast = Toast.makeText(getContext(), "", Toast.LENGTH_LONG);
Buybutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
toast.setText("Bought");
toast.show();
}
});
text.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
toast.setText(text.getText().toString());
toast.show();
}
});
return CustomView;
}
}
For the record, there should be a better way to do this, using food-id or other approach to prevent this, but based on your request, here we go:
1- in getView() when you get a reference on the button i.e:
final Button Buybutton= (Button)CustomView.findViewById(R.id.BuyButton);
do a one more step:
Buybutton.setTag(10);
10 here could be any other number, and you need to find a way to determine which number to use for each button, also it could be a string , ex value of SingleItem
Buybutton.setTag(SingleItem);
2- at onClick() you need to find out what the value assign to the view (button) and based on this value call the proper method:
#Override
public void onClick(View v) {
if (v.getTag().toString().equals("xxxxx")){
doSomething();
}else if (v.getTag().toString().equals("yyyy")){
doAnotherThing();
}else if (v.getTag().toString().equals("zzzzz")){
doSomething12();
}
//and so on...
}
this approach used String as value in setTag() and getTag()
if you use integers, just replace the condition as belwo:
if (Integer.parseInt(v.getTag().toString()) == 10)
EDIT:
If i understand well, then you need to:
Buybutton.setTag(SingleItem);
and onClick():
#Override
public void onClick(View v) {
showPriceTag(v.getTag().toString());
}
add method showPriceTag():
public void showPriceTag(String type){
switch (type)
{
case "Chicken":
//set the price tag data ...
break;
case "soap":
//set the price tag data ...
break;
case "Fish":
//set the price tag data ...
break;
default:
//set the default price tag data ...
break;
}
}
Related
android listview add headerView ,but the textView's onclicklestener in the header item does not work .
my english is not well. thanks.
code in myFragment
ViewGroup headerView = (ViewGroup) inflater.inflate(R.layout.item_header, list, true);
list.addHeaderView(headerView);
list.setAdapter(musicBaseAdapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
final Intent intent = new Intent(getActivity(), RedActivity.class);
intent.putExtra("rank", position);
startActivity(intent);
}
});
headerView.setOnClickListener(new ViewGroup.OnClickListener() {
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.text1:
log.d("text1");
break;
case R.id.text2:
log.d("text2");
break;
case R.id.text3:
break;
case R.id.text4:
break;
}
}
});
when i click the textview in headeritem,it nothing changed.
how can i set the click event ?
The problem you are having is you are assuming that click events for the child Views in your header ViewGroup will receive the group's click events.
This is not true. In that click listener you have, the value of View v will only ever be the headerView instance.
What you want is to attach click listeners to each of your header's views.
Ie.
headerView.findViewById(R.id.text1).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
log.d("text1");
}
});
This is not how it works . You need to set listner for each view if you want to get separate click on each . There can be multiple way to do it . You can try following.
Let your Fragment implement OnClickListener. and set onClick for each item. Below is an example .
class MFragment extends Fragment implements View.OnClickListener{
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.text1:
log.d("text1");
break;
case R.id.text2:
log.d("text2");
break;
case R.id.text3:
break;
case R.id.text4:
break;
}
}
}
And set Listener .
ViewGroup headerView = (ViewGroup) inflater.inflate(R.layout.item_header, list, true);
headerView.findViewById(R.id.text1).setOnClickListener(MFragment.this);
headerView.findViewById(R.id.text2).setOnClickListener(MFragment.this);
headerView.findViewById(R.id.text3).setOnClickListener(MFragment.this);
headerView.findViewById(R.id.text4).setOnClickListener(MFragment.this);
list.addHeaderView(headerView);
list.setAdapter(musicBaseAdapter);
Let's say i have a button which inserts a number to the front of a linked list and a button that inserts a number to the end of the linked list. I have this AlertDaialog and I'm trying to use it with both buttons. How can i differentiate the buttons from one another so that when i press "OK" on the dialog, the onClick method know where to insert the number(front or end).
I've tried somthing like this but it doesn't work, only the default case gets activated.
ok.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
String input = inputText.getText().toString().trim();
int numberInput = Integer.parseInt(input);
switch (v.getId()){
case R.id.btn_front: //insert to the beginning
break;
case R.id.btn_end: //insert to the end
break;
default: Log.e("DIALOG_ERROR", "Error!");
}
}
});
Use a class that extend dialog like:
DialogClass obj=new DialogClass(context,true);
obj.show();
class will be:
public DialogClass(final Context context,Boolean isAddToFront) {
super(context);
setContentView(R.layout.dialog_layout);
setTitle(Title);
TextView Ok = (TextView) findViewById(R.id.ok);
Ok.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(isAddToFront){
/// Add to front
}else{///add to back}
});
}
I'm setting up a CardView layout (with the libary) but I've got a problem.
I can setup a onClickListener (which works) in this way:
mCardView = (CardUI) getView().findViewById(R.id.cardsview);
mCardView.setSwipeable(true);
MyCard b = new MyCard("Hi", "Hi", 15);
b.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
System.out.println(v.toString());
Toast.makeText(getActivity(),"werkt",Toast.LENGTH_LONG).show();
}
});
mCardView.addCard(b);
But when I try to do it in a loop I can't seem to get the ID of the cards.
My cards do have a unique ID (just a int as ID).
I'm adding them like this:
int id = 0;
while(!c.isAfterLast()){
String description = "";
System.out.println("Opmerking: " + c.getString(3));
if(!c.getString(3).equals("")){
description = "Opmerkingen: " + c.getString(3);
}
if(recreate){
MyCard a = new MyCard(c.getString(2)+" "+c.getString(1), description, id);
cards.add(a);
mCardView.addCard(a);
a.setOnClickListener(new OnClickListener());
}
c.moveToNext();
id++;
}
And the onClickListener is this:
private class OnClickListener implements View.OnClickListener {
#Override
public void onClick(View view) {
switch (view.getId()){
case 1:
Toast.makeText(getActivity(), "werkt", Toast.LENGTH_LONG).show();
break;
default: Toast.makeText(getActivity(), "werkt niet", Toast.LENGTH_LONG).show(); break;
}
}
}
But everytime when I click one which has a onClickListener of the while loop I get 'werkt niet' which is the default item of the switch.
The card ID is a int in the MyCard object.
If anyone could help me it would be greatly appreciated.
Edit: The MyCard object is a object which needs this libary: http://nadavfima.com/cardsui-view-library/
That is because your view.getId() is never equals 1. Try adding
System.out.println("view.getId() -->> "+view.getId());
inside the default case..
You will get your problem then and there...
Basically I'm new to Android and don't know much about it. I'm making a quiz program in which I'm using custom ListView with 5 custom TextViews, one for question and other 4 for options. My problem is that I want the TextView as clickable as well as the LisView as choice mode as single. That is if I click one text all other TextViews should become unclickable. My problem is whenever I click on a TextView in the child layout, only the outer layout, that is the item of the ListView get selected.
here is the screenshot of the my listview
https://picasaweb.google.com/108429569548433380582/Android?authkey=Gv1sRgCJ3kxJz7tLvaTg#5783846428648608706
You can do it in two ways:
1. Either by directly using onClickListener like this:
textView.setOnClickListener(new View.OnClickListener(
public void onClick(View arg0) {
// Do anything here.
}
});
OR
2. In XML file, in declaration of <TextView /> add one more attribute as:
android:onClick="onClickTextView"
and in yout activity, add this function:
public void onClickTextView(View view) {
// Do anything here.
}
UPDATE:
Use following code to get click event on TextView:
// Click event for single list row
getListView().setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
TextView tv = (TextView) (findViewById(R.id.title));
if (tv != null) {
tv.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "CLICKED",
Toast.LENGTH_LONG).show();
}
});
} else {
Toast.makeText(MainActivity.this, "TV not found",
Toast.LENGTH_LONG).show();
}
}
});
Try this :
When you select one textview the other three will be unclickable
final TextView texta = (TextView) findViewById(R.id.text_a);
final TextView textb = (TextView) findViewById(R.id.text_b);
final TextView textc = (TextView) findViewById(R.id.text_c);
final TextView textd = (TextView) findViewById(R.id.text_d);
texta.setOnClickListener(new OnClickListener()
{
public void onClick(View v) {
textb.setClickable(false);
textc.setClickable(false);
textd.setClickable(false);
}
});
textb.setOnClickListener(new OnClickListener()
{
public void onClick(View v) {
texta.setClickable(false);
textc.setClickable(false);
textd.setClickable(false);
}
});
textc.setOnClickListener(new OnClickListener()
{
public void onClick(View v) {
texta.setClickable(false);
textb.setClickable(false);
textd.setClickable(false);
}
});
textd.setOnClickListener(new OnClickListener()
{
public void onClick(View v) {
texta.setClickable(false);
textb.setClickable(false);
textc.setClickable(false);
}
});
Assume you extend BaseAdapter to set the listview content ->
Open a TextView listener and settag of the current holder position , and perform your operation in the onclick method.
It's the default behavior of a ListView. Only one could be clickable: either the list row or the items inside the row.
Eg: if the row item is a textView(as in your case) the list row will be clickable but if the row item is a button then the the list row will not be clickable. Same is the case if you make TextView as clickable.
For your requirement the better approach would be to use RadioGroup (instead of multiple text view and disabling and enabling them).
You should use a custom layout for you list item with a TextView for question and a RadioGroup for options.
Layout could be something like this :
Follow these links for reference:
for listView
for RadioGroup
I hope this will help
Thanks for Shrikant and adam for there help Sorry and i appologize for a very late response.
either use this in adapter class as by Shrikant:
textViewa.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Do anything here.
}
});
textViewb.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
// Do anything here.
}
});
//and so on...
// or better to use ViewHolder holder; for these type of listviews;
View.OnClickListener clickListener = new View.OnClickListener() {
#Override
public void onClick(View view) {
// Do what you want to do.
// for my i have to call a method in my parent activity. so in constructor of adapter, I passed the activity and then typecasted it like
ParentActivity parent = (ParentActivity) activity;
parent.chosenAnswer(view.getId());
// then in chosenAnswer(int id) in parentActivity use a switch case for the same logic as in Adam's answer.
// OR
//you can write like this too..
switch (view.getId()) {
case R.id.textViewa:
break;
case R.id.textViewb:
break;
case R.id.textViewc:
break;
case R.id.textViewd:
break;
}
}
};
I have two editTextField, the same listener is attached to both of them. How would I figure out in the listener which one is clicked?
inputStart = (EditText)findViewById(R.id.editText3);
inputStart.setOnClickListener(TimePickerButtonOnClickListener);
inputEnd = (EditText)findViewById(R.id.editText4);
inputEnd.setOnClickListener(TimePickerButtonOnClickListener);
private Button.OnClickListener TimePickerButtonOnClickListener = new Button.OnClickListener() {
public void onClick(View v) { }
};
I have checked, one possible solution is in this question,
but I am looking for any property like EditText gotEditField = (EditText)v.targetEditField;
Does any such property exist?
Use switch case, in onClick() with parameter View v using v.getId()
private Button.OnClickListener TimePickerButtonOnClickListener = new Button.OnClickListener()
{
public void onClick(View v) {
switch(v.getId){
case R.id.editText3:
break;
.
.
.
}
};
Also you can get the object of EditText using Type Casting the View v parameter..
Like EditText editText = (EditText)v;
View v from the parameters in your onCLick method is the view that was clicked.
EditText gotEditField = (EditText) v;
public void onClick(View v)
{
int id= v.getId();
switch (id)
{
case R.id.editText3:
break;
case R.id.editText4:
break;
}
}