我有一个列表视图和一个textview显示的数组。我希望在列表视图中选择每个项目,将值添加到textview中显示的计数器。例如,如果我在listView中选择“THREE”,则texview必须显示“Value is 3”,如果我也选择“TWO”,则texview显示“Value is 5”(3 + 2)。
我已经创建了第二个listview_array,其值已经尝试传递给整数,但它不起作用。有什么想法或帮助吗?
这是我的问题的代码:
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);
}
});
}
}
答案 0 :(得分:0)
而不是:
String name = listview_array2.getItemAtPosition(position).toString();
使用:
String name = this.listview_array2[position];