2011-07-26 16 views
0

我有一個具有顯示查詢結果列表的清單活動。那麼我希望能夠點擊每個項目和項目更改顏色,但它不起作用。我希望項目保持選擇狀態,直到按下「接受」按鈕或再次按下項目。我知道文本框是如何工作的,但我更喜歡以自己的方式來完成。ListView和適配器列表中的Android變色

這裏是我的代碼:

public void createList() { 

    if (ok == 1) { 
    //hay muachas possibilidades 
    if (sol.get(i).getMultiseleccion() != 0){ 

     bt2.setVisibility(View.INVISIBLE); 
    }else { 
     //solo se clika en una 
     //lv.setChoiceMode(ListView.CHOICE_MODE_SINGLE); 
     bt2.setVisibility(View.VISIBLE); 
    } 

     String hd1 = sol.get(i).getDescSolicitud(); 

     tv2.setText(hd1); 

     ArrayList<SubSolicitud> sub = sol.get(i).getSubSol(); 
     mAdapter = new EventAdapter(this, sub); 
     setListAdapter(mAdapter); 
     lv.setTextFilterEnabled(true); 
     lv.computeScroll(); 
     lv.setDividerHeight(1); 
     lv.setItemsCanFocus(false); 
     lv.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE); 
     lv.setOnItemClickListener(new OnItemClickListener() { 

      @Override 
      public void onItemClick(AdapterView<?> arg0, View arg1, 
        int position, long arg3) { 

        ok = 1; 
        //OnListClick(position, arg1); 
        if (sol.get(i).getMultiseleccion() != 0) { 
         // multiples respuestas 

           ((EventEntryView)arg1).text1.setTextColor(Color.YELLOW); 

         guardarRespuesta(); 
        }else { 
        buscarElementos(); 
        } 

      } 

     }); 
    } 

    // informar el usuario de que hay un error 
    else 
     buildAlertDialog(); 

} 

和其他類: 公共類EventAdapter延伸BaseAdapter {

public ArrayList<SubSolicitud> mEvents = null; 

    public EventAdapter(Context c, ArrayList<SubSolicitud> subsol) { 
     mContext = c; 
     mEvents = subsol; 
    } 

    public int getCount() { 
     return mEvents.size(); 
    } 

    public Object getItem(int position) { 
     return position; 
    } 

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

    public View getView(int position, View convertView, ViewGroup parent) { 
     EventEntryView btv; 
     if (convertView == null) { 
      btv = new EventEntryView(mContext, mEvents.get(position)); 

     } else { 
      btv = (EventEntryView) convertView; 
      String title1 = mEvents.get(position).getDescripcion(); 

      if (title1 != null) { 
       btv.setText1Title(title1); 
      } 

     } 
     btv.setBackgroundColor(Color.BLACK); 

     return btv; 

    } 

    private Context mContext; 

    public void clearEvents() { 
     mEvents.clear(); 
     notifyDataSetChanged(); 
    } 

    public void addEvent(SubSolicitud e) { 
     mEvents.add(e); 

    } 

} 

public class EventEntryView extends LinearLayout { 

    // private View inflatedView; 
    private TextView text1; 

    // private TextView text2; 

    public EventEntryView(Context context, SubSolicitud subSolicitud) { 
     super(context); 
     this.setOrientation(VERTICAL); 



     text1=new TextView(context); 
     text1.setTextSize(20); 
     text1.setPadding(10, 10, 10, 10); 
     text1.setTextColor(Color.WHITE); 
     String t = subSolicitud.getDescripcion(); 
     text1.setText(t); 

     addView(text1, new LinearLayout.LayoutParams(
       LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); 

    } 

    public void setText1Title(String title1) { 
     // TODO Auto-generated method stub 
     text1.setText(title1); 

    } 

} 

正如你可以看到我試圖讓黃色的文字,但它不起作用,我點擊它不會變黃。

有沒有解決方案?

謝謝

+0

這個問題已經在這裏找到答案:http://stackoverflow.com/questions/6731167/change-list-background-problem/6731304#6731304 –

+0

感謝,但是這不是我想要的。他要去另一個活動,但我不加他有Android:choiceMode =「singleChoice」但我不能夠選擇幾個項目。 – vallllll

回答

2

它不起作用,因爲列表中的每個項目都沒有EventEntryView - 重複使用相同的EventEntryView來呈現每個項目。

您需要將SubSolicitud模型對象添加一些東西來表示它已經選擇(比方說,「選擇」屬性一個布爾值)。

在你onItemClicked處理程序,你會觸發這個屬性 -

public void onItemClick(AdapterView<?> adapterView, View view, 
       int position, long id) { 
    // ... 
    SubSolicitud selectedSubSol = (SubSolicitud)adapterView.getAdapter().getItem(id); 
    boolean currentValue = selectedSubSol.isSelected(); 
    selectedSubSol.setSelected(!currentValue); // toggle 'selected' on and off 
    // ... 
} 

(你還需要解決您的EventAdaptergetItem方法返回mEvents.get(position)這個工作......)

然後在你的EventAdaptergetView方法,您使用「selected」屬性的值呈現文本顏色 -

public View getView(int position, View convertView, ViewGroup parent) { 
    // ... 
    if (mEvents.get(position).isSelected()) { 
     btv.text1.setTextColor(Color.YELLOW); 
    } else { 
     // you have to have an else to set it back to the default 
     // color, because the view is reused for all list items. 
     btv.text1.setTextColor(Color.WHITE); 
    } 
    // ... 
} 
+0

很多你剛剛救了我的一天這種方法很好,加上它解決了我的另一個問題! – vallllll

0

這就是你如何改變顏色。

public void onItemClick(AdapterView<?> arg0, View arg1, 
         int position, long arg3) { 

         position = position - listView.getFirstVisibleItem(); 
         ((EditText)arg0.getChildAt(position).findViewById(R.id.myTextView)).setTextColor(Color.YELLOW); 

    } 

但是,如果你想從你應該通過列表視圖中的每一項重複的顏色釋放項目,並改回正常的,或者你可以做到這一點的getView()內,因爲它被稱爲每次有行動在列表視圖