2013-02-13 36 views
1

我有一個列表視圖裏面,我有部分標題與複選框。 如果我點擊標題複選框,那麼應該檢查所有的子複選框。如果我保持列表視圖內的複選框,然後我面臨problem.I已附加圖像正是我需要實現。這是我的代碼,我使用創建列表視圖 Listview with section view部分標題與複選框

import java.util.ArrayList; 

import android.app.Activity; 
import android.content.Context; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.view.View.OnClickListener; 
import android.widget.AdapterView; 
import android.widget.ArrayAdapter; 
import android.widget.Button; 
import android.widget.CheckBox; 
import android.widget.ListView; 
import android.widget.TextView; 
import android.widget.Toast; 
import android.widget.AdapterView.OnItemClickListener; 

public class ListViewCheckboxesActivity extends Activity { 

MyCustomAdapter dataAdapter = null; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    //Generate list View from ArrayList 
    displayListView(); 

    checkButtonClick(); 

} 

private void displayListView() { 

    //Array list of countries 
    ArrayList<Country> countryList = new ArrayList<Country>(); 
    Country country = new Country("AFG","Afghanistan",false); 
    countryList.add(country); 
    country = new Country("ALB","Albania",true); 
    countryList.add(country); 
    country = new Country("DZA","Algeria",false); 
    countryList.add(country); 
    country = new Country("ASM","American Samoa",true); 
    countryList.add(country); 
    country = new Country("AND","Andorra",true); 
    countryList.add(country); 
    country = new Country("AGO","Angola",false); 
    countryList.add(country); 
    country = new Country("AIA","Anguilla",false); 
    countryList.add(country); 

    //create an ArrayAdaptar from the String Array 
    dataAdapter = new MyCustomAdapter(this, 
    R.layout.country_info, countryList); 
    ListView listView = (ListView) findViewById(R.id.listView1); 
    // Assign adapter to ListView 
    listView.setAdapter(dataAdapter); 


    listView.setOnItemClickListener(new OnItemClickListener() { 
    public void onItemClick(AdapterView<?> parent, View view, 
    int position, long id) { 
    // When clicked, show a toast with the TextView text 
    Country country = (Country) parent.getItemAtPosition(position); 
    Toast.makeText(getApplicationContext(), 
     "Clicked on Row: " + country.getName(), 
     Toast.LENGTH_LONG).show(); 
    } 
    }); 

} 

private class MyCustomAdapter extends ArrayAdapter<Country> { 

    private ArrayList<Country> countryList; 

    public MyCustomAdapter(Context context, int textViewResourceId, 
    ArrayList<Country> countryList) { 
    super(context, textViewResourceId, countryList); 
    this.countryList = new ArrayList<Country>(); 
    this.countryList.addAll(countryList); 
    } 

    private class ViewHolder { 
    TextView code; 
    CheckBox name; 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 

    ViewHolder holder = null; 
    Log.v("ConvertView", String.valueOf(position)); 

    if (convertView == null) { 
    LayoutInflater vi = (LayoutInflater)getSystemService(
    Context.LAYOUT_INFLATER_SERVICE); 
    convertView = vi.inflate(R.layout.country_info, null); 

    holder = new ViewHolder(); 
    holder.code = (TextView) convertView.findViewById(R.id.code); 
    holder.name = (CheckBox) convertView.findViewById(R.id.checkBox1); 
    convertView.setTag(holder); 

    holder.name.setOnClickListener(new View.OnClickListener() { 
    public void onClick(View v) { 
     CheckBox cb = (CheckBox) v ; 
     Country country = (Country) cb.getTag(); 
     Toast.makeText(getApplicationContext(), 
     "Clicked on Checkbox: " + cb.getText() + 
     " is " + cb.isChecked(), 
     Toast.LENGTH_LONG).show(); 
     country.setSelected(cb.isChecked()); 
    } 
    }); 
    } 
    else { 
    holder = (ViewHolder) convertView.getTag(); 
    } 

    Country country = countryList.get(position); 
    holder.code.setText(" (" + country.getCode() + ")"); 
    holder.name.setText(country.getName()); 
    holder.name.setChecked(country.isSelected()); 
    holder.name.setTag(country); 

    return convertView; 

    } 

} 

private void checkButtonClick() { 


    Button myButton = (Button) findViewById(R.id.findSelected); 
    myButton.setOnClickListener(new OnClickListener() { 

    @Override 
    public void onClick(View v) { 

    StringBuffer responseText = new StringBuffer(); 
    responseText.append("The following were selected...\n"); 

    ArrayList<Country> countryList = dataAdapter.countryList; 
    for(int i=0;i<countryList.size();i++){ 
    Country country = countryList.get(i); 
    if(country.isSelected()){ 
     responseText.append("\n" + country.getName()); 
    } 
    } 

    Toast.makeText(getApplicationContext(), 
     responseText, Toast.LENGTH_LONG).show(); 

    } 
    }); 

} 

} 

Like this image i heed to be implement

+0

代碼說國家適配器,但圖像顯示其不同的東西。 #Confusing – 2013-02-13 07:04:30

+0

它可能不是標題...他們可能已經刪除標題!那些可能是帶有複選框的列表...請參閱如何刪除標題 - http://stackoverflow.com/questions/2591036/how-to-hide-the-title-bar-for-an-activity-in -xml-with-existing-custom-theme – 2013-02-13 07:08:43

+0

是的,因爲我給了設計不是我的應用程序屏幕拍攝,我需要像這樣的設計實現。 – Kumar 2013-02-13 07:09:18

回答

0

那麼你有沒有貼出你真正的代碼,否則我會告訴你確切的解決方案。 現在根據您的示例代碼,所有我可以建議是看出來這是負責getview方法設置複選框狀態行:

holder.name.setChecked(country.isSelected()); 

現在你的頭複選框點擊收聽,所有你需要做的就是將該參數設置爲true。 基於示例,它將是country.setSelected(true);對於在標題點擊時應檢查的所有項目。

然後調用

dataAdapter.notifyDataSetChanged(); 

這將刷新列表視圖和選擇標記所有的複選框。

編輯:

這裏是工作的代碼使用SeparatedListAdapter選擇一個標題下的所有項目:

ListSample.java

import java.util.ArrayList; 
import java.util.HashMap; 
import java.util.Map; 

import android.app.Activity; 
import android.os.Bundle; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.AdapterView; 
import android.widget.AdapterView.OnItemClickListener; 
import android.widget.ArrayAdapter; 
import android.widget.BaseAdapter; 
import android.widget.CheckBox; 
import android.widget.ListView; 
import android.widget.TextView; 
import android.widget.Toast; 

public class ListSample extends Activity implements HeaderClickListener 
    { 

     public final static String ITEM_TITLE = "title"; 
     public final static String ITEM_CAPTION = "caption"; 

     // SectionHeaders 
     private final static String[] days = new String[]{"Mon", "Tue", "Wed", "Thur", "Fri"}; 

     // MENU - ListView 
     private ListView addJournalEntryItem; 

     // Adapter for ListView Contents 
     private SeparatedListAdapter adapter; 

     // ListView Contents 
     private ListView journalListView; 

     public Map<String, ?> createItem(String title, String caption) 
      { 
       Map<String, String> item = new HashMap<String, String>(); 
       item.put(ITEM_TITLE, title); 
       item.put(ITEM_CAPTION, caption); 
       return item; 
      } 

     @Override 
     public void onCreate(Bundle icicle) 
      { 
       super.onCreate(icicle); 
       // Sets the View Layer 
       setContentView(R.layout.main); 
       // Interactive Tools 
       final ArrayAdapter<String> journalEntryAdapter = new ArrayAdapter<String>(this, R.layout.add_journalentry_menuitem, new String[]{"Add Journal Entry"}); 
       // AddJournalEntryItem 
       addJournalEntryItem = (ListView) this.findViewById(R.id.add_journalentry_menuitem); 
       addJournalEntryItem.setAdapter(journalEntryAdapter); 
       addJournalEntryItem.setOnItemClickListener(new OnItemClickListener() 
        { 
         @Override 
         public void onItemClick(AdapterView<?> parent, View view, int position, long duration) 
          { 
           String item = journalEntryAdapter.getItem(position); 
           Toast.makeText(getApplicationContext(), item, Toast.LENGTH_SHORT).show(); 
          } 
        }); 

       // Create the ListView Adapter 
       adapter = new SeparatedListAdapter(this); 
       // Add Sections 
       for (int i = 0; i < days.length; i++) 
       { 
        ArrayList<CategoryList> categoryLists = new ArrayList<CategoryList>(); 
        for(int j = 0; j < 10; j++) 
        { 
        CategoryList categoryList = new CategoryList(); 
        categoryList.setSelected(false); 
        categoryList.setTitle(days[i] + j); 
        categoryLists.add(categoryList); 
        } 
        CustomBaseAdpater baseAdpater = new CustomBaseAdpater(categoryLists); 
        adapter.addSection(days[i], baseAdpater); 
       } 

       // Get a reference to the ListView holder 
       journalListView = (ListView) this.findViewById(R.id.list_journal); 

       // Set the adapter on the ListView holder 
       journalListView.setAdapter(adapter); 

       // Listen for Click events 
       journalListView.setOnItemClickListener(new OnItemClickListener() 
        { 
         @Override 
         public void onItemClick(AdapterView<?> parent, View view, int position, long duration) 
          { 
           String item = (String) adapter.getItem(position); 
           Toast.makeText(getApplicationContext(), item, Toast.LENGTH_SHORT).show(); 
          } 
        }); 
      } 

     private class CustomBaseAdpater extends BaseAdapter 
     { 

      ArrayList<CategoryList> list = null; 
      CustomBaseAdpater(ArrayList<CategoryList> categoryLists) 
      { 
       this.list = categoryLists; 
      } 

      @Override 
      public int getCount() { 
       return list.size(); 
      } 

      @Override 
      public Object getItem(int position) { 
       return null; 
      } 

      @Override 
      public long getItemId(int position) { 
       return 0; 
      } 

      @Override 
      public View getView(int position, View convertView, ViewGroup parent) { 
       ViewHolder viewHolder = null; 
       if (null == convertView) 
       { 
        viewHolder = new ViewHolder(); 
        LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE); 
        convertView = inflater.inflate(R.layout.item_row, null); 
        viewHolder.checkBox = (CheckBox) convertView.findViewById(R.id.checkItem); 
        viewHolder.textView = (TextView) convertView.findViewById(R.id.textTitle); 
        convertView.setTag(viewHolder); 
       } 
       else 
        viewHolder = (ViewHolder) convertView.getTag(); 

       viewHolder.checkBox.setChecked(list.get(position).isSelected()); 
       viewHolder.textView.setText(list.get(position).getTitle()); 

       return convertView; 
      } 

      private class ViewHolder 
      { 
       CheckBox checkBox; 
       TextView textView; 
      } 
      private void checkCompleteSection() 
      { 
       for(CategoryList categoryList : this.list) 
       categoryList.setSelected(true); 
      } 
     } 

     @Override 
     public void refreshSection(String sectionName) { 
      if(adapter.sections.get(sectionName) != null) 
      { 
       ((CustomBaseAdpater)(adapter.sections.get(sectionName))).checkCompleteSection(); 
       adapter.notifyDataSetChanged(); 
      } 
     } 

    } 

SeparatedListAdapter.java

import java.util.LinkedHashMap; 
import java.util.Map; 

import android.content.Context; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.view.ViewGroup; 
import android.widget.Adapter; 
import android.widget.ArrayAdapter; 
import android.widget.BaseAdapter; 
import android.widget.CheckBox; 
import android.widget.Toast; 

public class SeparatedListAdapter extends BaseAdapter 
    { 
     public final Map<String, Adapter> sections = new LinkedHashMap<String, Adapter>(); 
     public final ArrayAdapter<String> headers; 
     public final static int TYPE_SECTION_HEADER = 0; 
     Context context = null; 

     public SeparatedListAdapter(Context context) 
      { 
       headers = new ArrayAdapter<String>(context, R.layout.list_header); 
       this.context = context; 
      } 

     public void addSection(String section, Adapter adapter) 
      { 
       this.headers.add(section); 
       this.sections.put(section, adapter); 
      } 

     public Object getItem(int position) 
      { 
       for (Object section : this.sections.keySet()) 
        { 
         Adapter adapter = sections.get(section); 
         int size = adapter.getCount() + 1; 

         // check if position inside this section 
         if (position == 0) return section; 
         if (position < size) return adapter.getItem(position - 1); 

         // otherwise jump into next section 
         position -= size; 
        } 
       return null; 
      } 

     public int getCount() 
      { 
       // total together all sections, plus one for each section header 
       int total = 0; 
       for (Adapter adapter : this.sections.values()) 
        total += adapter.getCount() + 1; 
       return total; 
      } 

     @Override 
     public int getViewTypeCount() 
      { 
       // assume that headers count as one, then total all sections 
       int total = 1; 
       for (Adapter adapter : this.sections.values()) 
        total += adapter.getViewTypeCount(); 
       return total; 
      } 

     @Override 
     public int getItemViewType(int position) 
      { 
       int type = 1; 
       for (Object section : this.sections.keySet()) 
        { 
         Adapter adapter = sections.get(section); 
         int size = adapter.getCount() + 1; 

         // check if position inside this section 
         if (position == 0) return TYPE_SECTION_HEADER; 
         if (position < size) return type + adapter.getItemViewType(position - 1); 

         // otherwise jump into next section 
         position -= size; 
         type += adapter.getViewTypeCount(); 
        } 
       return -1; 
      } 

     public boolean areAllItemsSelectable() 
      { 
       return false; 
      } 

     @Override 
     public boolean isEnabled(int position) 
      { 
       return (getItemViewType(position) != TYPE_SECTION_HEADER); 
      } 

     @Override 
     public View getView(int position, View convertView, ViewGroup parent) 
      { 
       int sectionnum = 0; 
       for (Object section : this.sections.keySet()) 
        { 
         Adapter adapter = sections.get(section); 
         int size = adapter.getCount() + 1; 

         // check if position inside this section 
         if (position == 0) 
         { 
         View headerView = headers.getView(sectionnum, convertView, parent); 
         headerView.setTag(section); 
         headerView.setOnClickListener(new OnClickListener() { 
           @Override 
           public void onClick(View v) { 
            String sectionName = v.getTag().toString(); 
           Toast.makeText(context, sectionName ,2000).show(); 
           ((HeaderClickListener) context).refreshSection(sectionName);  
           } 
          }); 
         return headerView; 
         } 
         if (position < size) return adapter.getView(position - 1, convertView, parent); 

         // otherwise jump into next section 
         position -= size; 
         sectionnum++; 
        } 
       return null; 
      } 

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

    } 

item_row。 xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="horizontal" > 
    <CheckBox 
     android:id="@+id/checkItem" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_margin="5dp" 
     /> 
    <TextView 
     android:id="@+id/textTitle" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     /> 

</LinearLayout> 
+0

但是如何用複選框實現標題部分。 – Kumar 2013-02-13 07:52:04

+0

我需要你真實的代碼來幫助你。您的數據列表,getview和onclicklistener。所有我可以建議沒有任何代碼創建一個額外的參數isHeader。如果它是真的,用不同的佈局誇大它,暗背景的佈局,否則用淺背景的佈局膨脹,使它看起來像在圖像中。現在在行上單擊確定它是否是標題單擊,設置選中所有項目isSelected = true直到列表中的下一個標題。 – 2013-02-13 07:58:23

+0

你可以看到這個鏈接,我正在關注這個鏈接來實現標題部分。 「http://w2davids.wordpress.com/android-sectioned-headers-in-listviews/」 – Kumar 2013-02-13 08:58:04