I want to return the value of choice in String prodName[] but I always got the value of choice is always null please help sorry for my bad English.
String prodName[]={choice};
int Quantity[] = {};
int Total[]={};
String Price[]={choice} ;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayAdapter<CharSequence> badapter = ArrayAdapter.createFromResource(
this,
R.array.products,
android.R.layout.simple_spinner_dropdown_item);
listView = (ListView) findViewById(R.id.list1);
button = (Button) findViewById(R.id.button);
spinner = (Spinner)findViewById(R.id.spinner);
badapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(badapter);
final MyAdapter adapter = new MyAdapter(this, prodName, Price);
//set the adapter
listView.setAdapter(adapter);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String sample;
choice = spinner.getSelectedItem().toString();
prodName = choice.split(choice);
}
});
}
I guess you're using split() method in the wrong way.
String demo = "1,2,3,4";
String[] splitted = demo.split(","); //splitted will be an array containing 4 elements [1, 2, 3, 4]
Now I guess you know what the split() method does. :)
Since you already got the selected item text as String in the variable choice. why are you calling split() method on it?
You can find more about the split() method here
Try below way to initialise array
String[] prodName={choice};
Related
The following code consist of the listview function for displaying data from sqlite.
The issue is that no matter which data i selected on, it will only delete the last data and not the data being selected. How can i go about solving the issue? please help.
Thank you.
Arealist.java
public class Arealist extends AppCompatActivity {
public static ListView listView;
public static ArrayList<Area> list;
AreaListAdapter adapter = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list);
listView = (ListView) findViewById(R.id.listView);
list = new ArrayList<>();
adapter = new AreaListAdapter(this, R.layout.area_items, list);
listView.setAdapter(adapter);
//get all data
Cursor cursor = MainActivity.sqLiteHelper.getData("SELECT * FROM AREA");
list.clear();
while (cursor.moveToNext()) {
int id = cursor.getInt(0);
double area = cursor.getDouble(1);
String date = cursor.getString(2);
list.add(new Area(id, area, date));
}
adapter.notifyDataSetChanged();
}
It seems your problem is in this line MainActivity.sqLiteHelper.deleteData(Area.getId());
Try with this
MainActivity.sqLiteHelper.deleteData(area.getId());
Make Area Object local.
set Area Id in delete buttons as a Tag and get it in onClick().
Add In getView()
holder.btnDelete.setTag(area.getId());
Add In OnClick()
final int areaId = view.getTag();
Add in AlertDialog:
MainActivity.sqLiteHelper.deleteData(areaId);
i want to use condition " if " for my array list
here is my code in (activity 1) to send string in textview with button to static variable that is in (exchange activity) :
Button btn1 = (Button) findViewById(R.id.btn1);
final TextView text = (TextView) findViewById(R.id.txt1);
btn1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
exchange.in =text.getText().toString();
}
});
here is my static variable in (exchange activity)
public static String in;
and here is my (activity 2) code that i use array list in it to receive & store string from (activity 1) that is in static variable (exchange.in)
ListView list;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayList<String> web = new ArrayList<String>(10);
for (int i = 0; i < 10; i++) {
web.add("");
}
CustomList adapter = new
CustomList(activity_main.this, web);
list = (ListView) findViewById(R.id.list);
list.setAdapter(adapter);
}
now i don't know how to store data from (exchange.in) to arraylist and then use if condition to recognize that, if arraylist(0) was full, put data that is in (exchange.in) to arraylist(1) and it will continue like this...
is anyone know about this?
I am confused that after adding more EditText field, how am i suppose to know the string code so i could do an addition math to add them all up.Thank You.
Update :
So I managed to use ArrayAdapter to add the data into an array but I am facing problem to call it back in. Help!!!
txt = (EditText) findViewById(R.id.iteminput);
show = (ListView) findViewById(R.id.listView);
save = (Button) findViewById(R.id.Add);
save.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String getInput = txt.getText().toString();
addArray.add(getInput);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(Splitter.this, android.R.layout.simple_list_item_1, addArray);
show.setAdapter(adapter);
((EditText) findViewById(R.id.iteminput)).setText("");
}
});
public void Calculate(View v){
for(int i = 0; i<adapter.getCount() ; i++){
Integer Total= adapter.getItem(i);
}
TextView Total=(TextView)findViewById(R.id.Total);
Total.setText("Splitted Cost: "+Total.toString());
}
The getCount couldnt get the adaptor :(
Just declare your adapter as a field and you will be able to access it in a method.
public class MainActivity extends Activity {
private ArrayAdapter<String> adapter; // declared as a field now you can access it anywhere.
public void onCreate(){
txt = (EditText) findViewById(R.id.iteminput);
show = (ListView) findViewById(R.id.listView);
save = (Button) findViewById(R.id.Add);
save.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String getInput = txt.getText().toString();
addArray.add(getInput);
adapter = new ArrayAdapter<String>(Splitter.this, android.R.layout.simple_list_item_1, addArray);
show.setAdapter(adapter);
((EditText) findViewById(R.id.iteminput)).setText("");
}
});
}
}
I need to set the open_time string value to spinner.In previous
activity I'm getting the response as string.
then I passed that string value to bundle from FirstActivity to
SecondActivity.
Coding :
SecondActivity.java:
String open_time;
Spinner staticSpinner = (Spinner) findViewById(R.id.static_spinner);
Bundle bundle = getIntent().getExtras();
if (bundle != null) {
open_time = bundle.getString("open_time");
}
My only problem is ,While entering into SecondActivity the open_time
String value have to set into Spinner.Anyone help me with this.
Edit: I have tried fromSpinner.setPrompt(open_time); .But that doesn't worked for me.
Android Spinners need adapters. So you should prepare a simple adapter for them.
String[] list = new String[] {open_time};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, list);
staticSpinner.setAdapter(adapter);
I solved it by using this below code:
int s3;
s3 = fromSpinnerAdapter.getPosition(open_time);
fromSpinner.setSelection(s3);
Try this code
private ArrayList<String> list;
private int selectedItem = -1;
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, list);
staticSpinner.setAdapter(adapter);
staticSpinner.setOnItemSelectedListener(this);
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
Spinner selectedSpinner = (Spinner) adapterView;
switch (selectedSpinner.getId()) {
case R.id.sp_actions:
String row_data = sp_actions.getSelectedItem();
Log.d(TAG, "" + row_data);
// pass these variable to new activity and call setSelected method
selectedItem = list.indexOf(row_data);
break;
}
}
`public class GPA extends Activity{
private Spinner GradeSpinner1;
private TextView Total1;
private EditText EditCH1;
double chEntered;
double totalPoints;
String grade;
String groupChoice;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_gpa);
groupChoice = GradeSpinner1.getSelectedItem().toString();
GradeSpinner1 = (Spinner) findViewById(R.id.spGP1);
Total1 = (TextView) findViewById(R.id.editTotal1);
EditCH1 = (EditText) findViewById(R.id.editCH1);
List<String> list = new ArrayList<String>();
list.add("--SELECT--");
list.add("A+");
list.add("A");
list.add("A-");
list.add("B+");
list.add("B");
list.add("B-");
list.add("C+");
list.add("C");
list.add("C-");
list.add("D+");
list.add("D");
list.add("F");
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, list);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
GradeSpinner1.setAdapter(dataAdapter);
setUpListeners();
}
private void setUpListeners() {
GradeSpinner1.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view,
int pos, long id) {
String credit = (EditCH1.getText().toString());
Double chEntered = Double.parseDouble(credit);
if(grade.equals("A")){
Double editTotal1 = chEntered * 4.00;
Total1.setText(editTotal1.toString());
}
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
}
}
`I want to do if/else statement for spinner and edit text. This is what I have done. No errors bt cnt run. Here is my java code.Can anyone help me? Please. thank you in advance.
Don´t know where the problem is, You have to add the stacktrace. But Your code has to be reformed inside onItemSelected:
String grade = parent.getItemAtPosition(pos).getText().toString(); //first mistake
String editTextText = EditCH1.getText().toString();
//check if String is null or empty before parsing. otherwise You will get an error if it is null or empty
if(editTextText!=null && !editTextText.equals("")){
chEntered = Double.parseDouble(editTextText);
}
if(grade.equals("A")){
totalPoints = chEntered*4.0;
String points = Double.toString(totalPoints);
Total1.setText(points);
}
}
And Your EditText should have input Type in xml layout android:inputType="numberDecimal"
Another simple test if the String is a digit is to check with TextUtils:
boolean digitOnly = android.text.TextUtils.isDigitsOnly(editTextText);
But maybe this doesn´t work on decimal numbers. There is a very simple solution:
private boolean isNumber(String numStr)
{
try
{
double d = Double.parseDouble(numStr);
}
catch(NumberFormatException nfe)
{
return false;
}
return true;
}
And Your "App has stopped" description could be caused from EVERYTHING!. There are so many possibillities....It´s important to see Your stacktrace and You have to post a part of code instead of link to a picture please.