gpt4 book ai didi

java - 如何在不显示java中的值的情况下为每一行设置一个值

转载 作者:行者123 更新时间:2023-11-29 19:06:35 25 4
gpt4 key购买 nike

我有一个字符串数组,它显示在 listView 上;但是,我希望为这些行中的每一行分配一个 int 数组,以便在单击它时,它将从用户“total_pts”中减去,然后显示在 totalPoints textview 上,如果他们有足够的点开始

这里是TextView和总点变量:

int total_pts =0;
TextView totalPoints = (TextView)findViewById(R.id.totalP);

这是 ListView :

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)"

}
}
});

最佳答案

您可以使用positionpoints 数组中获取值

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");
//}
}
});

关于java - 如何在不显示java中的值的情况下为每一行设置一个值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47002358/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com