2015-05-16 63 views
0

請原諒,如果這裏提到的任何東西都不相關,因爲我是Android開發的首發。Android的ListView與部分標題

我已經經歷了許多SO問題和網上的很多例子解釋所有相同的事情,但沒有一個適合我的要求,因爲我需要動態地列出標題以及列出項目。

終於在這裏找到了一些很好的建議。

我行佈局:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/myview" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:gravity="top" 
    android:orientation="horizontal" 
    android:paddingLeft="10dp" 
    android:paddingRight="10dp" 
    android:paddingTop="5dp" > 


    <LinearLayout 
     android:id="@+id/linearLayout2" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:background="@drawable/bg_card" 
     android:orientation="horizontal" > 

     <LinearLayout 
      android:id="@+id/linearLayout3" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:orientation="horizontal" > 


      <TableLayout 
       android:id="@+id/tableLayout4" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" > 

       <TableRow 
        android:id="@+id/tableRow5" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" > 

        <TextView 
         android:id="@+id/txtTitle" 
         android:layout_width="match_parent" 
         android:layout_height="wrap_content" 
         android:layout_marginLeft="10dp" 
         android:layout_marginStart="10dp" 
         android:layout_weight="1" 
         android:textAppearance="?android:attr/textAppearanceMedium" 
         android:textColor="#000" /> 

        <TextView 
         android:id="@+id/txtTime" 
         android:layout_width="match_parent" 
         android:layout_height="wrap_content" 
         android:layout_marginLeft="20dp" 
         android:layout_marginStart="20dp" 
         android:layout_weight="1" 
         android:textColor="#000" 
         android:textSize="24sp" /> 
       </TableRow> 

       <TableRow 
        android:id="@+id/tableRow6" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" > 

        <TextView 
         android:id="@+id/txtCategory" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:layout_marginLeft="12dp" 
         android:layout_marginStart="12dp" 
         android:layout_weight="1" 
         android:textAppearance="?android:attr/textAppearanceSmall" 
         android:textColor="#0000FF" /> 
       </TableRow> 
      </TableLayout> 
     </LinearLayout> 

     <ImageView 
      android:id="@+id/btndelete" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_alignParentRight="true" 
      android:layout_weight="1" 
      android:scaleType="fitEnd" 
      android:src="@drawable/ic_menu_delete" /> 

    </LinearLayout> 

</RelativeLayout> 

這是我的BaseAdapter:

class CustomAdapter extends BaseAdapter { 

    private static final int TYPE_ITEM = 0; 
    private static final int TYPE_SEPARATOR = 1; 

    private ArrayList<String> mData = new ArrayList<String>(); 
    private TreeSet<Integer> sectionHeader = new TreeSet<Integer>(); 

    private LayoutInflater mInflater; 

    public CustomAdapter(Context context) { 
     mInflater = (LayoutInflater) context 
       .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    } 

    public void addItem(final String title, String category,String time) { 
     mData.add(title); 
     mData.add(category); 
     mData.add(time); 
     notifyDataSetChanged(); 
    } 

    public void addSectionHeaderItem(final String item) { 
     mData.add(item); 
     sectionHeader.add(mData.size() - 1); 
     notifyDataSetChanged(); 
    } 

    @Override 
    public int getItemViewType(int position) { 
     return sectionHeader.contains(position) ? TYPE_SEPARATOR : TYPE_ITEM; 
    } 

    @Override 
    public int getViewTypeCount() { 
     return 2; 
    } 

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

    @Override 
    public String getItem(int position) { 
     return mData.get(position); 
    } 

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

    public View getView(int position, View convertView, ViewGroup parent) { 
     ViewHolder holder = null; 
     int rowType = getItemViewType(position); 

     if (convertView == null) { 
      holder = new ViewHolder(); 
      switch (rowType) { 
      case TYPE_ITEM: 
       //convertView = mInflater.inflate(R.layout.snippet_item1, null); 
       convertView = mInflater.inflate(R.layout.adapter_listview, parent, false); 
       holder.textView = (TextView) convertView.findViewById(R.id.txtTitle); 
       holder.txtCategory = (TextView) convertView.findViewById(R.id.txtCategory); 
       holder.txtTime = (TextView) convertView.findViewById(R.id.txtTime); 
       holder.btndelete = (ImageView) convertView.findViewById(R.id.btndelete); 
       break; 
      case TYPE_SEPARATOR: 
       //convertView = mInflater.inflate(R.layout.adapter_listview, null); 
       convertView = mInflater.inflate(R.layout.snippet_item1, parent, false); 
       holder.textView = (TextView) convertView.findViewById(R.id.text); 
       break; 
      } 
      convertView.setTag(holder); 
     } else { 
      holder = (ViewHolder) convertView.getTag(); 
     } 
     holder.textView.setText(mData.get(position)); 

     return convertView; 
    } 

    public static class ViewHolder { 
     public TextView textView; 
     public TextView txtCategory; 
     public TextView txtTime; 
     public ImageView btndelete; 
    } 

} 

這是我家的片段:

public class HomeFragment extends Fragment { 
    @Override 
     public View onCreateView(LayoutInflater inflater, ViewGroup container, 
       Bundle savedInstanceState) { 
      private CustomAdapter mAdapter; 
      View rootView = inflater.inflate(R.layout.fragment_home, container, false); 
      listViewData = (ListView)rootView.findViewById(R.id.listViewData); 



      return rootView; 
    } 

@Override 
    public void onActivityResult(int requestCode, int resultCode, Intent data) { 
     Log.d(TAG, "onActivityResult called"); 
     if((requestCode == 1)&&(resultCode == Activity.RESULT_OK)){ 
      if(data!= null){ 
        AddedTask=data.getStringExtra("NewTask"); 
        CategoryName=data.getStringExtra("CategoryItem"); 
        mAdapter.addSectionHeaderItem(CategoryName); 
        mAdapter.addItem(AddedTask); 
        mAdapter.addItem(TaskTime); 
        listViewData.setAdapter(mAdapter); 
        mAdapter.notifyDataSetChanged(); 
     } 
    } 
    } 
} 

輸出的結果是:

enter image description here

正如您所看到的,我有一個包含所有視圖的單行佈局,但每個視圖都在每行添加。

相關的代碼是在我的適配器:

public void addItem(final String title, String category,String time) { 
    mData.add(title); 
    mData.add(category); 
    mData.add(time); 
    notifyDataSetChanged(); 
} 

現在我這裏有2個問題。

1.如何改變addItem()得到下面的輸出

enter image description here

2.如何檢查是否添加一個新的,加上其餘的時候存在一個頭該標題下方的項目。

任何人都可以建議我這樣做

回答

0

在這段代碼的一種更好的方式:

public void addItem(final String title, String category,String time) { 
mData.add(title); 
mData.add(category); 
mData.add(time); 
notifyDataSetChanged(); 
} 
您要添加,因此實際上三個項目的結果

爲了獲得理想的結果,嘗試這樣的事:

class Item{ 
private String mTitle; 
private String mCategory; 
private String mTime; 
public Item(String title,String time,String category){ 
mTitle=title; 
mTime=time; 
mCategory=category; 
} 
} 

而在的addItem():

public void addItem(final String title, String category,String time) { 
mData.add(new Item(title,category,time)); 
notifyDataSetChanged(); 
} 

並更改BaseAdapter類從列表中獲得項目類的對象,因此然後分配elemnets其相應的標題,時間和類別

希望它有幫助:)

1

您CustomAdapter更改爲

class CustomAdapter extends BaseAdapter { 

private static final int TYPE_ITEM = 0; 
private static final int TYPE_SEPARATOR = 1; 

private Map<String, ArrayList<String>> mData = new HashMap<String, ArrayList<String>>(); 

private LayoutInflater mInflater; 

public CustomAdapter(Context context) { 
    mInflater = (LayoutInflater) context 
      .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
} 

public void addItem(final String header, final String... contents) { 
    if (mData.containsKey(header)) { 
     ArrayList<String> item = new ArrayList<String>(mData.get(header)); 
     for(String content : contents) { 
      item.add(content); 
     } 
     mData.remove(header); 
     mData.put(header, item); 
    } else { 
     ArrayList<String> item = new ArrayList<String>(); 
     for(String content : contents) { 
      item.add(content); 
     } 
     mData.put(header, item); 
    } 
    notifyDataSetChanged(); 
} 


@Override 
public int getItemViewType(int position) { 
    return (position&1) == 0 ? TYPE_SEPARATOR : TYPE_ITEM; 
} 

@Override 
public int getViewTypeCount() { 
    return 2; 
} 

@Override 
public int getCount() { 
    return mData.size() * 2; 
} 

@Override 
public Object getItem(int position) { 
    if (position&1 == 0) { 
     Set<String> headers = mData.keySet(); 
     Iterator<String> iterator = headers.iterator(); 
     for (int i = 0; i < position/2 && iterator.hasNext(); i++) { 
      iterator.next(); 
     } 
     return iterator.hasNext() ? iterator.next() : null; 
    } else { 
     Set<ArrayList<String>> items = mData.values(); 
     Iterator<ArrayList<String>> iterator = items.iterator(); 
     for (int i = 0; i < position/2 && iterator.hasNext(); i++) { 
      iterator.next(); 
     } 
     return iterator.hasNext() ? iterator.next() : null; 
    } 
} 

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

public View getView(int position, View convertView, ViewGroup parent) { 
    ViewHolder holder = null; 
    int rowType = getItemViewType(position); 

    if (convertView == null) { 
     switch (rowType) { 
     case TYPE_ITEM: 
      //convertView = mInflater.inflate(R.layout.snippet_item1, null); 
      convertView = mInflater.inflate(R.layout.adapter_listview, parent, false); 
      ArrayList<String> items = getItem(position); 
      //handle your items here! 
      (TextView) convertView.findViewById(R.id.txtTitle).setText() 
      (TextView) convertView.findViewById(R.id.txtCategory).setText(); 
      (TextView) convertView.findViewById(R.id.txtTime).setText(); 
      (ImageView) convertView.findViewById(R.id.btndelete); 
      break; 
     case TYPE_SEPARATOR: 
      //convertView = mInflater.inflate(R.layout.adapter_listview, null); 
      convertView = mInflater.inflate(R.layout.snippet_item1, parent, false); 
      (TextView) convertView.findViewById(R.id.text).setText(getItem(position)); 
      break; 
     } 
     convertView.setTag(holder); 
    } else { 
     holder = (ViewHolder) convertView.getTag(); 
    } 

    return convertView; 
} 

}

+0

@您的回覆jobcrazy- Thnks。我現在在您的代碼中遇到了幾個問題 1)private Map > mData = new HashMap (); 2)什麼是內容(字符串內容:內容){itemId add(內容); } – coder

+0

1)em,這是一個錯誤,應該是私人地圖> mData = new HashMap >(); 2)內容是你的標題,catrgory,時間,以及除標題之外的其他內容 – jobcrazy

+0

@ jobcrazy-Srry我無法得到你仍然是什麼意思的內容它showinng我錯誤的內容作爲內容不能解析爲變量 – coder