I am creating a gpa calculator and have used a spinner to populate the grades. how do I multiply the value in spinner2 which is credits to the grade points value in textview1 so it displays the result in textview2 which is the gradevalue.(should be something like: gradevalue=credits*gradepoints)
`public class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener{
String[] grades ={"A","B+","B","C+","C", "D+", "D", "E"};
String[] credits ={"1","2","3","4"};
double [] points1 = {4.0, 3.5, 3.0, 2.5, 2.0, 1.5, 1.0, 0 };
Spinner spinner;
TextView TextView1;
TextView TextView2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView1 = (TextView) findViewById(R.id.gradePoints);
TextView2 = (TextView) findViewById(R.id.gradeValue);
spinner = (Spinner) findViewById(R.id.spinner);
ArrayAdapter<String> adapter1 =
new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_spinner_item, grades);
adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter1);
spinner.setOnItemSelectedListener(onItemSelectedListener1);
Spinner spin2 = (Spinner) findViewById(R.id.spinner2);
ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_spinner_item, credits);
adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spin2.setAdapter(adapter2);
spin2.setOnItemSelectedListener(this);
}
AdapterView.OnItemSelectedListener onItemSelectedListener1 = new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String s1 = String.valueOf(points1[position]);
TextView1.setText(s1);
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
};
public void onItemSelected(AdapterView<?> parent, View v, int position, long id ){
Spinner spin = (Spinner)parent;
Spinner spin2 = (Spinner)parent;
if(spin.getId() == R.id.spinner)
{
Toast.makeText(this, "Your choice :" + grades[position],Toast.LENGTH_SHORT).show();
}
if(spin2.getId() == R.id.spinner2)
{
Toast.makeText(this, "Your choice :" + credits[position],Toast.LENGTH_SHORT).show();
}
}
public void onNothingSelected(AdapterView<?> parent){
Toast.makeText(this, "Choose grades :", Toast.LENGTH_SHORT).show();
}
}
enter code here`
you just need to set Onitemselected listener on spinner depending upon selected item's index you can change textview's value.Hope it will help you
You need to set the text on your spinner and you can get it with a method like :
getSelectedValue() and you add +1 on it because it start at 0.
Hey KunjiDaniel this is working at my end.Please check this Spinner spinner;
ArrayAdapter<String> adapter;
String[] spinnerData = {"1", "3", "4", "5", "6"};
TextView text2, resultText;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sample_spinner);
spinner = (Spinner) findViewById(R.id.spinner);
text2 = (TextView) findViewById(R.id.numbr2);
text2.setText(String.valueOf(3));
resultText = (TextView) findViewById(R.id.result);
adapter = new ArrayAdapter<String>(SampleSpinner.this, android.R.layout.simple_dropdown_item_1line, spinnerData);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int position, long l) {
String selected_value = adapterView.getItemAtPosition(position).toString();
changeResultantValues(selected_value);
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
}
int value = 0;
public void changeResultantValues(String spinnerSelected) {
if (!spinnerSelected.equals("")) {
value = Integer.parseInt(spinnerSelected);
}
String textview2Value = text2.getText().toString();
int value2 = Integer.parseInt(textview2Value);
String resultant = String.valueOf(value * value2);
resultText.setText(resultant);
}
Related
I have two spinners in my code. The problem I face is I'm unable to differentiate the results of the two spinners.I know we can use ids but I'm unable to do it.here's my code
public class AddProduct extends AppCompatActivity implements AdapterView.OnItemSelectedListener{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_product);
Spinner spinner = findViewById(R.id.spinner1);
SpinnerAdapter adap = new ArrayAdapter<String>(this, R.layout.spinner, new String[]{"Men", "Women", "Kids"});
spinner.setAdapter(adap);
spinner.setTag(1);
spinner.setOnItemSelectedListener(this);
Spinner spinner2 = findViewById(R.id.spinner2);
spinner2.setTag(2);
SpinnerAdapter adap2 = new ArrayAdapter<String>(this,
R.layout.spinner, new String[]{"Casual", "Formals", "Ethinic"});
spinner2.setAdapter(adap2);
spinner2.setOnItemSelectedListener(this);
}
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String text = parent.getItemAtPosition(position).toString();
Toast.makeText(parent.getContext(), text , Toast.LENGTH_SHORT).show();
}
Modify onItemSelected method
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String text = "";
if(parent.getId() == R.id.spinner1) {
text = "spinner1 " + parent.getItemAtPosition(position).toString();
} else if(parent.getId() == R.id.spinner2) {
text = "spinner2 " + parent.getItemAtPosition(position).toString();
}
Toast.makeText(parent.getContext(), text , Toast.LENGTH_SHORT).show();
}
differentiate the results:
Spinner s1, s2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_product);
s1= findViewById(R.id.spinner1);
SpinnerAdapter adap = new ArrayAdapter<String>(this, R.layout.spinner, new String[]{"Men", "Women", "Kids"});
s1.setAdapter(adap);
s1.setTag(1);
s2= findViewById(R.id.spinner2);
s2.setTag(2);
SpinnerAdapter adap2 = new ArrayAdapter<String>(this,
R.layout.spinner, new String[]{"Casual", "Formals", "Ethinic"});
s2.setAdapter(adap2);
s1.setOnItemSelectedListener(this);
s2.setOnItemSelectedListener(this);
}
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position,
long id) {
// TODO Auto-generated method stub
Spinner spinner = (Spinner) parent;
if(spinner.getId() == R.id.spinner1)
{
String sp1= String.valueOf(s1.getSelectedItem());
// Toast.makeText(this, sp1, Toast.LENGTH_SHORT).show();
if(sp1.contentEquals("Men")) {
//Do Something
}
if(sp1.contentEquals("Women")) {
//Do Something
}
if(sp1.contentEquals("Kids")) {
//Do Something
}
} else if(spinner.getId() == R.id.spinner2)
{
String sp2= String.valueOf(s2.getSelectedItem());
// Toast.makeText(this, sp2, Toast.LENGTH_SHORT).show();
if(sp2.contentEquals("Casual")) {
//Do Something
}
if(sp2.contentEquals("Formals")) {
//Do Something
}
if(sp2.contentEquals("Ethinic")) {
//Do Something
}
}
}
I'm trying to build an app that takes 2 inputted redox pairs from 2 spinners to display information about the cell that could be made from these chemicals such as what would be used as the anode/cathode, the reduction potential of each half-reaction and the cell etc.
My issue is I haven't been able to find any way to take the input of both the spinners to be able to make the calculations, only have both spinners do the same thing.
My current code is this:
public class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener{
Spinner spinner1, spinner2;
ArrayAdapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
adapter = ArrayAdapter.createFromResource(this, R.array.pairs, android.R.layout.simple_spinner_item);
spinner1 = (Spinner) findViewById(R.id.spinner1);
spinner1.setAdapter(adapter);
spinner1.setOnItemSelectedListener(MainActivity.this);
spinner2 = (Spinner) findViewById(R.id.spinner2);
spinner2.setAdapter(adapter);
spinner2.setOnItemSelectedListener(MainActivity.this);
}
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String choice1 = parent.getItemAtPosition(position).toString();
double first = 0;
double second = 0;
double anode;
double cathode;
if (choice1 == getResources().getString(R.string.MnO4)){
first = 0.60;
Toast.makeText(parent.getContext(), choice1, Toast.LENGTH_SHORT).show();
} else if (choice1 == getResources().getString(R.string.Cl)) {
first = 1.36;
Toast.makeText(parent.getContext(), choice1, Toast.LENGTH_SHORT).show();
}
if (first > second) {
cathode = first;
anode = second;
} else {
cathode = second;
anode = first;
}
double cellValue = anode - cathode;
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
}
I'm very new to android studio so any help is greatly appreciated.
Thanks
I suggest that you use anonymous classes to handle the onItemSelected. That way you can handle the selection of each Spinner independently.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
adapter = ArrayAdapter.createFromResource(this, R.array.pairs, android.R.layout.simple_spinner_item);
spinner1 = (Spinner) findViewById(R.id.spinner1);
spinner1.setAdapter(adapter);
spinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
// Do what you need to do with spinner1 info
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
spinner2 = (Spinner) findViewById(R.id.spinner2);
spinner2.setAdapter(adapter);
spinner2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
// Do what you need to do with spinner2 info
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
Now you can remove the Override method:
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
and you can remove implements :
implements AdapterView.OnItemSelectedListener
i want to multiply the credits by the gradePoints to return the gradevalue in textview2. how do i do that? the credits is the second spinner. `public class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener{
String[] grades ={"A","B+","B","C+","C", "D+", "D", "E"};
String[] credits ={"1","2","3","4"};
double [] points1 = {4.0, 3.5, 3.0, 2.5, 2.0, 1.5, 1.0, 0 };
Spinner spinner;
TextView TextView1;
TextView TextView2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView1 = (TextView) findViewById(R.id.gradePoints);
TextView2 = (TextView) findViewById(R.id.gradeValue);
spinner = (Spinner) findViewById(R.id.spinner);
ArrayAdapter<String> adapter1 =
new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_spinner_item, grades);
adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter1);
spinner.setOnItemSelectedListener(onItemSelectedListener1);
Spinner spin2 = (Spinner) findViewById(R.id.spinner2);
ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_spinner_item, credits);
adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spin2.setAdapter(adapter2);
spin2.setOnItemSelectedListener(this);
}
AdapterView.OnItemSelectedListener onItemSelectedListener1 = new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String s1 = String.valueOf(points1[position]);
TextView1.setText(s1);
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
};
public void onItemSelected(AdapterView<?> parent, View v, int position, long id ){
Spinner spin = (Spinner)parent;
Spinner spin2 = (Spinner)parent;
if(spin.getId() == R.id.spinner)
{
Toast.makeText(this, "Your choice :" + grades[position],Toast.LENGTH_SHORT).show();
}
if(spin2.getId() == R.id.spinner2)
{
Toast.makeText(this, "Your choice :" + credits[position],Toast.LENGTH_SHORT).show();
}
}
public void onNothingSelected(AdapterView<?> parent){
Toast.makeText(this, "Choose grades :", Toast.LENGTH_SHORT).show();
}
}
`
Maybe you can try this:
public class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener {
String[] grades ={"A","B+","B","C+","C", "D+", "D", "E"};
String[] credits ={"1","2","3","4"};
double [] points1 = {4.0, 3.5, 3.0, 2.5, 2.0, 1.5, 1.0, 0 };
Spinner spinner, spin2;
TextView TextView1;
TextView TextView2;
Double point = -1.0;
Double value = -1.0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView1 = (TextView) findViewById(R.id.gradePoints);
TextView2 = (TextView) findViewById(R.id.gradeValue);
spinner = (Spinner) findViewById(R.id.spinner);
ArrayAdapter<String> adapter1 =
new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_spinner_item, grades);
adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter1);
spinner.setOnItemSelectedListener(onItemSelectedListener1);
spin2 = (Spinner) findViewById(R.id.spinner2);
ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_spinner_item, credits);
adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spin2.setAdapter(adapter2);
spin2.setOnItemSelectedListener(this);
}
AdapterView.OnItemSelectedListener onItemSelectedListener1 = new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
point = points1[position];
TextView1.setText(String.valueOf(point));
if ( value != -1.0 ) {
double res = (double) point * value;
TextView2.setText(String.valueOf(res));
}
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
};
public void onItemSelected(AdapterView<?> parent, View v, int position, long id ){
Toast.makeText(this, "Your choice :" + credits[position],Toast.LENGTH_SHORT).show();
String valueStr = credits[position];
value = Double.parseDouble(valueStr);
if ( point != -1.0 ) {
double res = (double) point * value;
TextView2.setText(String.valueOf(res));
}
}
public void onNothingSelected(AdapterView<?> parent){
Toast.makeText(this, "Choose grades :", Toast.LENGTH_SHORT).show();
}
}
Hope this will work!
You can try making your selected values member variables then changing the value of the textview each time it's selected:
public class MainActivity extends AppCompatActivity{
int selectedCredit = 0;
double selectedPoints = 0;
#Override
public void onCreate(Bundle savedInstanceState){
int[] credits ={1,2,3,4};
double[] points = {4.0, 3.5, 3.0, 2.5, 2.0, 1.5, 1.0, 0 };
spinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener(){
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id){
selectedCredit = credits[position];
double value = selectedCredit * selectedPoints;
textView.setText("The calculated value is " + value);
}
#Override
public void onNothingSelected(AdapterView<?> parent){}
});
spinner2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener(){
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id){
selectedPoints = points[position];
double value = selectedCredit * selectedPoints;
textView.setText("The calculated value is " + value);
}
#Override
public void onNothingSelected(AdapterView<?> parent){}
});
}
}
I have given a solution according to what I understood from your question. Hope this helps. :)
public class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener{
String[] grades ={"A","B+","B","C+","C", "D+", "D", "E"};
String[] credits ={"1","2","3","4"};
double [] points1 = {4.0, 3.5, 3.0, 2.5, 2.0, 1.5, 1.0, 0 };
Spinner spinner;
TextView TextView1;
TextView TextView2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView1 = (TextView) findViewById(R.id.gradePoints);
TextView2 = (TextView) findViewById(R.id.gradeValue);
spinner = (Spinner) findViewById(R.id.spinner);
ArrayAdapter<String> adapter1 =
new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_spinner_item, grades);
spinner.setAdapter(adapter1);
spinner.setOnItemSelectedListener(onItemSelectedListener1);
Spinner spin2 = (Spinner) findViewById(R.id.spinner2);
ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_spinner_item, credits);
spin2.setAdapter(adapter2);
spin2.setOnItemSelectedListener(this);
}
spinner.OnItemSelectedListener onItemSelectedListener1 = new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String s1 = spinner.getSelectedItem().toString();
TextView1.setText(s1);
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
};
spin2.OnItemSelectedListener onItemSelectedListener1 = new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String s1 = spinner.getSelectedItem().toString();
Double gradePoint = 0.0;
if(s1.equals("A")){
gradePoint = 4.0;
}
else if(s1.equals("B+")){
gradePoint = 3.5;
}
else if(s1.equals("B")){
gradePoint = 3.0;
}
else if(s1.equals("C+")){
gradePoint = 2.5;
}
else if(s1.equals("c")){
gradePoint = 2.0;
}
else if(s1.equals("D+")){
gradePoint = 1.5;
}
else if(s1.equals("D")){
gradePoint = 1.0;
}
else if(s1.equals("E")){
gradePoint = 0;
}
int courseCredit = Integer.parseInt(spin2.getSelectedItem().toString());
double result = gradePoint*courseCredit;
TextView2.setText(String.valueOf(result));
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
};
public void onItemSelected(AdapterView<?> parent, View v, int position, long id ){
Spinner spin = (Spinner)parent;
Spinner spin2 = (Spinner)parent;
if(spin.getId() == R.id.spinner)
{
Toast.makeText(this, "Your choice :" + grades[position],Toast.LENGTH_SHORT).show();
}
if(spin2.getId() == R.id.spinner2)
{
Toast.makeText(this, "Your choice :" + credits[position],Toast.LENGTH_SHORT).show();
}
}
public void onNothingSelected(AdapterView<?> parent){
Toast.makeText(this, "Choose grades :", Toast.LENGTH_SHORT).show();
}
}
i'm practising how to add an string to the addapter of the spinner. If i declare the string in the java activity, works perfectly, but if i do in the string.xml:
<string-array name="tabs">
<item>tab</item>
<item>tab1</item>
<item>tab2</item>
<item>tab3</item>
<item>tab4</item>
<item>tab5</item>
</string-array>
and in the java
ArrayAdapter<String> adapterr1 = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, R.array.tabs);
dont work.
what's the problem? Thanks
This is the full code, because i have problems whit the app (crash)
public class MainActivity extends AppCompatActivity {
Spinner OptionSpinner;
TextView textview;
String[] stabs= getResources().getStringArray(R.array.tabs);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
OptionSpinner = (Spinner) findViewById(R.id.OptionSpinner);
textview = (TextView) findViewById(R.id.textview);
ArrayAdapter adapter1 = new ArrayAdapter(this, android.R.layout.simple_spinner_item, stabs);
adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
OptionSpinner.setAdapter(adapter1);
OptionSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
textview.setText("seleccionado: " + parent.getItemAtPosition(position));
if(position==1){
Intent IntentActT1 = new Intent(MainActivity.this, ActTab1.class);
startActivity(IntentActT1);
}
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
}
String[] stabs= getResources().getStringArray(R.array.tabs);
ArrayAdapter adapterr1 = new ArrayAdapter(this, android.R.layout.simple_spinner_item, stabs);
Edited 1
public class MainActivity extends AppCompatActivity {
Spinner OptionSpinner;
TextView textview;
String[] stabs;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
OptionSpinner = (Spinner) findViewById(R.id.OptionSpinner);
textview = (TextView) findViewById(R.id.textview);
stabs= getResources().getStringArray(R.array.tabs);
ArrayAdapter adapter1 = new ArrayAdapter(this, android.R.layout.simple_spinner_item, stabs);
adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
OptionSpinner.setAdapter(adapter1);
OptionSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
textview.setText("seleccionado: " + parent.getItemAtPosition(position));
if(position==1){
Intent IntentActT1 = new Intent(MainActivity.this, ActTab1.class);
startActivity(IntentActT1);
}
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
}
The constructor ArrayAdapter<String>(Context, int, int) doesn't use the third argument as an array resource id; it uses it as the id of the TextView to use to display each item in the layout. Instead, you can pass getStringArray(R.array.tabs) as the third argument (or getContext().getStringArray(R.array.tabs), if this code is not in an Activity).
I hava a listview with each row containing a textview and checkbox. Now what I want is that when I click the textview the checkbox must be checked accordingly. I tried using this code:
protected void onListItemClick(ListView l, View v, int position, long id) {
Toast.makeText(getApplicationContext(), "You have selected item no."
+(position+1)+"", Toast.LENGTH_SHORT).show();
super.onListItemClick(l, v, position, id);
if (v != null) {
CheckBox checkBox = (CheckBox)v.findViewById(R.id.Checkbox);
checkBox.setChecked(!checkBox.isChecked());
}
}
Here's my whole code:
public class MyActivity3 extends Activity {
private TextView tv;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my3);
Button m = (Button) findViewById(R.id.button3);
tv = (TextView) findViewById(R.id.textViewcat);
Typeface typeface = Typeface.createFromAsset(getAssets(), "BebasNeue Bold.ttf");
tv.setTypeface(typeface);
String[] listArray = new String[] { "All", "Friends & Family", "Sports", "Outside", "At School", "Fitness", "Photography", "Food", "Beach", "Money" };
SharedPreferences sharedPreferences = getSharedPreferences("status", MODE_PRIVATE);
Boolean[] checkedStatus = new Boolean[listArray.length];
for ( int index = 0; index < checkedStatus.length; index++)
checkedStatus[index] = sharedPreferences.getBoolean(Integer.toString(index), false);
ListView listView = (ListView) findViewById(R.id.listView);
MyAdapter adapter = new MyAdapter(this, R.layout.row_layout, listArray, checkedStatus);
listView.setAdapter(adapter);
}
protected void onListItemClick(ListView l, View v, int position, long id) {
Toast.makeText(getApplicationContext(), "You have selected item no."
+(position+1)+"", Toast.LENGTH_SHORT).show();
super.onListItemClick(l, v, position, id);
if (v != null) {
CheckBox checkBox = (CheckBox)v.findViewById(R.id.chk);
checkBox.setChecked(!checkBox.isChecked());
}
}
#Override
public void onBackPressed() {
super.onBackPressed();
overridePendingTransition(R.anim.animation8, R.anim.animation7);
}
}
Now in the line super.onListItemClick(l, v, position, id); , onListItemClick is red. Any idea why?
Cristiano
Because Activity doesn't have an onListItemClick(), so when you are calling super.onListItemClick() , it throws a compile Time Error because there is no such function in it's parent class. if you want use onListItemClick() you should extends your class from ListActivity.
Use following code to work with itemClickListener.
We need to set OnItemclickListener with list view object. in which we override onItemClick method.
update
public class MyActivity3 extends Activity {
private TextView tv;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my3);
Button m = (Button) findViewById(R.id.button3);
tv = (TextView) findViewById(R.id.textViewcat);
Typeface typeface = Typeface.createFromAsset(getAssets(), "BebasNeue Bold.ttf");
tv.setTypeface(typeface);
String[] listArray = new String[] { "All", "Friends & Family", "Sports", "Outside", "At School", "Fitness", "Photography", "Food", "Beach", "Money" };
SharedPreferences sharedPreferences = getSharedPreferences("status", MODE_PRIVATE);
Boolean[] checkedStatus = new Boolean[listArray.length];
for ( int index = 0; index < checkedStatus.length; index++)
checkedStatus[index] = sharedPreferences.getBoolean(Integer.toString(index), false);
ListView listView = (ListView) findViewById(R.id.listView);
MyAdapter adapter = new MyAdapter(this, R.layout.row_layout, listArray, checkedStatus);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// do here whatever you want :-)
Toast.makeText(getApplicationContext(), "You have selected item no."
+(position+1)+"", Toast.LENGTH_SHORT).show();
if (view != null) {
CheckBox checkBox = (CheckBox)v.findViewById(R.id.chk);
checkBox.setChecked(!checkBox.isChecked());
}
}
});
}