Here is the code...whenever I launch the app on my phone it just crashes. I'm new to android programming so please help me. After doing some tests it comes out the problem is when I asssign the value to Value1 and Value2(just a guess). please help me out here. Thanks in advance.
MainActivity.java
package com.example.android.add;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
EditText num1;
EditText num2;
Button add;
TextView ans;
int Value1;
int Value2;
int result;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
num1 = (EditText) findViewById(R.id.et1);
num2 = (EditText) findViewById(R.id.et2);
Value1 = Integer.parseInt(num1.getText().toString());
Value2 = Integer.parseInt(num2.getText().toString());
add = (Button) findViewById(R.id.addBtn);
ans = (TextView) findViewById(R.id.tv);
add.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
result = Value1 + Value2;
ans.setText(""+result);
}
});
}
}
and the activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.android.add.MainActivity">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:hint="Number 1"
android:textSize="40dp"
android:id="#+id/et1"
android:inputType="number"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:layout_below="#+id/et1"
android:hint="Number 2"
android:textSize="40dp"
android:id="#+id/et2"
android:inputType="number"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/addBtn"
android:layout_below="#+id/et2"
android:text="Add"
android:layout_marginLeft="80dp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Your Answer"
android:textSize="40dp"
android:layout_margin="40dp"
android:layout_below="#+id/addBtn"
android:id="#+id/tv"/>
</RelativeLayout>
You get the value before it inserted,you have to get value when user click on add button and get value1 and value2 inside onClickListner
change according to this
package com.example.android.add;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
EditText num1;
EditText num2;
Button add;
TextView ans;
int Value1;
int Value2;
int result;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
num1 = (EditText) findViewById(R.id.et1);
num2 = (EditText) findViewById(R.id.et2);
add = (Button) findViewById(R.id.addBtn);
ans = (TextView) findViewById(R.id.tv);
add.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Value1 = Integer.parseInt(num1.getText().toString());
Value2 = Integer.parseInt(num2.getText().toString());
result = Value1 + Value2;
ans.setText(""+result);
}
});
}
}
Edit your code to this and it should run. The problem is you are accessing the values in EditTexts too early, even before you type any in them. You need to access these values in onClick() of your add button
package com.example.android.add;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
EditText num1;
EditText num2;
Button add;
TextView ans;
int Value1;
int Value2;
int result;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
num1 = (EditText) findViewById(R.id.et1);
num2 = (EditText) findViewById(R.id.et2);
// If you get value from edit texts here it will cause an error
// since onCreate is called at the very beginning and you have not
// entered any value in num1 and num2.
add = (Button) findViewById(R.id.addBtn);
ans = (TextView) findViewById(R.id.tv);
add.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Instead put them here so you have already inserted values
// into the fields before they are retrieved:
Value1 = Integer.parseInt(num1.getText().toString());
Value2 = Integer.parseInt(num2.getText().toString());
result = Value1 + Value2;
ans.setText(""+result);
}
});
}
}
Value1 = Integer.parseInt(num1.getText().toString());
Value2 = Integer.parseInt(num2.getText().toString());
this code write into onClick() method.remove from onCreate() method.
Logic is : get edit text value on click listener of Button
add.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
try{
Value1 = Integer.parseInt(num1.getText().toString());
Value2 = Integer.parseInt(num2.getText().toString());
result = Value1 + Value2;
result = Value1 + Value2;
ans.setText(""+result);
}
catch(Exception e)
{
Log.e("Exc",e.toString());
}
}
});
Using OnClickListeners for button is discouraged. It has been replaced with the XML Attribute: android:onClick = "functionName".
And this function is to be implemented in the Activity to which the layout file is attached to. Hence, in MainActivity.java:
public void funcitonName(View v){
// your code here
}
There may be a number of string conversion error
Value1 = 0;
try {
Value1 = Integer.parseInt(num1.getText().toString());
} catch(NumberFormatException nfe) {
System.out.println("Could not parse " + nfe);
}
Your app is crashing because of these two lines
Value1 = Integer.parseInt(num1.getText().toString());
Value2 = Integer.parseInt(num2.getText().toString());
here you are trying to convert string to integer where string is null if editText doesnt have ant values these type of error occur while typeCasting so add validation before typeCasting like below
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
num1 = (EditText) findViewById(R.id.et1);
num2 = (EditText) findViewById(R.id.et2);
add = (Button) findViewById(R.id.addBtn);
ans = (TextView) findViewById(R.id.tv);
add.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(num1.getText() != null && num2.getText() != null) {
Value1 = Integer.parseInt(num1.getText().toString());
Value2 = Integer.parseInt(num2.getText().toString());
result = Value1 + Value2;
ans.setText("" + result);
}
}
});
}
Related
I am trying to create an app to increase the value of a number on screen to the next number when the button is pressed. It is not working. I have given my Java and XML code below.
XML code:
<Button
android:id="#+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="250dp"
android:text="+" />
<TextView
android:id="#+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:paddingLeft="200dp"
android:textSize="25sp"
android:text="0" />
Java code:
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
Button btn;
TextView txt;
protected int a = 1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button) findViewById(R.id.button);
txt = (TextView) findViewById(R.id.textView);
}
public void display(final int n) {
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
txt.setText(n);
}
});
}
public void increment(View view) {
a = a + 1;
display(a);
}
}
The button is unreactive.
When you click the button, increase the value of a. Then pass the a to display method. Finally display the value to textView.
public class MainActivity extends AppCompatActivity {
Button btn;
TextView txt;
protected int a = 1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button) findViewById(R.id.button);
txt = (TextView) findViewById(R.id.textView);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
a++;
display(a);
}
});
}
public void display(int n) {
txt.setText("" + n);
//txt.setText(n);
}
}
You have to increment your counter and set new text
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
Button btn;
TextView txt;
protected int a = 1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button) findViewById(R.id.button);
txt = (TextView) findViewById(R.id.textView);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
txt.setText(String.valueOf(++a));
}
});
}
}
if I am not mistake, we can not call `onClickListener in some method.
In any case, change the code in your button to the next one and it should work.
int a = 0;
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
a = a + 1;
txt.setText(a);
}
});
in this case, that code helps. becose it increases value a on 1, all time when we click on the button.
When you click the on the button, increase the value of a and display to the textView
public class MainActivity extends AppCompatActivity {
Button btn;
TextView txt;
protected int a = 1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button) findViewById(R.id.button);
txt = (TextView) findViewById(R.id.textView);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
a++;
txt.setText(a + "");
}
});
}
}
Android Noob here. I am attempting to increment and decrement an TextView via plus and minus buttons. I am running into trouble connecting the method to the image view. Using Java 9. Assuming I'm making a basic mistake.
<TextView
android:id="#+id/home_tds_var"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="80dp"
android:layout_marginTop="24dp"
android:text="0"
android:textSize="16sp"
android:textStyle="bold"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/home_touchdowns_header" />
<Button
android:id="#+id/home_td_plus"
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="+"
app:layout_constraintStart_toEndOf="#+id/home_tds_var"
app:layout_constraintTop_toBottomOf="#+id/home_touchdowns_header"
android:onClick="homeTDButtonPlus"/>
package com.example.android.scorekeeper;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import org.w3c.dom.Text;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
int homeTouchdowns = 0;
public void homeTDButtonPlus(View view) {
homeTouchdowns = homeTouchdowns + 1;
TextView home_tds_var = (TextView) findViewById(R.id.home_touchdowns_var);
home_tds_var.setText(homeTouchdowns);
//Updating the View
//tvId.setText(String.valueOf(homeTouchdowns));
}
}
According to your xml code, your findViewById() has an incorrect id:
TextView home_tds_var = (TextView) findViewById(R.id.home_touchdowns_var);
Should be:
TextView home_tds_var = (TextView) findViewById(R.id.home_tds_var);
I must say, using onClick in xml layout is discourage. You should create listeners in java code to decouple view from other application layers. A simple example:
TextView home_tds_var;
int homeTouchdowns = 0;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
home_tds_var = (TextView) findViewById(R.id.home_tds_var);
Button btn = (Button) findViewById(R.id.home_td_plus);
btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
homeTouchdowns = homeTouchdowns + 1;
home_tds_var.setText(homeTouchdowns);
}
});
}
Change
TextView home_tds_var = (TextView) findViewById(R.id.home_touchdowns_var);
to
TextView home_tds_var = (TextView) findViewById(R.id.home_tds_var);
public void incrementHomeTdsVar(View view) {
final TextView homeTdsView = findViewById(R.id.home_tds_var);
int newValue = Integer.parseInt(homeTdsView.getText().toString()) + 1;
homeTdsView.setText(String.valueOf(newValue));
/* Home Score + 7 Increment */
final TextView homeScoreView = findViewById(R.id.home_score_var);
int homeScorePlus = Integer.parseInt(homeScoreView.getText().toString()) + 7;
homeScoreView.setText(String.valueOf(homeScorePlus));
I was trying to program a simple calculator in Android Studio, only with plus, minus, times and divide functions. I am half-code in programming of "plus" function, which is triggered by clicking a button. Here is my code in MainActivity.java:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public class Arithmetic {
public void plus(View view) {
EditText editText = (EditText) findViewById(R.id.editText);
EditText editText2 = (EditText) findViewById(R.id.editText2);
TextView textView = (TextView) findViewById(R.id.textView);
String numberOne = editText.getText().toString();
String numberTwo = editText2.getText().toString();
String result = numberOne + numberTwo;
}
}}
Now I have two questions. Firstly, how do I make it to show result in textView? Secondly, public void plus is not showing in button's box onClick. Why? And how do I make it to show there?
I will be really thankful for every useful tip.
1.Firstly, how do I make it to show result in textView
int sum = (Integer.parseInt(numberOne)) + (Integer.parseInt(numberTwo));
String result = sum + "";
textView.setText(result);
//or
textView.setText(sum + "");
2.Why public void plus is not showing in button's box onClick
<button
----
android:onClick = "plus"/>
and
public class MainActivity extends AppCompatActivity{
onCreate().....
public void plus(View view){
//your button code here
}
No need to create a nested class for that!
This is what you are looking for, but you should learn some Android and Java basics before starting a project:
Remove the onClick property of your button in your xml file, it's better to use a OnClickListener (particularly if you are planning to use multiple buttons)
package com.example.arnaudpradier.calculator;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
Button calculBtn;
EditText inputFieldOne;
EditText inputFieldTwo;
TextView showResult;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
calculBtn = (Button) findViewById(R.id.calculateBtn);
inputFieldOne = (EditText) findViewById(R.id.inputFieldOne);
inputFieldTwo = (EditText) findViewById(R.id.inputFieldTwo);
showResult = (TextView) findViewById(R.id.resultText);
calculBtn.setOnClickListener(this);
}
private void makeAddition() {
int firstValue = (Integer.parseInt(inputFieldOne.getText().toString()));
int secondValue = (Integer.parseInt(inputFieldTwo.getText().toString()));
String result = (firstValue + secondValue) + "";
showResult.setText(result);
}
#Override
public void onClick(View view) {
int i = view.getId();
if (i == R.id.calculateBtn) {
makeAddition();
}
}
}
public class MainActivity extends AppCompatActivity {
EditText editText;
EditText editText2;
TextView textView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = (EditText) findViewById(R.id.editText);
editText2 = (EditText) findViewById(R.id.editText2);
textView = (TextView) findViewById(R.id.textView);
Arithmetic a = new Arithmetic();
Arithmetic.plus();
}
public class Arithmetic {
public void plus() {
String numberOne = editText.getText().toString();
String numberTwo = editText2.getText().toString();
String result = numberOne + numberTwo;
textView.setText(result);
}
}}
this is a small application which asks some questions and then it will show answers. When we finish answering all the questions, new activity appears and shows us the all the answers except the last one. I searched this problem and saw answers but none of them is working in my case. Thanks for any kind of help.
This is my code:
XML code for the main layout.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.itrio.iqtest.Result_QA"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Answers"
android:textSize="22sp"
android:textStyle="bold"
android:layout_margin="20dp"/>
<ListView
android:id="#+id/result_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</ListView>
</LinearLayout>
Another XML custom view.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="10dp"
android:padding="10dp">
<TextView
android:id="#+id/question_result"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:textSize="18sp"
android:textStyle="bold">
</TextView>
<View
android:layout_width="match_parent"
android:layout_height="10dp"/>
<TextView
android:textStyle="italic"
android:id="#+id/answer_result"
android:layout_width="fill_parent"
android:textSize="18sp"
android:layout_marginBottom="10dp"
android:layout_height="wrap_content">
</TextView>
</LinearLayout>
Here's the code for questions:
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.Collections;
public class Genius extends AppCompatActivity {
private TextView question_view;
private Button mNext;
int currentQ = 0;
RadioGroup genius_rg;
RadioButton op1, op2, op3, op4,checked;
private int score;
int turns=0;
boolean check= false, shuffle = true;
String[] questions = new String[8];
String[] answers = new String[8];
//Keys
private static final String Question_Index_key = "Index value of Questions";
private static final String Checked_Radio = "Selected radio Button";
private static final String Is_Radio_Checked = "ISC";
private static final String Do_Not_Shuffle_Again = "Dont' shuffle";
public static final String Result_Questions = "questions to display at result activity";
public static final String Result_Answers = "answers to display at result activity";
#Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt(Question_Index_key,currentQ);
outState.putInt(Checked_Radio,genius_rg.getCheckedRadioButtonId());
outState.putBoolean(Is_Radio_Checked,check);
outState.putBoolean(Do_Not_Shuffle_Again, shuffle);
}
#Override
public void onBackPressed() {
super.onBackPressed();
Dialog alert_exit = new QuizExitAlert(Genius.this);
alert_exit.show();
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
//Changes 'back' button action
if (keyCode == KeyEvent.KEYCODE_BACK) {
Dialog alert_exit = new QuizExitAlert(Genius.this);
alert_exit.show();
}
return true;
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_genius);
if (savedInstanceState != null) {
currentQ = savedInstanceState.getInt(Question_Index_key);
if (savedInstanceState.getBoolean(Is_Radio_Checked)) {
checked = (RadioButton) findViewById(savedInstanceState.getInt(Checked_Radio));
checked.setChecked(true);
}
shuffle = savedInstanceState.getBoolean(Do_Not_Shuffle_Again);
}
question_view = (TextView) findViewById(R.id.question_genius);
genius_rg = (RadioGroup) findViewById(R.id.radio_genius);
op1 = (RadioButton) findViewById(R.id.g_op1);
op2 = (RadioButton) findViewById(R.id.g_op2);
op3 = (RadioButton) findViewById(R.id.g_op3);
op4 = (RadioButton) findViewById(R.id.g_op4);
Collections.shuffle(bank);
question_view.setText(bank.get(currentQ).getQ());
op1.setText(bank.get(currentQ).getOp1());
op2.setText(bank.get(currentQ).getOp2());
op3.setText(bank.get(currentQ).getOp3());
op4.setText(bank.get(currentQ).getOp4());
mNext = (Button) findViewById(R.id.next_genius);
mNext.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
turns++;
if (turns >= 8) {
new AlertDialog.Builder(Genius.this)
.setTitle("Done!!!")
.setMessage("You have answered all the questions.")
.setPositiveButton("Submit", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
Intent result = new Intent(Genius.this,Result_QA.class);
result.putExtra(Result_Questions, questions);
result.putExtra(Result_Answers, answers);
startActivity(result);
}
}).show();
}else {
if (op1.isChecked() || op2.isChecked() || op3.isChecked() || op4.isChecked()) {
checked = (RadioButton) findViewById(genius_rg.getCheckedRadioButtonId());
if (bank.get(currentQ).getAns() == checked.getText()) {
score += 10;
}
questions[currentQ] = bank.get(currentQ).getQ();
answers[currentQ] = bank.get(currentQ).getAns();
genius_rg.clearCheck();
currentQ++;
question_view.setText(bank.get(currentQ).getQ());
op1.setText(bank.get(currentQ).getOp1());
op2.setText(bank.get(currentQ).getOp2());
op3.setText(bank.get(currentQ).getOp3());
op4.setText(bank.get(currentQ).getOp4());
} else {
Toast.makeText(Genius.this, "Select an option to proceed", Toast.LENGTH_SHORT).show();
}
}
}
});
}
}
Here's the code for LisView Activity:
import android.os.Bundle;
import android.app.Activity;
import android.widget.ListView;
public class Result_QA extends Activity {
ListView result;
ReslutListViewAdapter reslutListViewAdapter;
String[] ques, ans;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_result__q);
Bundle b= getIntent().getExtras();
ques = b.getStringArray(new Genius().Result_Questions);
ans = b.getStringArray(new Genius().Result_Answers);
result = (ListView) findViewById(R.id.result_view);
reslutListViewAdapter = new ReslutListViewAdapter(this, ques, ans);
System.out.println("adapter => "+reslutListViewAdapter.getCount());
result.setAdapter(reslutListViewAdapter);
}
}
Code for ListViewAdapter class:
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
public class ReslutListViewAdapter extends BaseAdapter {
Activity context;
String[] questions;
String[] answers;
public ReslutListViewAdapter(Activity con, String[] ques, String[] ans){
super();
context = con;
questions = ques;
answers = ans;
}
#Override
public int getCount() {
return questions.length;
}
private class ViewHolder {
TextView mQ;
TextView mA;
}
#Override
public Object getItem(int position) {
return null;
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
LayoutInflater inflater = context.getLayoutInflater();
if (convertView == null)
{
convertView = inflater.inflate(R.layout.result_listadapter, null);
holder = new ViewHolder();
holder.mQ = (TextView) convertView.findViewById(R.id.question_result);
holder.mA = (TextView) convertView.findViewById(R.id.answer_result);
convertView.setTag(holder);
}
else
{
holder = (ViewHolder) convertView.getTag();
}
holder.mQ.setText(questions[position]);
holder.mA.setText(answers[position]);
return convertView;
}
}
First of all listview is old one Recyclerview had came, which makes our work easier and lot more improvements have been done in RecyclerView to make developer work easier.
Coming to this issue, ListView doesn't support wrap_content. So change the height and width of ListView to match_parent. Then in adapter xml file the parent layout height should be wrap_content, but you have mentioned it as match_parent, so change that also to wrap_content. Hopefully, this should solve your problem. If the above solution didn't work, then try by removing the padding and margin in the adapter XML layout(from the parent layout in result_listadapter.xml file).
FYI fill_parent is deprecated and we are supposed to use match_parent instead of that. So avoid using the fill_parent.
Hope this helps:)
package walmart.namespace;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class WalmartActivity extends Activity {
/** Called when the activity is first created. */
EditText name;
Button search;
TextView display;
#Override
public void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.main);
super.onCreate(savedInstanceState);
name = (EditText)findViewById(R.id.etName);
search = (Button) findViewById(R.id.btnSearch);
display = (TextView) findViewById(R.id.tvdisplay);
name.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
name.setText("");
}
});
search.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (name.equals("Electronics")) {
display.setText ('5');
} else if (name.equals("candy")) {
display.setText ('1');
} else if (name.equals("Tobacco")) {
display.setText ("1");
}
}});
};}
No matter what i do, nothing shows up in my output. I'm very, very new to JAVA, so I can't seem to find out why nothing's showing up on my display.
edit Edited my code to what i currently have. Still not working.
Looking at that code, the issue might be with how you are initializing name and search.
Change your code:
name = (EditText) findViewById(getResources().getIdentifier("etName", "id", getPackageName()));
search = (Button) findViewById(getResources().getIdentifier("btnSearch", "id", getPackageName()));
to this:
name = (EditText) findViewById(R.id.etName);
search = (Button) findViewById(R.id.btnSearch);
Also, add a TextView to display your answer into your main.xml file, give it an id (let's say display for example) android:id="#+id/display" and then add it to your activity so you can change it:
display = (TextView) findViewById(R.id.display);
and then you can update it by calling:
display.setText('some text');
Thus the start of your file should look like this:
public class WalmartActivity extends Activity {
/** Called when the activity is first created. */
EditText name;
Button search;
TextView display;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
name = (EditText) findViewById(R.id.etName);
search = (Button) findViewById(R.id.btnSearch);
display = (TextView) findViewById(R.id.display);
....
Next update your if statements from:
if (name.equals("Electronics"))
to:
if (name.getText().toString().equals("Electronics")) {
display.setText("something");
}
An example piece of code:
Demo2Activity.java
package com.demo1;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class Demo2Activity extends Activity {
private Button button;
private EditText editText;
private TextView textView;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main2);
button = (Button) findViewById(R.id.button);
editText = (EditText) findViewById(R.id.editText);
textView = (TextView) findViewById(R.id.textText);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if (editText.getText().toString().equals("Electronics")) {
textView.setText("found");
}
}
});
}
}
main2.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="main2.xml"
android:id="#+id/textText" />
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/editText"/>
<Button
android:id="#+id/button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="button" />
</LinearLayout>