1

この形式のデータベースを持つ単純なアプリケーションを作成したいと考えています。

カテゴリ 項目 1 項目 2 項目 n

カテゴリ 2 item1 item n

このすべてのデータを、カテゴリをセパレータとしてボタン付きのリストビューにロードし、その下にあるすべてのアイテムを、アルファベットのセパレータを使用した典型的な連絡先アプリと同じようにロードします(ただし、すべてのセパレータにボタンがあります)。ニーズ:

  1. データベースは無限のカテゴリを持つことができます
  2. データベースは 1 つのカテゴリに無限のアイテムを持つことができ ここに画像の説明を入力 ます。どんな助けでも大歓迎です。
4

2 に答える 2

4

これがあなたを助けるものです、私は私のアプリケーションでそのようなことをしました。

まず、データを保持するためのいくつかの xml を作成します これは私の activity_main です

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

<ExpandableListView
    android:id="@+id/listView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/listbg" 
    >
</ExpandableListView>

Xml 2; listrow_details

  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="40dp"
android:clickable="true"
android:orientation="vertical"
android:paddingLeft="40dp"
tools:context=".MainActivity" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:drawableLeft="@drawable/ic_launcher"
    android:drawablePadding="5dp"
    android:gravity="center_vertical"
    android:text="@string/hello_world"
    android:textSize="14sp"
    android:textStyle="bold" >
</TextView>

<View
    android:layout_width="match_parent"
    android:layout_height="1dp"
    android:background="@android:color/black" />

Xml 3 : listview_group

        <TextView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="?android:attr/listPreferredItemHeight"
android:layout_marginLeft="8dp"
android:gravity="left"
android:paddingLeft="32dp"
android:paddingTop="8dp"
android:text="Test"
android:textSize="14sp"
android:textAlignment="textEnd"
android:textStyle="bold" /> 

グループ クラスを作成します。

 public class Group {

  public String string;
  public final List<String> children = new ArrayList<String>();

  public Group(String string) {
    this.string = string;
  }

} 

展開可能なリスト ビューのアダプター クラス:

     public class MyExpandableListAdapter extends BaseExpandableListAdapter {

  private final SparseArray<Group> groups;
  public LayoutInflater inflater;
  public Activity activity;

  public MyExpandableListAdapter(Activity act, SparseArray<Group> groups) {
    activity = act;
    this.groups = groups;
    inflater = act.getLayoutInflater();
  }

  @Override
  public Object getChild(int groupPosition, int childPosition) {
    return groups.get(groupPosition).children.get(childPosition);
  }

  @Override
  public long getChildId(int groupPosition, int childPosition) {
    return 0;
  }

  @Override
  public View getChildView(int groupPosition, final int childPosition,
      boolean isLastChild, View convertView, ViewGroup parent) {
    final String children = (String) getChild(groupPosition, childPosition);
    TextView text = null;
    if (convertView == null) {
      convertView = inflater.inflate(R.layout.listrow_details, null);
    }
    text = (TextView) convertView.findViewById(R.id.textView1);
    text.setText(children);
    convertView.setOnClickListener(new OnClickListener() {
      @Override
      public void onClick(View v) {
        Toast.makeText(activity, children,
            Toast.LENGTH_SHORT).show();
      }
    });
    return convertView;
  }

  @Override
  public int getChildrenCount(int groupPosition) {
    return groups.get(groupPosition).children.size();
  }

  @Override
  public Object getGroup(int groupPosition) {
    return groups.get(groupPosition);
  }

  @Override
  public int getGroupCount() {
    return groups.size();
  }

  @Override
  public void onGroupCollapsed(int groupPosition) {
    super.onGroupCollapsed(groupPosition);
  }

  @Override
  public void onGroupExpanded(int groupPosition) {
    super.onGroupExpanded(groupPosition);
  }

  @Override
  public long getGroupId(int groupPosition) {
    return 0;
  }

  @Override
  public View getGroupView(int groupPosition, boolean isExpanded,
      View convertView, ViewGroup parent) {

     ExpandableListView eLV = (ExpandableListView) parent;
   eLV.expandGroup(groupPosition);


    if (convertView == null) {
      convertView = inflater.inflate(R.layout.listview_group, null);
    }
    Group group = (Group) getGroup(groupPosition);
    ((TextView) convertView).setText(group.string);
//    ((CheckedTextView) convertView).setChecked(isExpanded);
    return convertView;
  }

  @Override
  public boolean hasStableIds() {
    return false;
  }

  @Override
  public boolean isChildSelectable(int groupPosition, int childPosition) {
    return false;
  }
} 

最後に、メインアクティビティでこれを行います

    public class MainActivity extends Activity {
  // More efficient than HashMap for mapping integers to objects
  SparseArray<Group> groups = new SparseArray<Group>();

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    createData();
    ExpandableListView listView = (ExpandableListView) findViewById(R.id.listView);
    listView.setGroupIndicator(getResources().getDrawable(R.drawable.listbg));
    MyExpandableListAdapter adapter = new MyExpandableListAdapter(this,
        groups);
    listView.setAdapter(adapter);
  }

  public void createData() {
    for (int j = 0; j < 5; j++) {
      Group group = new Group("Test " + j);
      for (int i = 0; i < 5; i++) {
        group.children.add("Sub Item" + i);
      }
      groups.append(j, group);
    }
  }

  }

上記のコードで問題が解決します。ドローアブル xml を使用して、expendablelistview の外観をカスタマイズしました。

ここにlistbg.xmlがあります

   <?xml version="1.0" encoding="utf-8"?>
  <selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_empty="true" android:drawable="@android:color/transparent"/>
<item android:state_expanded="true" android:drawable="@android:color/transparent" />
<item android:drawable="@android:color/transparent" />
 </selector>

ボタンなどを使用して、必要に応じてカスタマイズできます。このコードはあなたの問題を解決するでしょう、平和:)

スクリーンショット

これは、それがどのように見えるかです

于 2013-09-20T05:31:50.677 に答える
0

わかりましたので、ここで何か便利なものを見つけました。それが私が探しているものです。答えは次のとおりです。

  1. 展開可能なリスト ビューを使用する
  2. [展開] ボタンを非表示にする
  3. カスタムにします(その場合、上記のリンクが役立ちます)。@cyberwalker助けてくれてありがとう
于 2013-09-20T06:44:44.660 に答える