0
  public class RenkKorluguTesti extends Activity {
  /** Called when the activity is first created. */

ImageView imageView1;
EditText editText1=null;
Button button1;
@Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.galeri);

    imageView1 = (ImageView) findViewById(R.id.imageView1);
    editText1 = (EditText) findViewById(R.id.editText1);
    button1 = (Button) findViewById(R.id.button1);


    Gallery gallery = (Gallery) findViewById(R.id.gallery);
    gallery.setAdapter(new ImageAdapter(this));

    gallery.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView parent, View v, int position, long id) {

            switch(position) {

            case 0:{
                imageView1.setImageResource(R.drawable.plate01); 
                break;
            }
            case 1: imageView1.setImageResource(R.drawable.plate02); break;
            case 2: imageView1.setImageResource(R.drawable.plate03); break;

            }
            //Toast.makeText(RenkKorluguTesti.this, "" + position, Toast.LENGTH_SHORT).show();
        }
    });




   }

public void tikla(View v) {
    if(editText1.getText().toString()=="12")
        editText1.setText("yov");
        //Toast.makeText(RenkKorluguTesti.this, "Birinci testi geçtiniz.",Toast.LENGTH_SHORT).show();


}

public class ImageAdapter extends BaseAdapter {
    int mGalleryItemBackground;
    private Context mContext;

    private Integer[] mImageIds = {
            R.drawable.plate01,
            R.drawable.plate02,
            R.drawable.plate03,

    };

    public ImageAdapter(Context c) {
        mContext = c;
        TypedArray attr = mContext.obtainStyledAttributes(R.styleable.RenkKorluguTesti);
        mGalleryItemBackground = attr.getResourceId(
                R.styleable.RenkKorluguTesti_android_galleryItemBackground, 0);
        attr.recycle();
    }

    public int getCount() {
        return mImageIds.length;
    }

    public Object getItem(int position) {
        return position;
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView imageView = new ImageView(mContext);

        imageView.setImageResource(mImageIds[position]);
        imageView.setLayoutParams(new Gallery.LayoutParams(150, 100));
        imageView.setScaleType(ImageView.ScaleType.FIT_XY);
        imageView.setBackgroundResource(mGalleryItemBackground);

        return imageView;
    }
}




 }

I want to make a Toast if the text written is equal 12 in EditText box. I used editText1.getText().toString() method but it seems not working. What's wrong? Could you please check public void tikla() method? Thx.

3 Answers3

0

This comparison in your code is not correct.

(editText1.getText().toString()=="12")

Comparing Strings is done via equals() not == operator.

hovanessyan
  • 30,580
  • 6
  • 55
  • 83
  • I also would like to add one more condition to my if statement,how can i do that?.public void tikla(View v) { if(editText1.getText().toString().equals("12") /*&& if the first image in gallery is chosen*/) Toast.makeText(RenkKorluguTesti.this, "Birinci testi geçtiniz.", Toast.LENGTH_SHORT).show(); } – Figen Gungor Feb 22 '12 at 14:40
0

On Java you cannot do this kind of comparation:

if(editText1.getText().toString()=="12")

You need to do:

if(editText1.getText().toString().equalsIgnoreCase("12"))

or

if(editText1.getText().toString().compareTo("12")==0)
Derzu
  • 7,011
  • 3
  • 57
  • 60
  • I also would like to add one more condition to my if statement,how can i do that?.public void tikla(View v) { if(editText1.getText().toString().equals("12") /*&& if the first image in gallery is chosen*/) Toast.makeText(RenkKorluguTesti.this, "Birinci testi geçtiniz.", Toast.LENGTH_SHORT).show(); } – Figen Gungor Feb 22 '12 at 14:31
  • if (editText1.getText().toString().equals("12") && pathImage1!=null ) But you need to have a String variable (pathImage1) to store the path of the selected image. I don't saw this in you code sample. – Derzu Feb 22 '12 at 18:00
  • Thank you but,i want to get the selected position number not the path.Do you know how can i reach that? – Figen Gungor Feb 23 '12 at 15:35
  • @FigenGungor Do you wanna to get position number (index) of a image in the Gallery? If is that, see the method getLastImageId, in this post: http://stackoverflow.com/questions/4184951/get-path-of-image-from-action-image-capture-intent/9067224#9067224 – Derzu Feb 23 '12 at 18:27
0

You can do something like this:

if (editText1.getText().toString().compareTo("12") == 0)

using the java.lang.String.compareTo(java.lang.String) method.

Romain R.
  • 920
  • 12
  • 21
  • I also would like to add one more condition to my if statement,how can i do that?.public void tikla(View v) { if(editText1.getText().toString().equals("12") /*&& if the first image in gallery is chosen*/) Toast.makeText(RenkKorluguTesti.this, "Birinci testi geçtiniz.", Toast.LENGTH_SHORT).show(); } – Figen Gungor Feb 22 '12 at 14:42