setOnClickListener in Recycler View Crashes - android

I have recently started implementing RecyclerView. I have implemented horizontal recyclerview with image and imagebutton. I want to know position of which image or imagebutton is clicked. I could able to see six images horizontally in the view.
App crashes in the following line:
Toast.makeText(context, "You Clicked Image Button " + position, Toast.LENGTH_LONG).show();
Implementation:
#Override
public void onBindViewHolder(ViewHolder viewHolder, int pos) {
final int position = pos;
viewHolder.imgViewIcon.setImageResource(itemsData[position].getImageUrl());
viewHolder.imgBtn.setImageResource(R.drawable.ic_close_black_24dp);
viewHolder.imgBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Crashes in the following line
Toast.makeText(context, "You Clicked Image Button " + position, Toast.LENGTH_LONG).show();
((FrameLayout) v.getParent()).removeAllViews();
}
});
}

As you saying no logcat showing for ERROR I am only assuming that your context is null.
try this solution
if(context != null)
Toast.makeText(context, "You Clicked Image Button " + position, Toast.LENGTH_LONG).show();
else
Log.d("You Clicked Image Button", "Position : " + position);

Try the following Code, Hope it works
Toast.makeText(context,"item Clicked at "+getPosition(), Toast.LENGTH_LONG).show();

Related

checkedTextView OnClickListener .setChecked() not working

I have created a Custom Adapter and applied it to listView, but when I click a CheckTextView item the .setChecked() method won't change the state of the radio button on the UI.
int getPosition = position;
checkedTextView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (checkedTextView.isChecked()) {
Log.i("Username", Title.get(getPosition) + " is no longer checked");
checkedTextView.setChecked(false);
} else {
Log.i("Username", Title.get(getPosition) + " is checked");
checkedTextView.setChecked(true);
}
}
});
However the logs are showing that the view is being checked and unchecked.
What am I missing?
Thanks
I dont know why its happening but i have an idea to implement it in another way
Assuming your are using custom adapter to load list items
set OnClickListener for CheckedTextView at the time of view creation in your custom adapter
final CheckedTextView ct = (CheckedTextView) view
.findViewById(R.id.checkedTextView);
ct.setText("text");
ct.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// do what ever you want to perform here
if(ct.isChecked()){
} else {
}
}
});

How to Set Listview's Childitem Clicklistener in MainActivity Not in Adapter (In android)?

Hey guys This is my first question on stackoverflow.
I am searching on a topic almost for 1 month but did not find any relevant answer.
I want to set clicklistner to the list child items in the main activity.
I know about Listview.setOnItemClickListner... and also know how to set listview's child item clicking in own adapter.
But i don't want this..
I want to set listview's child item clicking in the main activity
this is my main activity code for list item clicking...
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, final int position, final long id) {
final TextView tx;
tx=(TextView)view.findViewById(R.id.textList);
tx.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(listviewclick_Activity.this, "in= "+String.valueOf(position), Toast.LENGTH_SHORT).show(); // clicking for textview on list
}
});
view.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(listviewclick_Activity.this,"out= "+ String.valueOf(position), Toast.LENGTH_SHORT).show(); // clicking for whole listitem
}
});
}
});
This code works fine but it has a problem that a user has to click first
time after that all clicking will be set...
Remember i dont want to click a childitem in adapter. i only want to click it in main activity.
Hoping for a good answer...
I don't understands what you are doing but this part:
view.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(listviewclick_Activity.this, "out= " + String.valueOf(position), Toast.LENGTH_SHORT).show(); // clicking for whole listitem
}
});
is wrong !, because view is the list view item (row) that is already clicked.
instead replace it with:
Toast.makeText(listviewclick_Activity.this, "out= " +String.valueOf(position),Toast.LENGTH_SHORT).show(); // clicking for whole listitem
Also this part:
tx.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(listviewclick_Activity.this, "in= "+String.valueOf(position), Toast.LENGTH_SHORT).show(); // clicking for textview on list
}
});
will not work unless you click the listview item, so u should move it inside the adapter.

How to know item selected on spinner?

I have the following code:
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Object item = parent.getItemAtPosition(position);
Toast.makeText(this, "You selected " +item.toString(), Toast.LENGTH_SHORT).show();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
Toast.makeText(this, "You didn't select any preset", Toast.LENGTH_SHORT).show();
}
And the following array:
<string-array name="presets_array">
<item>Light Car</item>
<item>Medium Car</item>
<item>Heavy Car</item>
<item>Van</item>
</string-array>
It's working fine, when I select Light car it displays the correct text and so on with the others.
But I want to do more things, if selected thing is Van, change weight value, if medium car, other cost, etc.
I think I have to use a switch but I'm not sure how it works.
There are some options. You should choose the best which fits to you...
Just checking position
Good performance but if you change array order, you must re-order the switch statement
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Object item = parent.getItemAtPosition(position);
Toast.makeText(this, "You selected " + item.toString(), Toast.LENGTH_SHORT).show();
swith(position) {
case 0:
// CODE for Light Car
break;
case 1:
// CODE for Medium Car
break;
case 2:
// CODE for Heavy Car
break;
case 3:
// CODE for Van
break;
}
}
String Checking
Low performance since you have to compare Strings several times in the worst case.
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Object item = parent.getItemAtPosition(position);
Toast.makeText(this, "You selected " + item.toString(), Toast.LENGTH_SHORT).show();
if(item.toString().equals("Light Car")) {
// CODE
} else if(item.toString().equals("Medium Car")) {
// CODE
} else if(item.toString().equals("Heavy Car")) {
// CODE
} else if(item.toString().equals("Van")) {
// CODE
}
}
you can write code similar to this code inside onItemSelected method:
switch (item)
{
case Light Car:
// TODD
break;
case Van:
// TODO
break;
.......
}

OnClickListener in a while loop on custom objects

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...

how to save previous button state on next click item in Gallery

I'm new in android .I'm working with gallary.I would like to know how to save previous button state when I click on next gallery item.I want to make default button state off on all images and when button state will select with image it shoud be safe when I click on next one.When I come back on previous image it shoud be show saving button state .I tried to compare image id and button selected image id.But that's not really good work .How to do this?Thanks a lot for feature help.
imageView = (ImageView) findViewById(R.id.ImageSportView);
imageView.setImageResource(imgID[0]);
sportButton = (Button) findViewById(R.id.SportButton1);
gallery = (Gallery) findViewById(R.id.SportGallery);
// creating AddImgadapter
gallery.setAdapter(new AddImgAdapter(this));
#Override
public void onItemClick(AdapterView<?> perent, View view,
int position, long id) {
// getting id position
setSelected(position);
// deselect button on item click
onImageClick(view, position);
Log.d(MY_LOG, "img click");
Log.d(MY_LOG, "img Id" + position);
}
});
}
// deselect button on image click
int itemPosition = 1;
public void onImageClick(View button, int position) {
itemPosition = position;
if (selectedPosition != position) {
sportButton.setSelected(false);
Log.d(MY_LOG, "selected postion " + selectedPosition + " = "
+ "item position" + position);
} else {
if (selectedPosition == position) {
sportButton.setSelected(true);
}
Log.d(MY_LOG, "selected postion " + selectedPosition + " = "
+ "item position " + position);
}
}
// getting Id current item
int selectedPosition = -1;
private void setSelected(int position) {
selectedPosition = position;
imageView.setImageResource(imgID[selectedPosition]);
}
public void onClickButton(View button) {
if (button.isSelected()) {
button.setSelected(false);
Log.d(MY_LOG, "click off");
} else {
// on button click on we select picture id
button.setSelected(true);
if (selectedPosition != -1) {
Log.d(MY_LOG, "selected position " + selectedPosition);
//selectedImage(selectedPosition);
//Intent intent = new Intent(Sport.this, Favorites.class);
}
Log.d(MY_LOG, "click on");
}
}
One way to save the state of the previous button would be to make use of shared preferences in Android . Shared preferences allow key value pairs of data to be stored which can be later retrieved easily . There are one of the dataaccess mechanisms in Android . Others being SqlLite Database & Files .
Android Documentation on Share preference
Video on shared preference
Now coming back to your problem again . I once had to save the state of a checkedbutton . Then later access it again ( which seems to be similar to what you want to do as well )
Part 1 Accessing Share preference Values :
public static final String PREFS_FILE = "MyPreferences";
public static final String PREFS_NAME = "USER_NAME";
public static final String PREFS_CHOICE = "USER_CHOICE";
SharedPreferences sp;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
chkChoice = (CheckBox)findViewById(R.id.chkChoice);
btnMain = (Button)findViewById(R.id.btnMain);
btnMain.setOnClickListener(this);
// Here i access the shared preference file .
sp = this.getSharedPreferences(PREFS_FILE, MODE_PRIVATE);
// If i have a preference for my checkbox saved then load it else FALSE(unchecked)
chkChoice.setChecked(sp.getBoolean(PREFS_CHOICE, false));
}
Part 2 Setting Share preference from your activity :
sp = this.getSharedPreferences(PREFS_FILE, MODE_PRIVATE);
SharedPreferences.Editor editor = sp.edit();
editor.putString(PREFS_NAME, txtName.getText().toString());
editor.putBoolean(PREFS_CHOICE, chkChoice.isChecked());
editor.commit();
// Close the activity to show that the data is still saved
finish();
The above is for a checkbox . You will have to adapt it for the kind of button information you want to save . Hope this gets you started .

Categories

Resources