Android Studio Display Elements of Arraylist on ImageView - java

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

Related

Chronometers reset to zero when I change activities

I have implemented two chronometers in a same activity and so far they are working fine. But when I change activities and then come back to them, they are at 00:00. I need them to keep counting "forever" until I hit the stop button. How can I do that?
And is it possible to make that counting appear also in another activity, so that I can follow up the counting in either activity? It has to be the same counting. Just two different places where I can follow the up from.
I tried a solution with onRestoreInstanceState but I am really confused. I am really new to coding and simple things are still frustrating to me. I am getting something wrong at the set.Base().
Chronometer cronometro_primeira_marca, cronometro_segunda_marca;
Button btn_iniciar_cronometro_primeira_marca, btn_parar_cronometro_primeira_marca, btn_resetar_cronometro_primeira_marca;
Button btn_iniciar_cronometro_segunda_marca, btn_parar_cronometro_segunda_marca, btn_resetar_cronometro_segunda_marca;
String timer1, timer2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cronometros);
cronometro_primeira_marca = (Chronometer) findViewById(R.id.cronometro_primeira_marca);
cronometro_segunda_marca = (Chronometer) findViewById(R.id.cronometro_segunda_marca);
btn_iniciar_cronometro_primeira_marca = (Button) findViewById(R.id.btn_iniciar_cronometro_primeira_marca);
btn_parar_cronometro_primeira_marca = (Button) findViewById(R.id.btn_parar_cronometro_primeira_marca);
btn_resetar_cronometro_primeira_marca = (Button) findViewById(R.id.btn_resetar_cronometro_primeira_marca);
btn_iniciar_cronometro_segunda_marca = (Button) findViewById(R.id.btn_iniciar_cronometro_segunda_marca);
btn_parar_cronometro_segunda_marca = (Button) findViewById(R.id.btn_parar_cronometro_segunda_marca);
btn_resetar_cronometro_segunda_marca = (Button) findViewById(R.id.btn_resetar_cronometro_segunda_marca);
btn_iniciar_cronometro_primeira_marca.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
cronometro_primeira_marca.setBase(SystemClock.elapsedRealtime());
cronometro_primeira_marca.start();
}
});
btn_iniciar_cronometro_segunda_marca.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
cronometro_segunda_marca.setBase(SystemClock.elapsedRealtime());
cronometro_segunda_marca.start();
}
});
btn_parar_cronometro_primeira_marca.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
cronometro_primeira_marca.stop();
}
});
btn_parar_cronometro_segunda_marca.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
cronometro_segunda_marca.stop();
}
});
btn_resetar_cronometro_primeira_marca.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
cronometro_primeira_marca.setBase(SystemClock.elapsedRealtime());
}
});
btn_resetar_cronometro_segunda_marca.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
cronometro_segunda_marca.setBase(SystemClock.elapsedRealtime());
}
});
}
#Override
protected void onSaveInstanceState(Bundle outState) {
outState.putLong("Restored_time1", SystemClock.elapsedRealtime());
super.onSaveInstanceState(outState);
}
#Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
if((savedInstanceState !=null)&& savedInstanceState.containsKey("Restored_time1")){
SystemClock.elapsedRealtime().**setBase**(savedInstanceState.getLong("Restored_time1"));
}
super.onRestoreInstanceState(savedInstanceState);
}
}

SetOnClickListener and View view- Cannot resolve Symbol

I have already imported android.view.View on the java class but the code below still has SetOnClickListener and View view unresolved. Not sure if this is linked but I have used another onClick and View view method as observed in the second code.
final ImageButton btnTest =(ImageButton) findViewById(R.id.passVisibilityStatus);
btnTest.setOnClickListener(new View.OnClickListener()
{
public void changePassVisibility (View view)
btnTest.setSelected(!btnTest.isPressed());
if (btnTest.isPressed()) {
btnTest.setImageResource(R.drawable.PassVisible);
} else {
btnTest.setImageResource(R.drawable.PassInvisible);
}
}}
Previous codes in case they are the problem:
public void loginButtonClicked(View view) {
String email = loginEmail.getText().toString().trim();
String pass = loginPass.getText().toString().trim();
if (!TextUtils.isEmpty(email) && !TextUtils.isEmpty(pass)) {
mAuth.signInWithEmailAndPassword(email, pass).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
#Override
public void onComplete(#NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
checkUserExists();
}
}
});
}
}
public void checkUserExists() {
final String user_id = mAuth.getCurrentUser().getUid();
mDatabase.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.hasChild(user_id)) {
Intent loginIntent = new Intent(LoginActivity.this, MainActivity.class);
startActivity(loginIntent);
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
You need to override the OnClick method in the OnClickListener.
Your new code should be:
btnTest.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// your desired code goes here.
}
});
edit, Solution:
btnTest.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
public void changePassVisibility (View view)
btnTest.setSelected(!btnTest.isPressed());
if (btnTest.isPressed()) {
btnTest.setImageResource(R.drawable.PassVisible);
} else {
btnTest.setImageResource(R.drawable.PassInvisible);
}
}
});
You need to implement onClick() method
btnTest.setOnClickListener(new View.OnClickListener()
{
//add this method
#Override
public void onClick(View v) {
// do something when the button is clicked
}
}
You have to set the listener like this
final ImageButton btnTest =(ImageButton)findViewById(R.id.passVisibilityStatus);
btnTest.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
btnTest.setSelected(!btnTest.isPressed());
if (btnTest.isPressed()) {
btnTest.setImageResource(R.drawable.PassVisible);
} else {
btnTest.setImageResource(R.drawable.PassInvisible);
}
}
});
you have to do as FlyingNades said, but I think your real problem is that you are not writing this code in his correct place, make sure your are calling "setOnClickListener" inside OnCreate() methode, this should resolve your issue.

How to change text in BaseAdapter

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

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

Pass a XML resource from one activity to another activity using java code in Android Studio?

I want to pass an XML resource from one activity to another activity using Java Code? I don't want to create separate different activities for different buttons.
ImageButton imageBttn = (ImageButton)findViewById(R.id.imageButton1);
imageBttn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, info.class));
}
});
Pseudocode to explain what I'm trying to do:
If BUTTON_1 is clicked
-Pass swirl.png to info.class
If BUTTON_2 is clicked
-Pass golden.png to info.class
If BUTTON_3 is clicked
-Pass arcade.png
imageBttn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, info.class)
intent.putExtra("image",R.drawable.ic_search_grey);
startActivity(intent);
}
});
On Mainactvitiy get the int drawable
int res = getIntent().getIntExtra("image", -1);
if(res > -1) {
Drawable drawable = getResources().getDrawable(res, null);
imgBtn.setImageDrawable(drawable);
}
InfoActivity.java
public class InfoActivity extends Activity {
private static final String EXTRA_IMAGE = "image";
public static void launch(Activity activity, #DrawableRes int imageResId) {
Intent intent = new Intent(activity, InfoActivity.class);
intent.putExtra(EXTRA_IMAGE, imageResId);
activity.startActivity(intent);
}
#Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_info);
int imageResId = getIntent().getIntExtra(EXTRA_IMAGE, -1);
if (imageResId == -1) {
throw new IllegalArgumentException(EXTRA_IMAGE);
// or set error/default image resource id
}
// ... something to do with imageResId
}
}
MainActivity.java
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
InfoActivity.launch(MainActivity.this, R.drawable.swirl);
}
});
button2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
InfoActivity.launch(MainActivity.this, R.drawable.golden);
}
});
button3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
InfoActivity.launch(MainActivity.this, R.drawable.arcade);
}
});
}

Categories

Resources