use if-statement to recognize arraylist data in android - android

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?

Related

Return value of string 3

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

Generating a ListView Using ArrayAdapter [duplicate]

This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 5 years ago.
I am trying to populate a ListView using an ArrayAdapter that I am filling with input from an EditText. My program seems to compile fine but during app start up it immediately crashes. I suspect that it has something to do with me trying to set-up my list view. I am not sure what I am initializing wrong such that my app instantly crashes. Any and all tips would be greatly appreciated. Thanks in advance!
This is my global declarations for my ArrayList and Adapter.
ArrayList<scoreScreen> savedScores = new ArrayList<>();
ScoreAdapter adapter = new ScoreAdapter(this, savedScores);
ListView listView = (ListView) findViewById(R.id.dispScores);
listView.setAdapter(adapter);
My Adapter Class:
private class ScoreAdapter extends ArrayAdapter<scoreScreen> {
private ScoreAdapter(Context context, ArrayList<scoreScreen> scores) {
super(context, 0, scores);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
scoreScreen score1 = getItem(position);
if (convertView == null){
convertView = LayoutInflater.from(getContext()).inflate(R.layout.item_scores, parent, false);
}
TextView holeNum = (TextView) convertView.findViewById(R.id.holeNum);
holeNum.setText(score1.hole);
return convertView;
}
}
My ListView inside of my onCreate method.
ListView listview = (ListView) findViewById(R.id.listView1);
listview.setAdapter(adapter);
I am assuming my problem is not inside my EditText inputs since they are inside of an OnClickListener method, but just incase I have attached it below.
public View.OnClickListener onClickListener = new View.OnClickListener() {
#Override
public void onClick(View view) {
EditText input1 = (EditText) findViewById(R.id.scorePrompt);
TextView output1 = (TextView) findViewById(R.id.textTotal);
String blankCheck = input1.getText().toString(); //CHANGE INPUT IN scorePrompt TO STRING
switch (view.getId()) {
case R.id.buttTotal:
if (blankCheck.equals("")) {
Toast blankError = Toast.makeText(getApplicationContext(), "YOU CANT SKIP HOLES JERK", Toast.LENGTH_LONG);
blankError.show();
break;
} else {
int num1 = Integer.parseInt(input1.getText().toString()); //Get input from text box
int sum = num1 + score2;
score2 = sum;
output1.setText("Your score is : " + Integer.toString(sum));
//savedScores.add(input1.getText().toString());
scoreScreen addScore = new scoreScreen("Score is" + num1);
adapter.add(addScore);
j++;
input1.setText(""); //Clear input text box
break;
}
case R.id.allScores: //CHANGE THIS TO AN EDIT BUTTON, ADD A HOLE NUMBER COUNTER AT TOP OF SCREEN!!!!!
output1.setText("you messed up");
break;
case R.id.editScore: //Need to set up Save Array before we can edit
//CURRENTLY ONLY DISPLAYS THE LAST NUNMBER IN THE TEXTEDIT, NEED TO SET UP LISTVIEW!!!!!!
for (int i=0; i < j; i++){
// output1.setText(savedScores.get(i));
} break;
}
}
};
onCreate method added as requested:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/*Button scoresAct = (Button) findViewById(R.id.allScores); //THIS IS TO GO TO ALL SCORES ACTIVITY
scoresAct.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent scoreScreen = new Intent(MainActivity.this, AllScoresAct.class);
startActivity(scoreScreen);
}
});*/
Button sumScores = (Button) findViewById(R.id.buttTotal);
Button saveScores = (Button) findViewById(R.id.allScores);
Button changeScores = (Button) findViewById(R.id.editScore);
sumScores.setOnClickListener(onClickListener);
saveScores.setOnClickListener(onClickListener);
changeScores.setOnClickListener(onClickListener);
ListView listview = (ListView) findViewById(R.id.listView1);
listview.setAdapter(adapter);
}
After moving my adapter and ArrayList into my onCreate, I get a new error. I did some research on null pointers, but I have already initialized both of these. Below is my logcat, any ideas? Thanks
Make sure super.onCreate(savedInstanceState) is the first thing you are calling in your onCreate() and also not trying to access any view from the layout xml before setContentView().
EDIT:
Initialize the adapter in onCreate() method instead of doing it globally.
onCreate() {
.......
this.adapter = new ScoreAdapter(this, savedScores);
}
This is my global declarations for my ArrayList and Adapter.
ArrayList<scoreScreen> savedScores = new ArrayList<>();
ScoreAdapter adapter = new ScoreAdapter(this, savedScores);
You can declare those globally, but for the Adapter, the this parameter must be used within/after onCreate, as the error says
System services not available to Activities before onCreate()
For example
private ArrayList<ScoreScreen> savedScores = new ArrayList<>();
private ScoreAdapter adapter;
#Override
protected void onCreate(Bundle b) {
...
adapter = new ScoreAdapter(this, savedScores);
}

Adding EditText with button with input later used to do addition

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

How to send contents of Listview to another Activity?

I have a RelativeLayout with two TextViews inside it,the layout looks like a button. The user presses the "button" and I send one of the TextViews to a resulting TextView and the second TextView I put in a list. This is all done on the same activity. The user can keep pressing the "button" and it will populate the ListView with many items. My question is how do I send what's been populated in that resulting TextView and the ListView to a new activity. I'm able to send contents of the resulting TextView just fine but not getting anywhere with sending the contents of the ListView to new Activity.
This is the first Activity
public class MenuView1Activity extends ListActivity {
private double overallTotalproduct;
public static TextView resultTextView;
ArrayList<String> listItems=new ArrayList<String>();
ArrayAdapter<String> adapter;
private Button whateverButton;
TextView inputPrice;
RelativeLayout lay1;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.menuview);
adapter=new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1,
listItems);
setListAdapter(adapter);
whateverButton = (Button)findViewById(R.id.whateverButton);
inputPrice= (TextView)findViewById(R.id.inputPrice);
lay1= (RelativeLayout)findViewById(R.id.lay1);
//Total Box
final TextView textViewtotalproduct = (TextView) findViewById(R.id.inputPrice);
final TextView textView1 = (TextView) findViewById(R.id.m1aa);
final String stringm1aa = textView1.getText().toString();
final double intm1aa = Double.parseDouble(stringm1aa);
final TextView textView1a = (TextView) findViewById(R.id.m1a);
final String stringm1a = textView1a.getText().toString();
lay1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
listItems.add("$"+intm1aa +"-"+ stringm1a);
adapter.notifyDataSetChanged();
resultTextView.setText(stringm1a);
overallTotalproduct = intm1aa + overallTotalproduct;
textViewtotalproduct.setText(String.valueOf(overallTotalproduct));
}
});
public void onwhateverPress(View v) {
Intent whateverIntent = new Intent(this, WhateverActivity.class);
if (whateverResult.iswhateveranicewhatever()) {
final TextView daplane =(TextView)findViewById(R.id.date);
String watch = daplane.getText().toString();
startActivity(new Intent(MenuView1Activity.this,RecordCheckActivity.class)
.putExtra("date",(CharSequence)watch)
.putStringArrayListExtra("list", (ArrayList<String>) listItems));
finish();
And the Second one
Intent id11 = getIntent();
if (id11.getCharSequenceExtra("list") != null) {
final TextView setmsg = (TextView)findViewById(R.id.saleNotes);
setmsg.setText(id11.getCharSequenceExtra("list"));
}
you can get all updated items from ListView using ListView.getAdapter() then use Intent.putStringArrayListExtra for sending ArrayList from one Activity to other:
startActivity(new Intent(MenuView1Activity.this,RecordCheckActivity.class)
.putExtra("date",(CharSequence)watch)
.putStringArrayListExtra("list", (ArrayList<String>)getListView().getAdapter()));
finish();

Android Get Checkbox Info from ListView

I have a ListView with multiple checkboxes (one for each list item). What would be the best way to create an array containing information from the selected checkboxes.
For Example, here's a list.
Item 1 "Ham" Checked
Item 2 "Turkey" NotChecked
Item 3 "Bread" Checked
I would like to create an array containing "ham" and "turkey"
If you used ListView's built-in checkbox method with setChoiceMode() then you only need to call getCheckedItemIds() or getCheckedItemPositions().
public class Example extends Activity implements OnClickListener {
ListView mListView;
String[] array = new String[] {"Ham", "Turkey", "Bread"};
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_checked, array);
mListView = (ListView) findViewById(R.id.list);
mListView.setAdapter(adapter);
mListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(this);
}
public void onClick(View view) {
SparseBooleanArray positions = mListView.getCheckedItemPositions();
int size = positions.size();
for(int index = 0; index < size; index++) {
Log.v("Example", "Checked: " + array[positions.keyAt(index)]);
}
}
}
If the Adapter is bound to a Cursor, then this becomes much more practical.

Categories

Resources