2017-10-29 1 views
1

문자열 배열이 있으며 listView에 표시된 내용입니다. 그러나 나는 각 행에 int 배열을 할당해야하므로 클릭 할 때 "total_pts"사용자에서 뺀 다음 totalPoints textview에 표시됩니다.Java에서 값을 표시하지 않고 각 행의 값을 설정하는 방법

여기에 여기

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

     } 
    } 
}); 
+0

매우 불분명하지만 원하는 것처럼 보입니다. if (total_pts> = points [position]) {total_pts- = points [position]; // ..other code}' –

+0

나는 안드로이드 스튜디오를 새로 고치고 코드를 사용했다. 그것은 당신이 대답으로 남겨 두는 경우에 나는 정확한 것으로 표시 할 수 있습니다. 감사합니다. –

답변

2

당신은 points 배열에서 값을 가져 오도록 position을 사용할 수 있습니다

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

@Tff Ghb 나는 행복하게 코딩 할 수있어서 기쁘다. –

+0

감사하지만 어떻게 textview 레이블에 포인트를 표시하겠습니까? 그것은 탈출 키가 문자열 리터럴 –

+1

에 불법입니다 오 통지하지 않았다, 당신은'totalPoints.setText ("총 포인트"+ String.valueOf (total_pts)); '을 사용해야합니다' –

관련 문제