How to change text in BaseAdapter - java

I am trying to change the text of button inside onClick() but it's not changing. I have tried the following -
viewHoder.btn_select_qq.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
viewHoder.btn_select.setText("xxxxx");
notifyDataSetChanged();
}
});
How to set it?

Try this -
viewHoder.btn_select_qq.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Button b = (Button) v;
b.setText("xxxxx");
}
});

Related

Caused by: java.lang.NullPointerException: Error message [duplicate]

This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 1 year ago.
Here is the screenshot of Error message
I was creating a calculator app. It seems the error message is in
display.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (getString(R.string.display).equals(display.getText().toString().length()));{
display.setText("");
}
}
});
Button b0,b1,b2,b3,b4,b5,b6,b7,b8,b9;
Button buttonMult;
Button buttonSub;
Button buttonDec;
Button equal;
Button add;
Button divide;
Button percent;
private EditText display;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b0 =findViewById(R.id.button0);
b1 =findViewById(R.id.button1);
b2 =findViewById(R.id.button2);
b3 =findViewById(R.id.button3);
b4 = findViewById(R.id.button4);
b5 =findViewById(R.id.button5);
b6 =findViewById(R.id.button6);
b7 =findViewById(R.id.button7);
b8 =findViewById(R.id.button8);
b9 =findViewById(R.id.button9);
buttonMult =findViewById(R.id.buttonMult);
buttonSub =findViewById(R.id.buttonSub);
buttonDec =findViewById(R.id.buttonDec);
add =findViewById(R.id.add);
divide =findViewById(R.id.divide);
percent =findViewById(R.id.percent);
equal =findViewById(R.id.equal);
display = findViewById(R.id.input);
display.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (getString(R.string.display).equals(display.getText().toString().length()));{
display.setText("");
}
}
});
add.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {display.setText("+");
}
});
equal.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {display.setText("=");
}
});
buttonDec.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {display.setText(".");
}
});
buttonSub.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {display.setText("-");
}
});
buttonMult.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {display.setText("*");
}
});
b0.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
display.setText("0");
}
});
b1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
display.setText("1");
}
});
b2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
display.setText("2");
}
});
b3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
display.setText("3");
}
});
b4.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) { display.setText("4");
}
});
b5.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
display.setText("5");
}
});
b6.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
display.setText("6");
}
});
b7.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
display.setText("7");
}
});
b8.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
display.setText("8");
}
});
b9.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
display.setText("9");
}
});
}
}
This error typically occurs when you are referencing a view that isn't associated with the layout you've loaded.
Your code references view with the ID input.
I'm guessing your EditText View has an ID of Display, try changing the reference ID for the display variable.
display = findViewById(R.id.display)

Android Studio Display Elements of Arraylist on ImageView

I would like to display images that are stored in an arraylist to an imageview.I am using picasso library to store the links.When i press the button next i want the picture to change to the next image.I am using a for loop but i get only the last element.Here is the code:
ImageView image1;
Button bNext,
ArrayList<String>ll=new ArrayList<>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fwtografies);
image1=(ImageView)findViewById(R.id.image1);
bNext=(Button)findViewById(R.id.bNext);
ll.add("http://i.imgur.com/QoUeA2I.jpg");
ll.add("http://i.imgur.com/21szRz9.jpg");
ll.add("https://upload.wikimedia.org/wikipedia/en/e/ec/Clip_Poster.jpg");
bBack.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent fwtografiesActivityIntent = new Intent(Fwtografies.this,MainActivity.class);
Fwtografies.this.startActivity(fwtografiesActivityIntent);
}
});
bNext.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
for(int i=0;i<ll.size();i++){
sp(ll.get(i),image1);
}
}
});
bPrevius.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
viewFlipper.showPrevious();
}
});
sp("http://i.imgur.com/gijIKJO.jpg",image1);
//sp("http://i.imgur.com/QoUeA2I.jpg",image2);
//sp("http://i.imgur.com/21szRz9.jpg",image3);
}
public void sp(String a,ImageView b){
Picasso
.with(getApplicationContext())
.load(a)
.into(b);
}}
Everytime you click on Button "bnext" you are going through your whole for-loop and therefore only the last element of your list gets shown. Try:
int i = 0;
bNext.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(i<ll.size()){
sp(ll.get(i),image1);
i++;
}
}
});
My mistake is that i used for loop where i should use if statment.
This code works for my case:
int i=0; bNext.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//viewFlipper.showNext();
if(i<ll.size()){
sp(ll.get(i),image1);
i++;
}`

Android :- Separate class for handling views clicks etc;

I am new to development field. I am developing android application where in my activity_main layout I have many different items. On clicking one of the button the top layout is replaced with a new layout.
Now instead of defining the new layouts buttons, textview etc in my main class , I want to have a separate class which can initialize my buttons etc, and also in that class I can declare onClickListners.
In my main Activity I want:-
public void onCreate(){
button bb = (Button)findViewById(R.id.mybutton);
View CurrentContentView= getLayoutInflater().inflate(R.layout.activity_main, null, false);
bb.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
new MyNewViewInit(CurrentContentView);
}
});
}
In my MyNewViewInit() class :-
public class MyNewViewInit{
ImageButton backbutton;
ChipsMultiAutoCompleteTextview search;
ImageButton searchclear;
ImageButton modeTime;
ImageButton modeTag;
TextView modeTimeTxt;
TextView modeTagTxt;
ScrollView mainScroll;
ScrollView selectedScroll;
public MyNewViewInit(View v){
backbutton = (ImageButton)v.findViewById(R.id.backbutton);
search = (ChipsMultiAutoCompleteTextview)v.findViewById(R.id.search);
searchclear = (ImageButton)v.findViewById(R.id.searchclear);
modeTime = (ImageButton)v.findViewById(R.id.modeTime);
modeTag = (ImageButton)v.findViewById(R.id.modeTag);
modeTimeTxt = (TextView)v.findViewById(R.id.modeTimeTxt);
modeTagTxt = (TextView)v.findViewById(R.id.modeTagTxt);
mainScroll = (ScrollView)v.findViewById(R.id.HAT1);
selectedScroll = (ScrollView)v.findViewById(R.id.HAT2);
tag = new OtherHelper().arrayread();
mainHashTag.setVisibility(View.GONE);
selectedHashTag.setVisibility(View.GONE);
clickListners();
}
public void clickListners(){
backbutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
searchclear.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
modeTime.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
modeTimeTxt.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
modeTag.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
modeTagTxt.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
}
}
So when I try using this code the onclicklistners are not working.
What is the best way to do this.
Thanks
Write an Interface in your class,
class MyNewViewInit{
public interface ClickListenerInterface{
void onCLickSomething(Something something);
}
ClickListenerInterface mClickListener;
...
public MyNewViewInit(View v,ClickListenerInterface clickListener){
mClickListener = clickListener;
...
}
public void clickListners(){
...
backbutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(mClickListener!=null){
mClickListener.onCLickSomething(v);
}else{
//throw an error that activity should implement ClickListenerInterface
}
}
});
}
}
In your Activity,
class MyActivity extends Activity implements ClickListenerInterface{
public void onCreate(){
bb.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
new MyNewViewInit(CurrentContentView,this);
//if using Android Studio, then use the context menu to implement the interface
}
});
}
void onCLickSomething(Something something){
//do something with something
}
}
You can try do a abstract class extends Activity, like :
public abstract class MyInitActivity extends Activity
{
EditText editText;
TextView textView;
#Override
public void setContentView(int layoutResID)
{
super.setContentView(layoutResID);
editText = (EditText)findViewById(R.id.editText);
textView = (TextView)findViewById(R.id.textView);
}
}
and in your main activity you can extend your abstract class, like :
public class MainActivity extends MyInitActivity
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
/*********** Listener ************/
public void onButtonClick(View v)
{
textView.setText(editText.getText().toString());
}
}
you can also implement onClick() in the abstract class
i don't know if its a good practice , i prefer do the methods with view arguments and set this methods in the layout.xml file .
I had use this in one of code
STEP 1
Declare button in layout(XML file)
STEP 2
Declare all ur button like this--
Button btn1 = (Button)findViewById(R.id.btn1);
Button btn2 = (Button)findViewById(R.id.btn2);
Button btn3 = (Button)findViewById(R.id.btn3);
............so on
STEP 3
implement View.OnClickListener
Now after step 2 do this
btn1.setOnClickListener(this);
.
.
.
same way for all
STEP 4:
Use switch case inside Onclick
public void OnClick(View v)
{
case:R.id.btn1{
//ur command similarly use multiple case stmt for many button
}
}

Displaying Numbers from onClickListener() Method

I'm looking to display a string of numbers from button clicks in the same TextView field. So far, I'm only able to display one at a time with each press. How can I display all the numbers I click on the same line? My code looks like the following:
one.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Display.setText("1");
}
});
two.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Display.setText("2");
}
});
.
.
.
nine.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Display.setText("9");
}
});
zero.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Display.setText("0");
}
});
How can I get it to where if I press buttons one through three my display will show "123" instead of "1" then "2" then "3" individually?
You can maintain a string variable containing the current numbers being displayed, then append each digit when the button is clicked, and display that.
String digits = "";
one.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
digits += "1";
Display.setText(digits);
}
});
two.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
digits += "2";
Display.setText(digits);
}
});

I wrote this few lines of code to change the xml file of this class but it doesn't work

Im a Beginner in Java and Android Studio. With this Code I try to change the layout in this activity. The current layout is "marcelscorpion_1".
Only the Buttons "weiter_1" and "zurück_1" are working and I don't know why...
public void SwitchLayout()
{
Button weiter_1 = (Button) findViewById(R.id.marcelscorpion_weiter1);
Button zurück_1 = (Button) findViewById(R.id.marcelscorpion_zurück1);
View marcelscorpion_2 = LayoutInflater.from(getApplication()).inflate(R.layout.marcelscorpion_2, null);
Button weiter_2 = (Button) marcelscorpion_2.findViewById(R.id.marcelscorpion_weiter2);
Button zurück_2 = (Button) marcelscorpion_2.findViewById(R.id.marcelscorpion_zurück2);
View marcelscorpion_3 = LayoutInflater.from(getApplication()).inflate(R.layout.marcelscorpion_3, null);
Button weiter_3 = (Button) marcelscorpion_3.findViewById(R.id.marcelscorpion_weiter3);
Button zurück_3 = (Button) marcelscorpion_3.findViewById(R.id.marcelscorpion_zurück3);
weiter_1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
setContentView(R.layout.marcelscorpion_2);
}
});
weiter_2.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
setContentView(R.layout.marcelscorpion_3);
}
});
weiter_3.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
setContentView(R.layout.marcelscorpion_1);
}
});
zurück_1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
setContentView(R.layout.marcelscorpion_3);
}
});
zurück_2.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
setContentView(R.layout.marcelscorpion_1);
}
});
zurück_3.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
setContentView(R.layout.marcelscorpion_2);
}
});
}
Need Help ;) Thank you!
Every time you call setContentView() you should find and setOnClickListener(..) again to all buttons contained in the new layout.
A better way is define onclick attribute in the layouts xml file.
For instance if you have this:
<Button
android:id="#+id/w2"
android:onClick="getms3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="w2" />
then you should define
public void getms3(View v)
{
setContentView(R.layout.ms3);
}
Just add your buttons of that layout when you are doing setContentView() in onClick() methods like this :
weiter_1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
setContentView(R.layout.marcelscorpion_2);
Button weiter_2 = (Button) marcelscorpion_2.findViewById(R.id.marcelscorpion_weiter2);
Button zurück_2 = (Button) marcelscorpion_2.findViewById(R.id.marcelscorpion_zurück2);
}
});

Categories

Resources