상세 컨텐츠

본문 제목

안드로이드 Group List 관련

스마트기기개발관련/안드로이드 개발

by AlrepondTech 2011. 5. 17. 14:18

본문

반응형
728x170

 

 

=================================

=================================

=================================

 

 

 

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.app.ExpandableListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.SimpleExpandableListAdapter;
import android.widget.TextView;
import android.widget.Toast;
import co.taebong.encyclopedias.franchise.Franchise;
import co.taebong.encyclopedias.news.News;
import co.taebong.encyclopedias.tradingarea.newSangkyon1;
import co.taebong.items.beginner.Beginner;
import co.taebong.items.franchise.ItemFranchise;
import co.taebong.items.littlecapital.LittleCapital;
import co.taebong.items.trustivest.TrustIvest;
import co.taebong.items.trustmanage.TrustManage;
import co.taebong.mynote.knowhow.KnowHow;
import co.taebong.mynote.recommend.RecommendItems;
import co.taebong.mynote.saveinfo.SaveInfo;

public class ExpList extends ExpandableListActivity {
private static final String LOG_TAG = "ElistCBox";

static final String colors[] = { "", "", "",

}; // 이부분에서 group list, 작성

static final String shades[][] = {

{ "", "", "" },

{ "", "", "", "", "즈" },

{ "", "", "" }

}; // group list에 따른 child list

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle icicle)
{
super.onCreate(icicle);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.expand);
SimpleExpandableListAdapter expListAdapter =
new SimpleExpandableListAdapter(
this,
createGroupList(), // groupData describes the first-level entries
R.layout.group_row, // Layout for the first-level entries
new String[] { "GroupName" }, // Key in the groupData maps to display
new int[] { R.id.childname }, // Data under "colorName" key goes into this TextView
createChildList(), // childData describes second-level entries
R.layout.child_row, // Layout for second-level entries
new String[] { "ChildName" }, // Keys in childData maps to display
new int[] { R.id.rgb } // Data under the keys above go into these TextViews
){

// groupposition 을 받아와 switch문으로 background 그려줌

public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
final View v = super.getGroupView(groupPosition, isExpanded, convertView, parent);

// Populate your custom view here

switch(groupPosition){

case 0:

((TextView)v.findViewById(R.id.childname)).setBackgroundResource(R.drawable.main_button_01_ex);

break;

case 1:
((TextView)v.findViewById(R.id.childname)).setBackgroundResource(R.drawable.main_button_02_ex);
break;

case 2:
((TextView)v.findViewById(R.id.childname)).setBackgroundResource(R.drawable.main_button_03_ex);
break;




}
return v;
}


// childposition 을 받아와 switch문으로 background 그려줌
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
final View v = super.getChildView(groupPosition, childPosition, isLastChild, convertView, parent);

// Populate your custom view here
if(groupPosition== 0) {
switch(childPosition){

case 0:

((TextView)v.findViewById(R.id.rgb)).setBackgroundResource(R.drawable.main_button_sub_01_ex);

break;

case 1:
((TextView)v.findViewById(R.id.rgb)).setBackgroundResource(R.drawable.main_button_sub_02_ex);
break;

case 2:
((TextView)v.findViewById(R.id.rgb)).setBackgroundResource(R.drawable.main_button_sub_03_ex);
break;

}
}
return v;


}
};
setListAdapter( expListAdapter );
}

@Override
public void onGroupExpand(int groupPosition) {
Log.d(LOG_TAG, "onGroupExpand: " + groupPosition);
}

private List<HashMap<String, String>> createGroupList() {
ArrayList<HashMap<String, String>> result = new ArrayList<HashMap<String, String>>();
for (int i = 0; i < colors.length; ++i) {
HashMap<String, String> m = new HashMap<String, String>();
m.put("GroupName", colors[i]);
result.add(m);
}
return (List<HashMap<String, String>>) result;
}

private List<ArrayList<HashMap<String, String>>> createChildList() {
ArrayList<ArrayList<HashMap<String, String>>> result = new ArrayList<ArrayList<HashMap<String, String>>>();
for (int i = 0; i < shades.length; ++i) {
// Second-level lists
ArrayList<HashMap<String, String>> secList = new ArrayList<HashMap<String, String>>();
for (int n = 0; n < shades[i].length; n += 1) {
HashMap<String, String> child = new HashMap<String, String>();
child.put("ChildName", shades[i][n]);
secList.add(child);
}
result.add(secList);
}
return result;
}

tabView tab = new tabView();

private String TAG;

 

 

// 각 group , child position을 받아와 거기에 따른 실행을 결정해준다

public boolean onChildClick(android.widget.ExpandableListView parent,
View v, int groupPosition, int childPosition, long id) {
int grposition = groupPosition;
int chposition = childPosition;
long ids = id;

Log.d(TAG, "toast" + " " + grposition + " " + chposition + " " + ids);
if (grposition == 0) {
switch (chposition) {

case 0:

// tab.TabView();
Intent sang = new Intent(ExpList.this, newSangkyon1.class);
startActivity(sang);
finish();
break;
case 1:

Intent chang = new Intent(ExpList.this, News.class);
startActivity(chang);
finish();
break;

case 2:
Intent fran = new Intent(ExpList.this, Franchise.class);
startActivity(fran);

finish();

break;
}
} else if (grposition == 1) {
switch (chposition) {

case 0:


Intent intent = new Intent(getApplicationContext(),
LittleCapital.class);

int page = 1;
intent.putExtra("page", page);
String div = "1";
intent.putExtra("div", div);

startActivity(intent);
finish();
break;
case 1:

Intent intent1 = new Intent(getApplicationContext(),
TrustManage.class);

int page1 = 1;
intent1.putExtra("page", page1);
String div1 = "2";
intent1.putExtra("div", div1);

startActivity(intent1);
finish();
break;
case 2:
Intent intent2 = new Intent(getApplicationContext(),
TrustIvest.class);

int page2 = 1;
intent2.putExtra("page", page2);
String div2 = "3";
intent2.putExtra("div", div2);

startActivity(intent2);
finish();
break;
case 3:

Intent intent3 = new Intent(getApplicationContext(),
Beginner.class);

int page3 = 1;
intent3.putExtra("page", page3);
String div3 = "4";
intent3.putExtra("div", div3);

startActivity(intent3);
finish();
break;
case 4:

Intent intent4 = new Intent(getApplicationContext(),
ItemFranchise.class);

int page4 = 1;
intent4.putExtra("page", page4);
String div4 = "5";
intent4.putExtra("div", div4);

startActivity(intent4);
finish();
break;

}
} else if (grposition == 2) {
switch (chposition) {

case 0:

Intent note = new Intent(ExpList.this, SaveInfo.class);
startActivity(note);
finish();
break;
case 1:

Intent rec = new Intent(ExpList.this, RecommendItems.class);
int page = 1;
rec.putExtra("page", page);
startActivity(rec);
finish();
break;
case 2:
Intent knowhow = new Intent(ExpList.this, KnowHow.class);
startActivity(knowhow);
finish();
break;

}

}

return true;
}

}

 

 

 

=================================

=================================

=================================

 

 


출처: http://jeehun.egloos.com/3999264

어제에 이어 오늘도 GroupList를 구현해보도록 하겠습니다.
먼저 SimpleCursorAdapter 를 상속받아 커스텀 커서 어댑터를 만들어야 합니다.
이 커서 어댑터에서 커서 데이터를 바인드 하면서 새로운 그룹인지 아닌지를
판단하여 새로운 그룹이 나타나면 헤더(그룸명)를 보여주고 아니면 숨겨주고
하는 방식으로 그룹화 된 리스트를 구현하게 됩니다.

먼저 리스트에 데이터를 뿌릴때 사용할 item layout을 정의하도록 하겠습니다.

note_list_item_w_header.xml 파일을 아래와같이 만듭니다.
여기에서 리니어 레이아웃으로 정의된 llHeader 부분이 새로운 그룹이
나타남 여부에 따라 보였다 사라졌다 할 것입니다.
**********************note_list_item_w_header.xml***********************
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
 xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical"
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:gravity="center_vertical">
 <LinearLayout
  android:id="@+id/llHeader"
  android:layout_width="fill_parent"
  android:layout_height="30dip"
  android:gravity="center_vertical|left"
  android:background="#8696A5">
  <TextView
   android:text="text"
   android:id="@+id/tvHeader"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:gravity="center_vertical"
   android:textColor="#FFFFFF"
   android:paddingLeft="5dp"
   android:singleLine="true"
   android:textSize="18sp" />
 </LinearLayout> 
 <TextView
     android:id="@+id/tvNote"
     android:layout_width="fill_parent"
     android:layout_height="50dp"
     android:singleLine="true"
     android:textColor="#060004"
     android:textStyle="bold"
     android:typeface="monospace"
     android:textSize="18sp"
     android:paddingLeft="5dp"
     android:gravity="center_vertical" /> 
</LinearLayout>
************************************************************************

다음은 커스텀 커서 어댑터 클래스입니다.
아래와 같이 만들어 줍니다.
별로 어려울 것은 없습니다. bindView 에서 새로운 그룹 여부를 판단하여
새로운 그룹일때 헤더를 보이게 하고 같은 그룹이 계속 될 경우 헤더를
사라지게 합니다.
************************NoteCursorAdapter.java *************************
package com.android.grouplist;

import com.android.grouplist.GroupListInfo.TbNote;

import android.content.Context;
import android.database.Cursor;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;

public class NoteCursorAdapter extends SimpleCursorAdapter
{
  private int mLayout;
  private static final int VIEW_TYPE_GROUP_START = 0;
  private static final int VIEW_TYPE_GROUP_CONT = 1;

  public NoteCursorAdapter(Context context, int layout, Cursor c, String[] from, int[] to)
  {
    super(context, layout, c, from, to);
   
    mLayout = layout;
  }
 
  @Override
  public View newView(Context context, Cursor cur, ViewGroup parent)
  {
    LayoutInflater li = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    return li.inflate(mLayout, parent, false);
  }

  @Override
  public void bindView(View view, Context context, Cursor cur)
  {
    String note = cur.getString(cur.getColumnIndex(TbNote.NOTE));
    String header = cur.getString(cur.getColumnIndex(TbNote.DATE));
    int position = cur.getPosition();
    int viewType;
    LinearLayout llHeader = (LinearLayout) view.findViewById(R.id.llHeader);
   
    if (position == 0)
    {
      viewType = VIEW_TYPE_GROUP_START;
    }
    else
    {
      boolean newGroup = isNewGroup(cur, position);
     
      if (newGroup)
        viewType = VIEW_TYPE_GROUP_START;
      else
        viewType = VIEW_TYPE_GROUP_CONT;
    }
   
    if (viewType == VIEW_TYPE_GROUP_START)
      llHeader.setVisibility(0x00000000); // Visiable
    else
      llHeader.setVisibility(0x00000008); // Gone
   
    TextView tvNote = (TextView) view.findViewById(R.id.tvNote);
    TextView tvHeader = (TextView) view.findViewById(R.id.tvHeader);
    tvNote.setText(note);
    tvHeader.setText(header);
  }
 
  private boolean isNewGroup(Cursor cursor, int position)
  {
    String curDate = cursor.getString(cursor.getColumnIndex(TbNote.DATE));
    cursor.moveToPosition(position - 1);
    String preDate = cursor.getString(cursor.getColumnIndex(TbNote.DATE));
    cursor.moveToPosition(position);
   
    if (!curDate.equals(preDate))
    {
      return true;
    }
   
    return false;
  }
}
************************************************************************

다음은 GroupList.java 를 수정합니다.
findNoteList() 메서드만 수정하면 될듯 하네요
아래와 같이 수정해줍니다. 

  private void findNoteList()
  {
    String searchTxt = mEtSearchTxt.getText().toString();
    mTbNoteCursor = mGroupListDAO.selectNoteList(searchTxt);
   
    if (mTbNoteCursor != null)
      Log.v("GroupList", "mTbNoteCursor.count >>>>>>>>>>>>>>>" + mTbNoteCursor.getCount());
   
    mAdapter = new NoteCursorAdapter(this, R.layout.note_list_item_w_header, mTbNoteCursor, new String[] {TbNote.NOTE}, null);
    setListAdapter(mAdapter);
  }

GroupList.java 의 전체 소스는 아래와 같습니다.
**************************** GroupList.java *****************************
package com.android.grouplist;

import com.android.grouplist.GroupListInfo.TbNote;

import android.app.ListActivity;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;

public class GroupList extends ListActivity implements View.OnClickListener
{
  private GroupListDAO mGroupListDAO;
  private ImageButton mIbtnSearch;
  private EditText mEtSearchTxt;
  private Cursor mTbNoteCursor;
  private NoteCursorAdapter mAdapter;
 
  @Override
  public void onCreate(Bundle savedInstanceState)
  {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    getListView().setTextFilterEnabled(true);
   
    mIbtnSearch = (ImageButton) findViewById(R.id.ibtnSearch);
    mEtSearchTxt = (EditText) findViewById(R.id.etSearchTxt);
   
    mIbtnSearch.setOnClickListener(this);
   
    mGroupListDAO = new GroupListDAO(getApplicationContext());
  }
 
  @Override
  protected void onDestroy()
  {
    super.onDestroy();
   
    try
    {
      if (mTbNoteCursor != null)
        mTbNoteCursor.close();
     
      mGroupListDAO.close();
    }
    catch (Exception e)
    {
      e.printStackTrace();
    }
  }

  // 버튼의 OnClick 이벤트 처리
  @Override
  public void onClick(View v)
  {
    switch(v.getId())
    {
      case R.id.ibtnSearch: // Search Button
        findNoteList();
        break;
      default:
        break;
    }
  }
 
  private void findNoteList()
  {
    String searchTxt = mEtSearchTxt.getText().toString();
    mTbNoteCursor = mGroupListDAO.selectNoteList(searchTxt);
   
    if (mTbNoteCursor != null)
      Log.v("GroupList", "mTbNoteCursor.count >>>>>>>>>>>>>>>" + mTbNoteCursor.getCount());
   
    mAdapter = new NoteCursorAdapter(this, R.layout.note_list_item_w_header, mTbNoteCursor, new String[] {TbNote.NOTE}, null);
    setListAdapter(mAdapter);
  }
}
************************************************************************

그리고 데이터를 조회할때 그룹순으로 정렬을 시켜야 하겠죠
GroupListDAO.java 파일에서 selectNoteList() 메서드의 쿼리를 수정하겠습니다.
다음과 같이 수정합니다. 참... 커서 어댑터를 쓰려면 조회 쿼리에 항상 
"_id" 컬럼이 있어야 합니다. 여기서는 '0' as _id 로 가상 컬럼을 하나
만들어 주었습니다. 실제로 소스코드상에서 이 컬럼을 사용하지는 않습니다. 
커서 어댑터 내부적으로 _id 컬럼을 사용하는듯 합니다.

  /-*
   * selectNoteList
   * @param text (String)
   * @return
   *-
  public Cursor selectNoteList(String searchText)
  {
    StringBuffer sbSql = new StringBuffer();
   
    sbSql.append("select                \n");
    sbSql.append("    '0' as _id        \n");
    sbSql.append("    , date            \n");
    sbSql.append("    , note            \n");
    sbSql.append("from                  \n");
    sbSql.append("     tb_note          \n");
    sbSql.append("where                 \n");
    sbSql.append("    note like '%" + searchText + "%' \n");
    sbSql.append("order by date desc, note \n");
  
    Cursor c = null;
   
    try
    {
      c = mDb.select(sbSql.toString());     
    }
    catch (Exception e)
    {
      e.printStackTrace();
    }
   
    return c;
  }

여기까지 코딩하면 프로젝트는 아래와 같은 모습을 하고 있을겁니다.

프로젝트를 실행시키면 다음과 같은 결과가 나오게 됩니다. 저는 데이터를 31개로 늘려서 집어 넣었습니다.
DatabaseDDL 클래스에 정의된 init 데이터 넣는 쿼리만 추가해서 DBHelper에서 불러주면 데이터가 많이
들어간 상태로 테스트가 가능하겠죠 ^^ DATE 컬럼 기준으로 그룹화 되어 리스트가 나나타는 것을
확인할 수 있습니다.

그럼 그룹 리스트 편은 여기서 마치도록 하겠습니다.
다음번에는 트위터 연동을 다뤄볼까 합니다.

 

 

=================================

=================================

=================================

 

 


출처: http://www.androidpub.com/883122



expandableListView를 이용하고 있는데, 
 
child가 없어도 앞에 indicator가 나오네요.
 
이거 없애는방법을 찾고 있는데, 혹시 구현하신분 있나요?
 




댓글
2010.10.15 20:29:31
id: 그냥가자그냥가자
01.public View getGroupView(int groupPosition, boolean isExpanded, View convertView,
02.ViewGroup parent) {
03.TextView textView = getGenericView();
04.textView.setText(getGroup(groupPosition).toString());
05. 
06.//그룹에 child가 없으면 정해진 이미지를 찍기 위해서 카운트0일때 expand
07.if(getChildrenCount(groupPosition) == 0){
08.getExpandableListView().expandGroup(groupPosition);
09.}
10.return textView;
11.}

1.<selector xmlns:android="http://schemas.android.com/apk/res/android">
2.<item android:state_expanded="true" android:state_empty="true" android:drawable="@drawable/icon48x48_2"/>
3.<item
4.android:state_expanded="true"
5.android:drawable="@drawable/expander_ic_maximized" />
6.<item
7.android:drawable="@drawable/expander_ic_minimized" />
8.</selector>
01.public void onCreate(Bundle savedInstanceState) {
02.super.onCreate(savedInstanceState);
03.// Set up our adapter
04.mAdapter = new MyExpandableListAdapter();
05.setListAdapter(mAdapter);
06.registerForContextMenu(getExpandableListView());
07.StateListDrawable groupIndicator = (StateListDrawable)getResources().getDrawable(R.drawable.group_indicator_01);
08.getExpandableListView().setGroupIndicator(groupIndicator);
09. 
10.}


지금 보시는 예제에 부분부분만 제가 드린대로 바꿔보세요...
그리고 저 인디케이터 이미지는 SDK에서 찾아서 복사해서 실행하셔야 합니다.

댓글
2010.10.15 21:01:15
어묵엔정종
제가 잘 몰라서 그러는데요, 위에 selector나온 xml 리소스는 어디에 있는 건가요?
 
그리고 인디케이터 이미지를 SDK에서 찾으려면 어떻게 해야하나요?
댓글
2010.10.15 21:09:29
id: 그냥가자그냥가자
저 selector나온 xml은 만든겁니다.
expander_ic_max...와 expander_ic_min... 이 두개는 이미지 파일이며...
[sdk_home]/platforms/[안드로이드 버전]/data/res/drawable-hdpi 에서 찾으세요....
댓글
2010.10.15 21:18:35
어묵엔정종
sdk 폴더를 뒤저보니 /res/drawable에 expander_group.xml 파일이 있네요. 그걸 변형해서 쓰라는 말같은데요.
 
근데 의문점이 있어요. 저 xml파일은 group의 layout 파일인것 같은데
 
제가 새로 만든 layout을 코드와 연결하는건 어디에서 해주는 거죠?
 
바쁘실텐데 계속 물어봐서 죄송합니다.
 
 
(수정) 제가 selector를 처음봐서 뭘 몰라서 그랬네요. selector 공부하고 다시 해볼께요
 
댓글
2010.10.15 21:26:47
id: 그냥가자그냥가자
위의 selector xml은
StateListDrawable groupIndicator =
       (StateListDrawable)getResources().getDrawable(R.drawable.group_indicator_01);
이부분과 관련이 깊습니다. 잘 찾아보세요...

댓글
2010.10.15 21:48:15
어묵엔정종
와 님 정말 감사합니다. 덕분에 많이 알고 갑니다. ^^;

 

 

 

=================================

=================================

=================================

 

 



출처; http://blog.naver.com/PostView.nhn?blogId=newefgold777&logNo=90104888248

어플이 실행하자 마자 화면상에 보이는 것과 같이 박스들이 open 되어 표시 됩니다.

 

핵심코드

final int groupCount = listView.getCount();

     for(int i=0; i <groupCount; i++){

         listView.collapseGroup(i);

         listView.expandGroup(i);

     }

 

 


 

 

 

 

=================================

=================================

=================================

 

 



그룹 리스트 그룹버튼 클릭 안되게 하기

출처: 여기

ExpandableListView glist = new ExpandableListView(context);

glist.setOnGroupClickListener(new OnGroupClickListener()
 {
            //psj 그룹 리스트가 클릭되어 차일드가 스위치(펼침or접음)되는것을 막는다.
            public boolean onGroupClick(ExpandableListView arg0, View arg1,
                int groupPosition, long arg3)
            {
            
                return true;
           
            }  
  }

 

 

=================================

=================================

=================================

 

 



그룹 리스트 포커스되었을때 색깔 조절

출처: 여기

ExpandableListView glist = new ExpandableListView(context);

glist.setCacheColorHint(Color.WHITE); // 화이트로 설정

 

=================================

=================================

=================================

 

 



그룹 리스트 화살표 없애기

출처: 여기

ExpandableListView glist = new ExpandableListView(context);

glist.setGroupIndicator(null);; // 화이트로 설정




=================================

=================================

=================================



반응형
그리드형


관련글 더보기

댓글 영역