2012-10-30 53 views
0

所以我看了很多其他的答案,但一直沒有能夠讓我的應用程序工作,我想如何。我基本上想要列表視圖,其文本和check markright,但隨後在左邊添加按鈕。現在我的列表視圖顯示出來,但檢查圖像從未改變。自定義ListView沒有被點擊選擇

編輯:經過有用的評論後,我發現可以通過在模擬器上使用箭頭(向上/向下)選擇行,這會突出顯示我想要的行。但是,當我點擊行時,它不會像我想要的那樣被選中。此外,如果它有助於在對話框中使用列表視圖。

選擇:

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item 
     android:state_selected="true" 
     android:drawable="@drawable/accept_on" /> 
    <item 
     android:drawable="@drawable/accept" /> 
</selector> 

行的xml:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/layout" 
    android:orientation="horizontal" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:padding="10dp" 
    android:background="#EEE"> 

    <ImageButton 
     android:id="@+id/goToMapButton" 
     android:src="@drawable/go_to_map" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:gravity="left" /> 

    <TextView 
     android:id="@+id/itemName" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:gravity="center_vertical" 
     android:textColor="#000000" 
     android:layout_marginTop="5dp" 
     android:layout_marginBottom="5dp" 
     android:layout_weight="1"  /> 

    <Button 
     android:id="@+id/checkButton" 
     android:background="@drawable/item_selector" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:gravity="right"  /> 
</LinearLayout> 

MapAdapter:

import android.content.Context; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.ArrayAdapter; 
import android.widget.Button; 
import android.widget.ImageButton; 
import android.widget.LinearLayout; 
import android.widget.TextView; 

public class MapAdapter extends ArrayAdapter<String>{ 

    Context context; 
    int layoutResourceId;  
    String data[] = null; 
    LayoutInflater inflater; 
    LinearLayout layout; 

    public MapAdapter(Context context, int layoutResourceId, String[] data) { 
     super(context, layoutResourceId, data); 
     this.layoutResourceId = layoutResourceId; 
     this.context = context; 
     this.data = data; 
     inflater = LayoutInflater.from(context); 
    } 

    @Override 
    public String getItem(int position) { 
     return data[position]; 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     ViewHolder holder = new ViewHolder(); 

     if(convertView == null) 
     { 
      convertView = inflater.inflate(R.layout.map_item_row, null); 
      layout = (LinearLayout)convertView.findViewById(R.id.layout); 
      holder.map = (ImageButton)convertView.findViewById(R.id.goToMapButton); 
      holder.name = (TextView)convertView.findViewById(R.id.itemName); 
     //holder.check = (Button)convertView.findViewById(R.id.checkButton); 

      convertView.setTag(holder); 
     } 
     else 
     { 
      holder = (ViewHolder) convertView.getTag(); 
     } 

     layout.setBackgroundColor(0x00000004); 
     holder.name.setText(getItem(position)); 

     return convertView; 
    } 

    static class ViewHolder 
    { 
     ImageButton map; 
     TextView name; 
     Button check; 
    } 
} 
+1

在模擬器試試這個,然後滾動列表(按鍵盤的向下箭頭按鈕使用軌跡球不點擊列表中),那麼你的複選框,圖像會改變 –

+0

好了,所以我想,和它像你說的那樣工作。現在,我希望它在單擊某行時更改,並且一次檢查多行。我有CHOICE_MODE_MULTIPLE設置。 – jgelderloos

+0

請維護一個布爾數組,其中包含true和false only.eg如果ListView包含5個項目,然後取一個大小爲5的布爾數組,並在其所有5個位置開始都包含false(這表示所有複選框在開始時都未選中)並使如果任何位置爲true,則在getView(int p,View c,ViewGroup p)方法中進行檢查,然後在該位置檢查複選框,否則將使其未選中,如果單擊列表位置,則在數組中調用該位置並調用適配器。 notifyDatasetChanged() –

回答

0

嘗試通過選擇器設置爲背景而不是SRC

0

嘗試這樣

@Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     // Planet to display 
     Planet planet = (Planet) this.getItem(position); 

     // The child views in each row. 
     CheckBox checkBox; 
     TextView textView; 

     // Create a new row view 
     if (convertView == null) { 
      convertView = inflater.inflate(R.layout.simplerow, null); 

      // Find the child views. 
      textView = (TextView) convertView.findViewById(R.id.rowTextView); 
      checkBox = (CheckBox) convertView.findViewById(R.id.CheckBox01); 

      // Optimization: Tag the row with it's child views, so we don't 
      // have to 
      // call findViewById() later when we reuse the row. 
      convertView.setTag(new PlanetViewHolder(textView, checkBox)); 

      // If CheckBox is toggled, update the planet it is tagged with. 
      checkBox.setOnClickListener(new View.OnClickListener() { 
       public void onClick(View v) { 
        CheckBox cb = (CheckBox) v; 
        Planet planet = (Planet) cb.getTag(); 
        planet.setChecked(cb.isChecked()); 
       } 
      }); 
     } 
     // Reuse existing row view 
     else { 
      // Because we use a ViewHolder, we avoid having to call 
      // findViewById(). 
      PlanetViewHolder viewHolder = (PlanetViewHolder) convertView 
        .getTag(); 
      checkBox = viewHolder.getCheckBox(); 
      textView = viewHolder.getTextView(); 
     } 

     // Tag the CheckBox with the Planet it is displaying, so that we can 
     // access the planet in onClick() when the CheckBox is toggled. 
     checkBox.setTag(planet); 

     // Display planet data 
     checkBox.setChecked(planet.isChecked()); 
     textView.setText(planet.getName()); 

     return convertView; 
    } 

見這個例子http://windrealm.org/tutorials/android/listview-with-checkboxes-without-listactivity.php

+0

鏈接不起作用 – jgelderloos