2012-05-21 4 views
1

Я стараюсь много раз, но почему я не могу этого сделать, я не знаю. После того, как я объявлю Expandable List, выполните следующие действия:Как создать расширяемый вид списка в линейном макете на другом линейном макете

listView =(ExpandableListView)findViewById(R.id.expandableListView1); 

И создаю свой адаптер как есть;

public class MyExpandableListAdapter extends BaseExpandableListAdapter { 
     // Sample data set. children[i] contains the children (String[]) for groups[i]. 
     private String[] groups = { "People Names", "Dog Names", "Cat Names", "Fish Names" }; 
     private String[][] children = { 
       { "Arnold", "Barry", "Chuck", "David" }, 
       { "Ace", "Bandit", "Cha-Cha", "Deuce" }, 
       { "Fluffy", "Snuggles" }, 
       { "Goldy", "Bubbles" } 
     }; 

     public Object getChild(int groupPosition, int childPosition) { 
      return children[groupPosition][childPosition]; 
     } 

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

     public int getChildrenCount(int groupPosition) { 
      return children[groupPosition].length; 
     } 

     public TextView getGenericView() { 
      // Layout parameters for the ExpandableListView 
      AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
        ViewGroup.LayoutParams.FILL_PARENT, 64); 

      TextView textView = new TextView(ExpandableList1.this); 
      textView.setLayoutParams(lp); 
      // Center the text vertically 
      textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT); 
      // Set the text starting position 
      textView.setPadding(36, 0, 0, 0); 
      return textView; 
     } 

     public View getChildView(int groupPosition, int childPosition, boolean isLastChild, 
       View convertView, ViewGroup parent) { 
      TextView textView = getGenericView(); 
      textView.setText(getChild(groupPosition, childPosition).toString()); 
      return textView; 
     } 

     public Object getGroup(int groupPosition) { 
      return groups[groupPosition]; 
     } 

     public int getGroupCount() { 
      return groups.length; 
     } 

     public long getGroupId(int groupPosition) { 
      return groupPosition; 
     } 

     public View getGroupView(int groupPosition, boolean isExpanded, View convertView, 
       ViewGroup parent) { 
      TextView textView = getGenericView(); 
      textView.setText(getGroup(groupPosition).toString()); 
      return textView; 
     } 

     public boolean isChildSelectable(int groupPosition, int childPosition) { 
      return true; 
     } 

     public boolean hasStableIds() { 
      return true; 
     } 

    } 

И затем я установил свой адаптер в listView.

mAdapter = new MyExpandableListAdapter(); 
     setListAdapter(mAdapter); 

Но есть так много проблем, которые я не могу создать; İf u помогите мне, я буду так счастлив. Спасибо.

+1

Добавить здесь журнал ошибок. – Sajmon

ответ

0

Ok. Спасибо, ребята, я справляюсь со своей проблемой, все проблемы в том, что не расширяет ExpandableListActivity, просто Activity это мой код. Теперь я пытаюсь научиться фокусировать группу, когда нажимаю кнопку. Вот код.

public class expandXml extends Activity{ 

ExpandableListView listView; 
ExpandableListAdapter adapter; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.deneme2); 


    listView =(ExpandableListView)findViewById(R.id.expandableListView1); 
    listView.setFocusable(true); 
    adapter = new ExpandableListAdapter() { 


     private String[] groups = { "People Names", "Dog Names", "Cat Names", "Fish Names" }; 
     private String[][] children = { 
       { "Arnold", "Barry", "Chuck", "David" }, 
       { "Ace", "Bandit", "Cha-Cha", "Deuce" }, 
       { "Fluffy", "Snuggles" }, 
       { "Goldy", "Bubbles" } 
     }; 
     public void unregisterDataSetObserver(DataSetObserver observer) { 
      // TODO Auto-generated method stub 

     } 

     public void registerDataSetObserver(DataSetObserver observer) { 
      // TODO Auto-generated method stub 

     } 

     public void onGroupExpanded(int groupPosition) { 
      // TODO Auto-generated method stub 

     } 

     public void onGroupCollapsed(int groupPosition) { 
      // TODO Auto-generated method stub 

     } 

     public boolean isEmpty() { 
      // TODO Auto-generated method stub 
      return false; 
     } 

     public boolean isChildSelectable(int groupPosition, int childPosition) { 
      // TODO Auto-generated method stub 
      return true; 
     } 

     public boolean hasStableIds() { 
      // TODO Auto-generated method stub 
      return true; 
     } 

     public View getGroupView(int groupPosition, boolean isExpanded, 
       View convertView, ViewGroup parent) { 
      TextView textView = getGenericView(); 
       textView.setText(getGroup(groupPosition).toString()); 
       return textView; 
     } 

     public long getGroupId(int groupPosition) { 
      // TODO Auto-generated method stub 
      return groupPosition; 
     } 

     public int getGroupCount() { 
      // TODO Auto-generated method stub 
      return groups.length; 
     } 

     public Object getGroup(int groupPosition) { 
      // TODO Auto-generated method stub 
      return groups[groupPosition]; 
     } 

     public long getCombinedGroupId(long groupId) { 
      // TODO Auto-generated method stub 
      return 0; 
     } 

     public long getCombinedChildId(long groupId, long childId) { 
      // TODO Auto-generated method stub 
      return 0; 
     } 

     public int getChildrenCount(int groupPosition) { 
      // TODO Auto-generated method stub 
      return children[groupPosition].length; 
     } 

     public View getChildView(int groupPosition, int childPosition, 
       boolean isLastChild, View convertView, ViewGroup parent) { 
      TextView textView = getGenericView(); 
       textView.setText(getChild(groupPosition, childPosition).toString()); 
       return textView; 
     } 

     public long getChildId(int groupPosition, int childPosition) { 
      // TODO Auto-generated method stub 
      return childPosition; 
     } 

     public Object getChild(int groupPosition, int childPosition) { 
      // TODO Auto-generated method stub 
      return children[groupPosition][childPosition]; 
     } 

     public boolean areAllItemsEnabled() { 
      // TODO Auto-generated method stub 
      return false; 
     } 

     public TextView getGenericView() { 
      // Layout parameters for the ExpandableListView 
      AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
        ViewGroup.LayoutParams.FILL_PARENT, 64); 

      TextView textView = new TextView(expandXml.this); 
      textView.setLayoutParams(lp); 
      // Center the text vertically 
      textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT); 
      // Set the text starting position 
      textView.setPadding(36, 0, 0, 0); 

      return textView; 
     } 

    }; 
    listView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() { 

     public boolean onChildClick(ExpandableListView parent, View v, 
       int groupPosition, int childPosition, long id) { 

      Object x =adapter.getChild(groupPosition, childPosition); 

      //Object o = this.getExpandableListAdapter().getChild(groupPosition, childPosition); 
      String key =x.toString(); 
      Toast.makeText(expandXml.this,"Secildi "+key ,Toast.LENGTH_LONG).show();  
      return true; 
     } 
    }); 
    Button butex = (Button)findViewById(R.id.button1); 
    butex.setOnClickListener(expandgroup); 



    listView.setAdapter(adapter); 

} 
private OnClickListener expandgroup = new OnClickListener() { 

    public void onClick(View v) { 
     // TODO Auto-generated method stub 
     listView.expandGroup(3); 



    } 
}; } 
0

Вместо того чтобы устанавливать этот список адаптер, как вы:

setListAdapter(mAdapter); 

Вы должны установить его так:

listView.setAdapter(mAdapter); 
Смежные вопросы