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));
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 + "");
}
});
}
}
I'm using an adapter class to set values for my items. For each item there is a + and a - button. I have a foodActivity java class, activity_food layout for the java class and a list_view_layout for the lists. I have initialized the + and - buttons inside my activity class. When the activity_food starts, the app crashes.
plus, minus and edittext in list_view_layout -
<Button
android:id="#+id/minus"
android:layout_width="35dp"
android:layout_height="35dp"
android:layout_below="#+id/foodname"
android:layout_toEndOf="#+id/foodpic"
android:text="-"
android:textSize="12sp" />
<EditText
android:id="#+id/quantity"
android:layout_width="25dp"
android:layout_height="30dp"
android:layout_below="#+id/foodname"
android:layout_toEndOf="#+id/minus"
android:ems="10"
android:inputType="number"
android:textAlignment="center"
android:background="#drawable/edittextbackground"
android:text="0" />
<Button
android:id="#+id/plus"
android:layout_width="35dp"
android:layout_height="35dp"
android:layout_alignTop="#+id/quantity"
android:layout_toEndOf="#+id/quantity"
android:text="+"
android:textSize="12sp" />
activity_food layout -
<?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.res.easyorder.foodActivity">
<ListView
android:id="#+id/listview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:divider="#ffff"
android:dividerHeight="5dp"/>
</RelativeLayout>
My adapter class -
package com.res.easyorder;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.zip.Inflater;
public class MyAdapter extends ArrayAdapter<item> {
int q_ty = 0;
ArrayList<item> foodlist = new ArrayList<>();
public MyAdapter(Context context, int textViewResourceId, ArrayList<item> objects){
super(context, textViewResourceId, objects);
foodlist = objects;
}
#Override
public int getCount(){
return super.getCount();
}
#Override
public View getView(final int position, View convertView, ViewGroup parent){
View v = convertView;
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.list_view_layout, null);
ImageView foodpic = (ImageView) v.findViewById(R.id.foodpic);
TextView foodname = (TextView) v.findViewById(R.id.foodname);
TextView foodprice = (TextView) v.findViewById(R.id.foodprice);
Button plus = (Button) v.findViewById(R.id.plus);
final EditText quantity = (EditText) v.findViewById(R.id.quantity);
Button minus = (Button) v.findViewById(R.id.minus);
plus.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
item item = getItem(position);
q_ty = Integer.parseInt(item.getQuantity());
q_ty = q_ty + 1;
quantity.setText("" +q_ty);
}
});
foodname.setText(foodlist.get(position).getFoodName());
foodpic.setImageResource(foodlist.get(position).getFoodImage());
foodprice.setText("BDT: " +foodlist.get(position).getFoodPrice());
quantity.setText(foodlist.get(position).getQuantity());
return v;
}
}
foodActivity java code. Plus minus onclicklistener is being commented -
package com.res.easyorder;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;
import com.google.firebase.auth.FirebaseAuth;
import java.util.ArrayList;
public class foodActivity extends AppCompatActivity {
private String type = null;
int quantity = 0;
private Button plus, minus;
private EditText q_ty;
ListView simplelist;
ArrayList<item> foodlist = new ArrayList<>();
private FirebaseAuth firebaseAuth;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_food);
firebaseAuth = FirebaseAuth.getInstance();
simplelist = (ListView) findViewById(R.id.listview);
plus = (Button) findViewById(R.id.plus);
minus = (Button) findViewById(R.id.minus);
q_ty = (EditText) findViewById(R.id.quantity);
type = getIntent().getExtras().getString("type");
if(type.equals("breakfast"))
{
foodlist.add(new item("Rooti",R.drawable.ruti,5));
foodlist.add(new item("Parata",R.drawable.porata,8));
foodlist.add(new item("Tandoor",R.drawable.tandoor,15));
foodlist.add(new item("Vegetable",R.drawable.sodji,10));
foodlist.add(new item("Daal",R.drawable.dal,10));
foodlist.add(new item("Omelet",R.drawable.dimvaji,15));
foodlist.add(new item("Singara",R.drawable.singara,8));
foodlist.add(new item("Samosa",R.drawable.samosa,10));
foodlist.add(new item("Puri",R.drawable.puri,5));
}
MyAdapter myAdapter = new MyAdapter(this,R.layout.list_view_layout,foodlist);
simplelist.setAdapter(myAdapter);
/*plus.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//quantity = quantity + 1;
//q_ty.setText(""+quantity);
}
});
minus.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (quantity==0)
{
//q_ty.setText(""+quantity);
}
else if (quantity>0)
{
quantity = quantity - 1;
//q_ty.setText(""+quantity);
}
}
});*/
}
}
You are facing this problem because you have plus minus button on Listview item row xml file and you are finding plus minus button in activity. You have to put your click listener in your adapter class
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.list_entry, null);
holder = new ViewHolder();
holder.quantity= (EditText) convertView.findViewById(R.id.quantity);
holder.plus = (Button) convertView.findViewById(R.id.plus);
holder.minus = (Button) convertView.findViewById(R.id.minus);
convertView.setTag(holder);
}
else {
holder = (ViewHolder) convertView.getTag();
}
Item item = getItem(position);
holder.quantity.setText(item.getQuantity());
holder.plus.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Item item = getItem(position);
q_ty = Integer.parseInt(item.getQuantity());
q_ty = q_ty + 1;
quantity.setText("" +q_ty);
item.setQuantity(q_ty);
}
});
return convertView;
}
You have to set the OnClicklistener in your "MyAdapter.getView(..)" method, because in this method the items are created. Currently in your foodActivity.onCreate() methode the objects plus, minus and q_ty are null.
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);
}
}
});
}
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>