Can't resolve setOnItemSelectedListener - java

My goal is to make items from spinner list to change background color of main_activity.
But I can't get working setOnItemSelectedListener for some reason.
Spinner works fine, toast also, but setOnItemSelectedListener is getting error: "cant resolve the symbol..."
Couldn't find any help online that was applicable to my case.
Please see code below:
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Spinner;
import android.widget.AdapterView;
import android.widget.TextView;
import android.widget.Button;
import android.widget.ToggleButton;
import android.widget.ArrayAdapter;
import android.widget.Toast;
import java.util.*;
public class MainActivity extends AppCompatActivity implements
AdapterView.OnItemSelectedListener {
private TextView tekst1;
private int ile = 1;
private Button klawisz1;
private ToggleButton onOff;
private Spinner spinner1;
public void setActivityBackgroundColor(int color) {
View view = this.getWindow().getDecorView();
view.setBackgroundColor(color);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tekst1 = (TextView) findViewById(R.id.tekst);
klawisz1 = (Button) findViewById(R.id.button);
onOff = (ToggleButton) findViewById(R.id.toggleButton);
spinner1 = (Spinner) findViewById(R.id.spinner);
spinner1.setOnItemSelectedListener((AdapterView.OnItemSelectedListener) this);
spinner1.setOnItemSelectedListener(this);
List<String> colors = new ArrayList<String>();
colors.add("White");
colors.add("Yellow");
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, colors);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner1.setAdapter(dataAdapter);
onOff.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if ((++ile%2)==0) {
spinner1.setVisibility(View.VISIBLE);
}
else{
spinner1.setVisibility(View.GONE);
}
}
});
}
spinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
{
public void onItemSelected(AdapterView<?> parent, View view, int position,
long id) {
// On selecting a spinner item
//setActivityBackgroundColor(Color.YELLOW);
String item = parent.getItemAtPosition(position).toString();
if(selectedItem.equals("White"))
{
setActivityBackgroundColor(Color.WHITE);
}
else if(selectedItem.equals("Yellow"))
{
setActivityBackgroundColor(Color.YELLOW);
}
// Showing selected spinner item
//Toast.makeText(parent.getContext(), "Selected: " + item, Toast.LENGTH_LONG).show();
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
public void buttonOnClick(View view) {
if ((++ile%2)==0){
tekst1.setText(new Date().toString());
klawisz1.setBackgroundColor(Color.RED);
}
else
{
tekst1.setText("Next click - TIME");
klawisz1.setBackgroundColor(Color.GREEN);
}
}

You have an extra } before the setOnItemSelectedListener() call and the code is outside of the onCreate() method. Code like spinner1.setOnItemSelectedListener(...) needs to be inside a method.

Try below code for your Spinner:
mTypeSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
// Do your stuff at item selection time.
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
// Intentionally kept blank
}
});

It's becoz you have set setOnItemSelectedListener two times for spinner 1
\\ spinner1.setOnItemSelectedListener((AdapterView.OnItemSelectedListener)
this); \\not required
spinner1.setOnItemSelectedListener(this);
you should set only 1 setOnItemSelectedListener for spinner1

This code worked fine for me.Try it...
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ToggleButton;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
public class MainActivity extends AppCompatActivity implements
AdapterView.OnItemSelectedListener {
List<String> colors;
private TextView tekst1;
private int ile = 1;
private Button klawisz1;
private ToggleButton onOff;
private Spinner spinner1;
public void setActivityBackgroundColor(int color) {
View view = this.getWindow().getDecorView();
view.setBackgroundColor(color);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tekst1 = (TextView) findViewById(R.id.tekst);
klawisz1 = (Button) findViewById(R.id.button);
onOff = (ToggleButton) findViewById(R.id.toggleButton);
spinner1 = (Spinner) findViewById(R.id.spinner);
klawisz1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
buttonOnClick(view);
}
});
spinner1.setOnItemSelectedListener(this);
colors = new ArrayList<String>();
colors.add("White");
colors.add("Yellow");
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, colors);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner1.setAdapter(dataAdapter);
onOff.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if ((++ile % 2) == 0) {
spinner1.setVisibility(View.VISIBLE);
} else {
spinner1.setVisibility(View.GONE);
}
}
});
}
public void onItemSelected(AdapterView<?> parent, View view, int position,
long id) {
// On selecting a spinner item
//setActivityBackgroundColor(Color.YELLOW);
String item = colors.get(position);
if (item.equals("White")) {
setActivityBackgroundColor(Color.WHITE);
} else if (item.equals("Yellow")) {
setActivityBackgroundColor(Color.YELLOW);
}
// Showing selected spinner item
//Toast.makeText(parent.getContext(), "Selected: " + item,
//Toast.LENGTH_LONG).show();
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
public void buttonOnClick(View view) {
if ((++ile % 2) == 0) {
tekst1.setText(new Date().toString());
klawisz1.setBackgroundColor(Color.RED);
} else {
tekst1.setText("Next click - TIME");
klawisz1.setBackgroundColor(Color.GREEN);
}
}
}

Related

Click on one card view affected another card view value

When I create a custom card view in recycler view and click the increment button counter work properply. But when I crete another cardview then already created card counter value reset to 0. How to solve this issue?
Here's my Main Activity code
package com.example.muhasbaapp;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import java.lang.reflect.Type;
import java.util.ArrayList;
import com.example.muhasbaapp.CustomAdapter.ViewHolder.*;
public class MainActivity extends AppCompatActivity {
private static RecyclerView.Adapter mAdapter,countAdapter;
private RecyclerView.LayoutManager layoutManager,layoutManager2;
private static RecyclerView recyclerView,recyclerView2;
private ImageView imageView;
public CustomModel customModel;
public static ArrayList<String> input = new ArrayList<>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// myOnClickListener = new MyOnClickListener(this);
recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
recyclerView2=(RecyclerView)findViewById(R.id.recycler_view2);
recyclerView.setHasFixedSize(true);
recyclerView2.setHasFixedSize(true);
layoutManager2=new LinearLayoutManager(this);
recyclerView2.setLayoutManager(layoutManager2);
recyclerView2.setItemAnimator(new DefaultItemAnimator());
mAdapter= new CustomAdapter(input);
recyclerView2.setAdapter(mAdapter);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.more_action_menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(#NonNull MenuItem item) { //add action2 kau kat sini
switch (item.getItemId()) {
case R.id.action_item_1:
openCustomDialoge();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
private void openCustomDialoge() {
final AlertDialog dialogBuilder = new AlertDialog.Builder(this).create();
LayoutInflater inflater = this.getLayoutInflater();
View dialogView = inflater.inflate(R.layout.user_generated_cards, null);
final EditText editText = (EditText) dialogView.findViewById(R.id.edt_comment);
Button button1 = (Button) dialogView.findViewById(R.id.buttonSubmit);
Button button2 = (Button) dialogView.findViewById(R.id.buttonCancel);
button2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
dialogBuilder.dismiss();
}
});
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String s=editText.getText().toString();
if (s.isEmpty()){
Toast.makeText(MainActivity.this, "You can't enter empty spaces", Toast.LENGTH_SHORT).show();
}else {
input.add(s);
recyclerView2.setAdapter(mAdapter);
dialogBuilder.dismiss();
}
}
});
dialogBuilder.setView(dialogView);
dialogBuilder.show();
}
}
And Here' my Adaptar Class code
package com.example.muhasbaapp;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
public class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.ViewHolder> {
public ArrayList<String> values;
final CustomModel customMdl=new CustomModel();
final MainActivity mainActivity=new MainActivity();
// Provide a reference to the views for each data item
// Complex data items may need more than one view per item, and
// you provide access to all the views for a data item in a view holder
public class ViewHolder extends RecyclerView.ViewHolder {
// each data item is just a string in this case
public TextView tv;
public View layout;
public TextView counter_value;
public ImageView increment,decrement, deleted;
public int counter;
public ViewHolder(View v) {
super(v);
layout = v;
tv = (TextView) v.findViewById(R.id.textViewName);
counter_value=(TextView) v.findViewById(R.id.counter);
increment=(ImageView)v.findViewById(R.id.increment);
decrement=(ImageView)v.findViewById(R.id.decrement);
deleted=(ImageView)v.findViewById(R.id.delbtn);
}
}
public void add(int position, String item) {
values.add(position, item);
notifyItemInserted(position);
}
public void remove(int position) {
values.remove(position);
notifyItemRemoved(position);
notifyItemRangeChanged(position, values.size());
}
// Provide a suitable constructor (depends on the kind of dataset)
public CustomAdapter(ArrayList<String> myDataset) {
this.values = myDataset;
}
// Create new views (invoked by the layout manager)
#Override
public CustomAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
int viewType) {
// create a new view
LayoutInflater inflater = LayoutInflater.from(
parent.getContext());
View v =
inflater.inflate(R.layout.custom_cards_layout, parent, false);
// set the view's size, margins, paddings and layout parameters
ViewHolder vh = new ViewHolder(v);
return vh;
}
// Replace the contents of a view (invoked by the layout manager)
#Override
public void onBindViewHolder(ViewHolder holder, final int position) {
// - get element from your dataset at this position
// - replace the contents of the view with that element
final String name = values.get(position);
holder.tv.setText(name);
final int[] counter={0};
holder.deleted.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
remove(position);
}
});
holder.decrement.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (counter[0] == 0) {
Toast.makeText(v.getContext(), "Can't add less than 0", Toast.LENGTH_SHORT).show();
} else {
counter[0] -= 1;
holder.counter_value.setText(String.valueOf(counter[0]));
}
}
});
holder.increment.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
holder.decrement.setEnabled(true);
counter[0] += 1;
holder.counter_value.setText(String.valueOf(counter[0]));
}
});
}
// Return the size of your dataset (invoked by the layout manager)
#Override
public int getItemCount() {
return values.size();
}
}
If anyone knows this problem please inform me.
Thanks in advance
Well, I'm seeing some bad practices in your code:
Adapters hosted in main activity must be not static, what do you think if try accessing via recyclerView.getAdapter()
Please remove the next one in your adapter
final MainActivity mainActivity = new MainActivity();
So the problem that you are having is inside your click listener.
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String s = editText.getText().toString();
if (s.isEmpty()) {
Toast.makeText(MainActivity.this, "You can't enter empty spaces", Toast.LENGTH_SHORT).show();
} else {
input.add(s); // <-------------- HERE
recyclerView2.setAdapter(mAdapter);
dialogBuilder.dismiss();
}
}
});
You add the new item to the array, however, the adapter never knows that there is a new item has been added.
Access to the instanced adapter which has been attached to RecyclerView in onCreate() step and update their data.

How can I also make enter add input text in edittext to my listview? On top of the created add button

I have a button "add" which adds input text from the editText into a listview below. However, can I make it possible to add text to the created list by simply pressing enter? If so, how can i do that in a simple manner?
Name=edittext, list =listview
#Override
public void onClick(View view) {
String name = Name.getText().toString();
if (Name.length() > 0) {
list.add(name);
adapter.notifyDataSetChanged();
}
}
Quoted from : https://www.stackoverflow.com/a/6832095
Here is what you need to do to capture Enter event on editText :
final EditText edittext = (EditText) findViewById(R.id.edittext);
edittext.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
// If the event is a key-down event on the "enter" button
if ((event.getAction() == KeyEvent.ACTION_DOWN) &&
(keyCode == KeyEvent.KEYCODE_ENTER)) {
// Perform action on key press
Toast.makeText(HelloFormStuff.this, edittext.getText(), Toast.LENGTH_SHORT).show();
return true;
}
return false;
}
});
You need to overwrite your dispatchKeyEvent something like this
#Override
public boolean dispatchKeyEvent(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.KEYCODE_ENTER) {
Toast.makeText(UITestsActivity.this,
"YOU CLICKED ENTER KEY",
Toast.LENGTH_LONG).show();
return true;
}
return super.dispatchKeyEvent(e);
};
Too long for a comment, therefore adding an answer. Building over previous answer, to achieve your function use this code:
import android.app.LauncherActivity;
import android.content.Context;
import android.content.Intent;
import android.hardware.input.InputManager;
import android.support.constraint.ConstraintLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.view.KeyEvent;
import java.util.ArrayList;
public class LoginPage extends AppCompatActivity {
EditText Name;
Button Add;
ListView Lv;
Button Reset;
Button Sgame;
private ImageView information;
private PopupWindow popupWindow;
private LayoutInflater layoutInflater;
private RelativeLayout relativeLayout;
ArrayList<String> list = new ArrayList<String>();
ArrayAdapter<String> adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login_page);
Name = (EditText) findViewById(R.id.Playername);
Add = (Button) findViewById(R.id.Addbutton);
Lv = (ListView) findViewById(R.id.list);
Reset = (Button) findViewById(R.id.Resetbutton);
Sgame = (Button) findViewById(R.id.Startgamebutton);
information = (ImageView) findViewById(R.id.info);
relativeLayout = (RelativeLayout) findViewById(R.id.constrain);
adapter = new ArrayAdapter<String>(this, R.layout.listviewlayout, R.id.list_content, list);
Lv.setAdapter(adapter);
final EditText Name = (EditText) findViewById(R.id.Playername);
class MyKeyListener implements View.OnKeyListener {
#Override
public boolean onKey (View v, int keyCode, KeyEvent event) {
if ((event.getAction() == KeyEvent.ACTION_DOWN) &&
(keyCode == KeyEvent.KEYCODE_ENTER)) {
String name = Name.getText().toString();
if (Name.length() > 0) {
list.add(name);
adapter.notifyDataSetChanged();
}
return true;
}
return false;
}
}
Name.setOnKeyListener(new MyKeyListener());
Add.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String name = Name.getText().toString();
if (Name.length() > 0) {
list.add(name);
adapter.notifyDataSetChanged()
;
}
}
});
Reset.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
list.clear();
}
});
Sgame.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(!list.isEmpty())
openActivity2();
}
});
information.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
layoutInflater = (LayoutInflater) getApplicationContext().getSystemService(LAYOUT_INFLATER_SERVICE);
ViewGroup container = (ViewGroup) layoutInflater.inflate(R.layout.informationpopup, null);
popupWindow = new PopupWindow(container, 1150, 2000, true);
popupWindow.showAtLocation(relativeLayout, Gravity.NO_GRAVITY, 140, 300);
container.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View view, MotionEvent motionEvent) {
popupWindow.dismiss();
return true;
}
});
}
});
}
public void openActivity2() {
Intent intent = new Intent(this, Gamescreen.class);
String s= list.get(0);
intent.putExtra("Name1", s);
startActivity(intent);
}}

how to check radio buttons programatically when the scanned qr match with text in the listview

I am developing attendance system and i have a listview with students name i want to programatically check or uncheck the radio buttons in the listview when the scanned qr matchs the student name I tried to do it in the ontextchengeListner method of txtresult but the text is changing many time even in reading a single qr
This are my java classes
CustomAdapter.java
package com.attendance.olana.qr_scanner;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CompoundButton;
import android.widget.RadioButton;
import android.widget.TextView;
import java.util.ArrayList;
/**
* Created by olana on 23/03/2018.
*/
public class CustomAdapter extends BaseAdapter {
Context context;
String[] questionsList;
public static String message;
LayoutInflater inflter;
public static TextView question ;
public static RadioButton yes,no ;
public static ArrayList<String> selectedAnswers;
public CustomAdapter(Context applicationContext, String[] questionsList) {
this.context = context;
this.questionsList = questionsList;
// initialize arraylist and add static string for all the questions
selectedAnswers = new ArrayList<>();
for (int i = 0; i < questionsList.length; i++) {
selectedAnswers.add("Not Attempted");
}
inflter = (LayoutInflater.from(applicationContext));
}
#Override
public int getCount() {
return questionsList.length;
}
#Override
public Object getItem(int i) {
return null;
}
#Override
public long getItemId(int i) {
return 0;
}
#Override
public View getView(final int i, View view, ViewGroup viewGroup) {
view = inflter.inflate(R.layout.main, null);
// get the reference of TextView and Button's
question = (TextView) view.findViewById(R.id.question);
yes = (RadioButton) view.findViewById(R.id.yes);
no = (RadioButton) view.findViewById(R.id.no);
// perform setOnCheckedChangeListener event on yes button
yes.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// set Yes values in ArrayList if RadioButton is checked
if (isChecked)
selectedAnswers.set(i, "Present");
}
});
// perform setOnCheckedChangeListener event on no button
no.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// set No values in ArrayList if RadioButton is checked
if (isChecked)
selectedAnswers.set(i, "Absent");
}
});
// set the value in TextView
question.setText(questionsList[i]);
return view;
}
}
list.java
package com.attendance.olana.qr_scanner;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.Toast;
/**
* Created by olana on 23/03/2018.
*/
public class list extends AppCompatActivity {
ListView simpleList;
public static String[] questions;
Button submit,scan;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list);
scan =(Button)findViewById(R.id.scan);
// get the string array from string.xml file
questions = getResources().getStringArray(R.array.student);
// get the reference of ListView and Button
simpleList = (ListView) findViewById(R.id.simpleListView);
submit = (Button) findViewById(R.id.submit);
// set the adapter to fill the data in the ListView
CustomAdapter customAdapter = new CustomAdapter(getApplicationContext(), questions);
simpleList.setAdapter(customAdapter);
scan.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i;
i = new Intent(list.this,MainActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(i);
}
});
// perform setOnClickListerner event on Button
submit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String message = "";
// get the value of selected answers from custom adapter
for (int i = 0; i < CustomAdapter.selectedAnswers.size(); i++) {
message = message + "\n" + (i + 1) + " " + CustomAdapter.selectedAnswers.get(i);
}
// display the message on screen with the help of Toast.
Toast.makeText(getApplicationContext(), message, Toast.LENGTH_LONG).show();
}
});
}
}
MainActivity.java
package com.attendance.olana.qr_scanner;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Vibrator;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.util.SparseArray;
import android.view.Menu;
import android.view.MenuItem;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import com.google.android.gms.vision.CameraSource;
import com.google.android.gms.vision.Detector;
import com.google.android.gms.vision.barcode.Barcode;
import com.google.android.gms.vision.barcode.BarcodeDetector;
import java.io.IOException;
import java.util.jar.Manifest;
public class MainActivity extends AppCompatActivity {
BarcodeDetector barcodeDetector;
SurfaceView camerapreview;
CameraSource cameraSource;
TextView txtresult;
String message;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button finish = (Button) findViewById(R.id.finish);
//Button scan = (Button) findViewById(R.id.scan);
finish.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
finish();
Toast.makeText(getApplicationContext(), message, Toast.LENGTH_LONG).show();
}
});
camerapreview = (SurfaceView) findViewById(R.id.camerapreview);
txtresult = (TextView) findViewById(R.id.txtresult);
barcodeDetector = new BarcodeDetector.Builder(this)
.setBarcodeFormats(Barcode.QR_CODE)
.build();
cameraSource = new CameraSource
.Builder(this, barcodeDetector)
.setRequestedPreviewSize(640, 480)
.build();
//add event
camerapreview.getHolder().addCallback(new SurfaceHolder.Callback() {
#Override
public void surfaceCreated(SurfaceHolder holder) {
try {
cameraSource.start(camerapreview.getHolder());
} catch (IOException e) {
e.printStackTrace();
}
}
#Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}
#Override
public void surfaceDestroyed(SurfaceHolder holder) {
cameraSource.stop();
}
});
barcodeDetector.setProcessor(new Detector.Processor<Barcode>() {
#Override
public void release() {
try {
cameraSource.start(camerapreview.getHolder());
} catch (IOException e) {
e.printStackTrace();
}
}
#Override
public void receiveDetections(Detector.Detections<Barcode> detections) {
final SparseArray<Barcode> qrcodes = detections.getDetectedItems();
if (qrcodes.size() != 0) {
txtresult.post(new Runnable() {
#Override
public void run() {
//create vibration
Vibrator vibrator = (Vibrator) getApplicationContext().getSystemService(Context.VIBRATOR_SERVICE);
vibrator.vibrate(100);
txtresult.setText(qrcodes.valueAt(0).displayValue);
//barcodeDetector.release();
}
});
}
}
});
}
}
Please tell me where i can implement these the programatically checking or unchecking the radio button when the name in the listview matchs the scanned qr please help me

Cannot Resolve OnClickListener

I am a highschool student in an Android App Development class. I was making an introduction slider for my app with buttons to go to the next activity. I unfortunately get the error cannot resolve symbol 'OnClickListener'. I made sure that this was all written under the OnCreate(); method and included the import statement for the OnClickListener(); but it still cannot resolve the symbol. Is there another import statement or was this misplaced? Below you will find my code.
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.text.Html;
import android.text.Layout;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.view.View.OnClickListener;
public class SecondActivity extends AppCompatActivity {
private ViewPagerAdapter viewPagerAdapter;
private ViewPager viewPager;
private IntroManager introManager;
private TextView [] dots;
private LinearLayout dotsLayout;
private int[] layouts;
Button next,skip;
View view;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
introManager = new IntroManager(this);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.second_activity);
if(!introManager.Check())
{
introManager.setFirst(false);
Intent intent = new Intent(SecondActivity.this,ThirdActivity.class);
startActivity(intent);
finish();
}
layouts= new int[]{R.layout.second_activity,R.layout.third_activity,R.layout.fourth_activity,R.layout.fifth_activity};
if(Build.VERSION.SDK_INT>=21){
getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE|View.SYSTEM_UI_FLAG_FULLSCREEN);
}
viewPager = (ViewPager) findViewById(R.id.view_pager);
dotsLayout= (LinearLayout) findViewById(R.id.layoutdots);
skip = (Button) findViewById(R.id.btn_skip);
next = (Button) findViewById(R.id.btn_next);
addBottomDots(0);
viewPagerAdapter= new ViewPagerAdapter();
viewPager.setAdapter(viewPagerAdapter);
viewPager.addOnPageChangeListener(viewListener);
skip.setOnClickListener(new view.OnClickListener(){
#Override
public void onClick(View view){
Intent intent = new Intent(SecondActivity.this,FifthActivity.class);
startActivity(intent);
finish();
}
});
next.setOnClickListener(new view.OnClickListener(){
#Override
public void onClick(View view){
int current = getItem(+1);
if(current<layouts.length){
viewPager.setCurrentItem(current);
}
else
{
Intent intent = new Intent(SecondActivity.this,ThirdActivity.class);
startActivity(intent);
finish();
}
}
});
}
private void addBottomDots(int position){
dots = new TextView[layouts.length];
int [] colorActive = getResources().getIntArray(R.array.dot_active);
int [] colorInactive = getResources().getIntArray(R.array.dot_inactive);
dotsLayout.removeAllViews();
for (int i = 0; i<dots.length; i++){
dots[i]= new TextView(this);
dots[i].setText(Html.fromHtml("&#8226;"));
dots[i].setTextSize(35);
dots[i].setTextColor(colorInactive[position]);
dotsLayout.addView(dots[i]);
}
if(dots[position].length()>0){
dots[position].setTextColor(colorActive[position]);
}
}
private int getItem(int i){
return viewPager.getCurrentItem() + 1;
}
ViewPager.OnPageChangeListener viewListener = new ViewPager.OnPageChangeListener(){
#Override
public void onPageScrolled(int position, float positionOffeet, int positionOffeetPixels){
}
#Override
public void onPageSelected(int position)
{
addBottomDots(position);
if (position==layouts.length-1){
next.setText("PROCEED");
skip.setVisibility(View.GONE);
}else{
next.setText("NEXT");
skip.setVisibility(View.VISIBLE);
}
}
#Override
public void onPageScrollStateChanged(int state)
{}
}
;
public void changetatusColor(){
if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.LOLLIPOP){
Window window = getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.setStatusBarColor(Color.TRANSPARENT);
}
}
public class ViewPagerAdapter extends PagerAdapter {
private LayoutInflater layoutInflater;
#Override
public Object instantiateItem(ViewGroup container, int position){
layoutInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = LayoutInflater.inflate(layouts[position],container,false);
container.addView(v);
return v;
}
#Override
public int getCount(){
return 0;
}
#Override
public boolean isViewFromObject(View view,Object object){
return view==object;
}
#Override
public void destroyItem(ViewGroup container, int position, Object object){
View v = (View) object;
container.removeView(v);
}
}
}
Do only one of those:
import android.view.View;
// note that View is uppercase
.setOnClickListener(new View.OnClickListener() {
import android.view.View.OnClickListener;
.setOnClickListener(new OnClickListener() {

Get item from spinner into url

I searched for an answer but couldn't find it.
The problem:
Depending on the selected spinner-item the application should show a different image.
At this moment I can't get it to work.
The Url works like this: "my.site.com/images/" imc_met ".png"
were imc_met is the filename.
I can't get it to work.
Btw the app isn't finished yet
package example.myapplication;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Spinner;
public class itemsActivity extends Activity {
private Spinner spinner1, spinner2;
private Button btnSubmit;
private Bitmap image;
private ImageView imageView;
private String imc_met, imc;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.items);
addItemsOnSpinner2();
addListenerOnButton();
addListenerOnSpinnerItemSelection();
}
// add items into spinner dynamically
public void addItemsOnSpinner2() {
spinner2 = (Spinner) findViewById(R.id.spinner2);
List<String> list = new ArrayList<String>();
list.add("list 1");
list.add("list 2");
list.add("list 3");
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, list);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner2.setAdapter(dataAdapter);
}
public void addListenerOnSpinnerItemSelection() {
spinner1 = (Spinner) findViewById(R.id.spinner1);
spinner1.setOnItemSelectedListener(new CustomOnItemSelectedListener());
}
// get the selected dropdown list value
public void addListenerOnButton() {
spinner1 = (Spinner) findViewById(R.id.spinner1);
spinner2 = (Spinner) findViewById(R.id.spinner2);
btnSubmit = (Button) findViewById(R.id.btnSubmit);
spinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
if(spinner1.getSelectedItem()!=null){
imc_met = spinner1.getSelectedItem().toString();
}
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
imageView = (ImageView)findViewById(R.id.ImageView01);
btnSubmit.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
URL url = null;
try {
url = new URL("my.site.com"); //here should the right link appear.
} catch (MalformedURLException e) {
e.printStackTrace();
}
try {
if (url != null) {
image = BitmapFactory.decodeStream(url.openStream());
}
} catch (IOException e) {
e.printStackTrace();
}
imageView.setImageBitmap(image);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
EDIT
Please post the whole code needed to be used to make the correct URL.
You call spinner1.setOnItemSelectedListener() twice! The first time (which should get the the item's value) is overwritten by the second time (which is CustomOnItemSelectedListener). Thus, the first listener won't be executed.
Remove the spinner1.setOnItemSelectedListener(new CustomOnItemSelectedListener());
(When you use setOnXxxListener(), only the last call will be used).
YOu should take one string object and on click of spinner item, assign it the value of it and then just use that in URL.
The problem here seems to be that your list values returned contains blank spaces in them which are not permissible in URLs.. Try something without spaces...
EDIT:- ok.. you havent set and OnItemSelectedListener on your spinner2 and have set 2 different ones on spinner1
public void addListenerOnSpinnerItemSelection() {
spinner1 = (Spinner) findViewById(R.id.spinner1);
spinner1.setOnItemSelectedListener(new CustomOnItemSelectedListener());
}
And
spinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
You have set data on spinner2 and added listener on spiner1. spiner1 has no data. Add listener on spiner2 and run it again

Categories

Resources