I have one array that is displayed in a listview and one textview. I want that every item is selected on the list view, add a value to a counter that is displayed in the textview. For example if I select "THREE" in the listView, the texview must show "Value is 3", and if I also select "TWO", the texview shows "Value is 5" (3 + 2).
I have made a second listview_array with values that I have tried to pass to integer, but its not working. Any idea or help please?
This is the code for my question:
package com.javaya.proyecto002;
import android.app.Activity;
import android.app.AlertDialog;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.ListView;
import android.widget.TextView;
;
public class activity2 extends Activity {
TextView tv1;
ListView lv1;
int counter;
int counter2;
String listview_array[] = { "ONE", "TWO", "THREE", "FOUR", "FIVE", "SIX",
"SEVEN", "EIGHT", "NINE", "TEN" };
String listview_array2[] = { "1", "2", "3", "4", "5", "6",
"7", "8", "9", "10" };
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity2);
tv1 = (TextView)findViewById(R.id.textView1);
counter = 0;
lv1 = (ListView) findViewById(R.id.listView1);
lv1.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_activated_1, listview_array));
lv1.setChoiceMode(lv1.CHOICE_MODE_MULTIPLE);
lv1.setOnItemClickListener(new OnItemClickListener(){
private String[] listview_array2 = { "1", "2", "3", "4", "5", "6",
"7", "8", "9", "10" };
public void onItemClick(AdapterView<?> arg0, View v, int position, long id){
if (lv1.isItemChecked(position)); String name = this.listview_array2[position];
counter2 = Integer.parseInt(name);
counter = counter + counter2;
tv1.setText("Total " + counter);
}
});
}
}
Instead of:
String name = listview_array2.getItemAtPosition(position).toString();
use:
String name = this.listview_array2[position];
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.