如何在不显示java中的值的情况下为每行设置一个值
我有一个字符串数组,这就是listView上显示的内容;但是,我希望为这些行中的每一行分配一个int数组,这样当单击它时,它将从用户的“total_pts”中减去,然后如果用户有足够的点开始,它将显示在totalPoints文本视图上 以下是TextView和total point变量:如何在不显示java中的值的情况下为每行设置一个值,java,android,arrays,listview,Java,Android,Arrays,Listview,我有一个字符串数组,这就是listView上显示的内容;但是,我希望为这些行中的每一行分配一个int数组,这样当单击它时,它将从用户的“total_pts”中减去,然后如果用户有足够的点开始,它将显示在totalPoints文本视图上 以下是TextView和total point变量: int total_pts =0; TextView totalPoints = (TextView)findViewById(R.id.totalP); 以下是列表视图: ArrayAdapter<S
int total_pts =0;
TextView totalPoints = (TextView)findViewById(R.id.totalP);
以下是列表视图:
ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, android.R.id.text1, redeem);
//assign to listview
list.setAdapter(adapter);
//click listener
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
//ListView Clicked item index
int itemPosition = position;
int[] points = new int[]{120,150,200,200,300,400,600};
if (total_pts >= points){
//where I am stuck...the conditional statement won't work because I
//am unsure how to say the points(value) at a certain index
//(the index clicked) and then subtract them and then display them
total_pts-=points;
totalPoints.text = "Total Points: \(Total_Points)"
}
}
});
ArrayAdapter adapter=新的ArrayAdapter(这个,android.R.layout.simple\u list\u item\u 1,android.R.id.text1,兑换);
//分配给listview
list.setAdapter(适配器);
//单击侦听器
list.setOnItemClickListener(新的AdapterView.OnItemClickListener(){
@凌驾
public void onItemClick(AdapterView父级、视图、,
内部位置,长id){
//ListView单击的项目索引
int itemPosition=位置;
int[]点=新的int[]{120150200200300400600};
如果(总分数>=分){
//我被卡住了…条件语句不起作用,因为我
//我不确定如何说出某个索引的点(值)
//(单击索引),然后减去它们,然后显示它们
总分-=分;
totalPoints.text=“总分:\(总分)”
}
}
});
您可以使用位置
从点
数组中获取值
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// move it outside oncreate
// no need to recreate this array everytime on click
int[] points = new int[]{120,150,200,200,300,400,600};
if(total_pts>=points[position]){
total_pts-=points[position];
totalPoints.setText("Total Points "+String.valueOf(total_pts));
}//else{ // optional
//totalPoints.setText("Not enough points");
//}
}
});
list.setOnItemClickListener(新的AdapterView.OnItemClickListener(){
@凌驾
public void onItemClick(AdapterView父级、视图、,
内部位置,长id){
//在创建时将其移到外部
//无需每次单击都重新创建此阵列
int[]点=新的int[]{120150200200300400600};
如果(总分数>=点[位置]){
总分数-=点数[位置];
totalPoints.setText(“Total Points”+String.valueOf(Total_pts));
}//else{//可选
//totalPoints.setText(“点数不足”);
//}
}
});
非常不清楚,但似乎您想要如果(total_pts>=points[position]){total_pts-=points[position];/…other code}
我刚刚刷新了android Studio并使用了您的代码。它成功了,谢谢你,如果你把它作为一个answer@TffGhb我很高兴能为您提供帮助,很高兴编码谢谢您,但我将如何在textview标签中显示点。它说转义键在字符串literaloh中是非法的,没有注意到,您需要使用totalPoints.setText(“Total Points”+string.valueOf(Total_pts))代码>好的,所以它给了我一个错误,说它无法连接这些,所以我尝试了这个:String ptsFormat=String.format(res.getString(R.String.total_points),total_pts);totalPoints.setText(ptsFormat)代码>并且它将res
变成红色,表示限定符必须是一个表达式。我发现我必须Resources res=getResources()代码>