2012-07-24 26 views
2

用戶從列表視圖中的一系列選項中選擇答案。提交後(正確或錯誤),我打算在列表視圖中突出顯示正確的答案。在listivew中使背景爲黃色以獲得正確答案

我以爲我已經想通:

// get the position from the array we fed into the list view that has the correct answer  
int correct = curquestion.GetIndexOfCorrectAnswer(); 
// set that item's background to yellow 
lView.getChildAt(correct).setBackgroundColor(Color.YELLOW); 

我發現,雖然通過觀看這部運行,並閱讀其他人的帖子herem是getChildAt有時不提供了可靠的result-它似乎挑隨機的孩子設置黃色。有關其他方法的任何建議?

+0

櫃面有人提出這一點,可惜我已經嘗試過了:((查看)lView.getItemAtPosition(正確))setBackgroundColor(Color.YELLOW); – tobylang 2012-07-24 05:52:24

+0

'lView'中是否有更多的元素,比你可以放在屏幕上而不滾動'lView'? – overbet13 2012-07-24 06:05:21

+0

請注意,如果'lView'中的元素多於無需滾動而放在屏幕上的元素,則在滾動時熄滅屏幕的元素將被回收(如果正確構建了「lView」)。我的意思是,無論在備份這個listView的適配器中有多少元素,listView將始終具有相同數量的子元素。這可以欺騙你,因爲你建立的模型來解決你的問題。 – overbet13 2012-07-24 06:13:07

回答

1

在輸入要設置列表項的位置顏色:

adapter.setSelectedPosition(position); 
adapter.setNotifyOnChange(true); 
listView.invalidate(); 

和適配器類:

//改變基於選中狀態

if(selectedPos == position){ 
    label.setBackgroundColor(Color.YELLOW); 
}else{ 
    label.setBackgroundColor(Color.TRANSPARENT); 
} 

行顏色希望這可以幫助。

+0

我不熟悉setselectedposition,它似乎不在我的ArrayAdapter 適配器中;我用來填充我的清單,會做一些閱讀。 – tobylang 2012-07-24 05:59:17

+0

創建您自己的適配器來填充列表,它將工作。 – 2012-07-24 06:18:41

+0

你可以重寫getView方法,也可以使用arrayadater。 – jeet 2012-07-24 06:20:58

0

您應該實施OnItemClickListener抽象類(link),您應該使用setOnItemClickListener()方法註冊ListView對象。並且,在onItemClick(AdapterView<?> parent, View view, int position, long id)回調方法中,您可以測試用戶是否選擇了正確的答案,並且您有用戶點擊的視圖,因此您可以將其着色爲黃色。

+0

好想法,但所有這些都會告訴我他們點擊的是否是正確的答案。如果其他選項之一是正確的答案,我仍然無法找到它。 – tobylang 2012-07-24 05:46:01

+0

然後,您應該爲您的問題使用另一個模型(例如創建一個自定義視圖,該模型由更好的模型來解決此問題)。 – overbet13 2012-07-24 05:47:34

+0

我真正的例子是使用自定義視圖(它也遭受了示例問題),我爲了得到一些建議而進行了簡化。任何人有任何其他的想法? – tobylang 2012-07-24 05:52:10

0

設置on提交按鈕單擊以下代碼 具有正確的整數變量作爲使用-1初始化的活動字段;

// get the position from the array we fed into the list view that has the correct answer  
correct = curquestion.GetIndexOfCorrectAnswer(); 
adapter.notifyDataSetInvalidated(); 

和適配器的getView做以下操作:

if(position==correct){ 
    label.setBackgroundColor(Color.YELLOW); 
} 
0

對於任何人誰是有興趣我會後我在這裏最終代碼:

 lView.setEnabled(false); 
     btnsubmit.setEnabled(true); 
     int correct = curquestion.GetIndexOfCorrectAnswer(); 

     // tell the system the correct answer 
     adapters.SetSelectedPosition(correct); 
     //adapters.setNotifyOnChange(true); 
     adapters.notifyDataSetChanged(); 

上面的代碼被稱爲上活動,下面是處理程序的自定義類。其他的xml文件你可能會想到。

import android.app.Activity; 
import android.content.Context; 
import android.graphics.Color; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.ArrayAdapter; 
import android.widget.CheckedTextView; 
import android.widget.TextView; 
import com.example.walpdroid2.R; 

public class QuizDataAdapter extends ArrayAdapter<Answer> 
{ 
Answer [] objects = null; 
Context checkcontext; 
int _selectedIndex; 
int checkedviewresourceid; 
int uncheckedviewresourceid; 

int selectedPos = -1; 

public QuizDataAdapter(Context checkcontext, int checkresourceid, int uncheckresourceid, Answer[] objects) 
{ 
    super(checkcontext, checkresourceid, objects); 
    this.checkedviewresourceid = checkresourceid; 
    this.uncheckedviewresourceid = uncheckresourceid; 
    this.checkcontext = checkcontext; 
    this.objects = objects; 

} 

public int getCount() 
{ 
    return this.objects.length; 
} 

public Answer getItem(int position) 
{ 
    return this.objects[position]; 
} 

public long getItemId(int position) 
{ 
    return position; 
} 

public void setSelected(int index) { 

     if (index == -1) { 
     // unselected 
     } 
     else { 
     // selected index... 
     } 

     _selectedIndex = index; 

     // notify the model that the data has changed, need to update the view 
     notifyDataSetChanged(); 

} 

public void SetSelectedPosition(int pos) 
{ 
    this.selectedPos = pos; 
} 

@Override 
public View getView(int position, View convertView, ViewGroup parent) 
{ 
    View row = convertView; 
    CheckedTextView holder = null; 

    if((row == null) || (selectedPos == position)) 
    { 
     LayoutInflater inflater = ((Activity)checkcontext).getLayoutInflater(); 

     if(selectedPos == position) 
     { 
      row = inflater.inflate(checkedviewresourceid, parent, false); 
      holder = (CheckedTextView)row.findViewById(R.id.CheckRow); 
     } 
     else 
     { 
      row = inflater.inflate(uncheckedviewresourceid, parent, false); 
      holder = (CheckedTextView)row.findViewById(R.id.UnCheckRow);     
     } 

     holder.setText(objects[position].getAnswer()); 
     row.setTag(holder); 
    } 
    else 
    { 
     holder = (CheckedTextView)row.getTag(); 
     holder.setText(objects[position].getAnswer()); 
    } 


    return row; 
} 

}

相關問題