2015-05-25 34 views
0

我正在創建一個具有可擴展列表視圖的應用程序我能夠獲得可展開列表視圖,但是我的孩子點擊不起作用。我試圖在可展開列表視圖中單擊一個孩子時顯示Toast消息。請幫助我,讓我知道我在做什麼錯誤。提前致謝。onChildClickListener無法在Expandable列表視圖中工作

My Activity code: 

    import android.content.Context; 
    import android.database.Cursor; 
    import android.graphics.Color; 
    import android.os.Bundle; 
    import android.support.v7.app.AppCompatActivity; 
    import android.support.v7.widget.Toolbar; 
    import android.view.Menu; 
    import android.view.MenuItem; 
    import android.view.View; 
    import android.widget.ExpandableListAdapter; 
    import android.widget.ExpandableListView; 
    import android.widget.SimpleCursorTreeAdapter; 
    import android.widget.Toast; 

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


    public class AdminActivity extends AppCompatActivity { 

     Toolbar toolbar; 
     ExpandableListAdapter listAdapter; 
     List<String> titleText; 
     SQLiteDataBaseAdapter db; 

     HashMap<String, List<String>> listDataChild; 

     ExpandableListView login, android, ios, testing, java, dotNet, os, hr, others; 

     @Override 
     protected void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.activity_admin); 

      toolbar = (Toolbar) findViewById(R.id.appBar); 
      toolbar.setTitle(" Admin Screen"); 
      toolbar.setTitleTextColor(Color.WHITE); 

      login = (ExpandableListView) findViewById(R.id.expandableListViewLogin); 

      titleText = new ArrayList<>(); 
      titleText.add("User Id Authentication"); 
      titleText.add("Android Posts Authentication"); 
      titleText.add("iOS Posts Authentication"); 
      titleText.add("Testing Posts Authentication"); 
      titleText.add("Java Posts Authentication"); 
      titleText.add("Dot Net Posts Authentication"); 
      titleText.add("OS Posts Authentication"); 
      titleText.add("HR Posts Authentication"); 
      titleText.add("Others Posts Authentication"); 






      SQLiteDataBaseAdapter db = new SQLiteDataBaseAdapter(this); 
      List<String> childDataLogin = db.getLoginList(); 
      List<String> childDataAndroid = db.getAndroidList(); 
      List<String> childDataIos = db.getIosList(); 
      List<String> childDataTesting = db.getTestingList(); 
      List<String> childDataJava = db.getJavaList(); 
      List<String> childDataDotNet = db.getDotNetList(); 
      List<String> childDataOs = db.getOSList(); 
      List<String> childDataHr = db.getHRList(); 
      List<String> childDataOthers = db.getOthersList(); 

      listDataChild = new HashMap<>(); 
      listDataChild.put(titleText.get(0), childDataLogin); // Header, Child data 
      listDataChild.put(titleText.get(1), childDataAndroid); 
      listDataChild.put(titleText.get(2), childDataIos); 
      listDataChild.put(titleText.get(3), childDataTesting); 
      listDataChild.put(titleText.get(4), childDataJava); 
      listDataChild.put(titleText.get(5), childDataDotNet); 
      listDataChild.put(titleText.get(6), childDataOs); 
      listDataChild.put(titleText.get(7), childDataHr); 
      listDataChild.put(titleText.get(8), childDataOthers); 



      listAdapter = new MyExpandableListAdapter(this, titleText, listDataChild); 
      login.setAdapter(listAdapter); 

      // Listview on child click listener 
      login.setOnChildClickListener(new ExpandableListView.OnChildClickListener() { 

       @Override 
       public boolean onChildClick(ExpandableListView parent, View v, 
              int groupPosition, int childPosition, long id) { 
        Toast.makeText(
          getApplicationContext(), 
          titleText.get(groupPosition) 
            + " : " 
            + listDataChild.get(
            titleText.get(groupPosition)).get(
            childPosition), Toast.LENGTH_SHORT) 
          .show(); 
        return true; 
       } 
      }); 

     } 

     @Override 
     public boolean onCreateOptionsMenu(Menu menu) { 
      // Inflate the menu; this adds items to the action bar if it is present. 
      getMenuInflater().inflate(R.menu.menu_admin, menu); 
      return true; 
     } 

     @Override 
     public boolean onOptionsItemSelected(MenuItem item) { 
      // Handle action bar item clicks here. The action bar will 
      // automatically handle clicks on the Home/Up button, so long 
      // as you specify a parent activity in AndroidManifest.xml. 
      int id = item.getItemId(); 

      //noinspection SimplifiableIfStatement 
      if (id == R.id.action_settings) { 
       return true; 
      } 

      return super.onOptionsItemSelected(item); 
     } 



    } 

Layout of child: 



<?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="vertical"> 

     <TextView 
      android:id="@+id/lblListItem" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:textSize="17sp" 
      android:paddingTop="5dp" 
      android:paddingBottom="5dp" 
      android:paddingLeft="?                    android:attr/expandableListPreferredChildPaddingLeft" /> 

    </LinearLayout> 

Adapter Code: 



    public class MyExpandableListAdapter extends BaseExpandableListAdapter { 
      private Context _context; 
      private List<String> _listDataHeader; // header titles 
      // child data in format of header title, child title 
      private HashMap<String, List<String>> _listDataChild; 

      public MyExpandableListAdapter(Context context, List<String>   listDataHeader, HashMap<String, List<String>> listChildData) { 
       this._context = context; 
       this._listDataHeader = listDataHeader; 
       this._listDataChild = listChildData; 
      } 

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

      @Override 
      public int getChildrenCount(int groupPosition) { 
       return  this._listDataChild.get(this._listDataHeader.get(groupPosition)) 
         .size(); 
      } 

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

      @Override 
      public Object getChild(int groupPosition, int childPosititon) { 
       return this._listDataChild.get(this._listDataHeader.get(groupPosition)) 
         .get(childPosititon); 
      } 

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

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

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

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


       String headerTitle = (String) getGroup(groupPosition); 
       if (convertView == null) { 
        LayoutInflater infalInflater = (LayoutInflater) this._context 
          .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
        convertView = infalInflater.inflate(R.layout.list_group, null); 
       } 

       TextView lblListHeader = (TextView) convertView 
         .findViewById(R.id.expandableListHeader); 
       lblListHeader.setTypeface(null, Typeface.BOLD); 
       lblListHeader.setText(headerTitle); 

       return convertView; 
      } 

      @Override 
      public View getChildView(int groupPosition, final int childPosition, 
            boolean isLastChild, View convertView, ViewGroup parent) { 

       final String childText = (String) getChild(groupPosition, childPosition); 

       if (convertView == null) { 
        LayoutInflater infalInflater = (LayoutInflater) this._context 
          .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
        convertView = infalInflater.inflate(R.layout.list_item, null); 
       } 

       TextView txtListChild = (TextView) convertView 
         .findViewById(R.id.lblListItem); 

       txtListChild.setText(childText); 
       return convertView; 

      } 

      @Override 
      public boolean isChildSelectable(int i, int i1) { 
       return false; 
      } 
     } 
+0

顯示適配器代碼。 –

+0

發佈孩子列表項目的佈局 – Kartheek

+0

@NoName加入請檢查。 – Keshav1234

回答

1

一定要重寫您的擴展列表適配器的isChildSelectable方法並返回true,就像這樣:

public class MyExpandableListAdapter extends BaseExpandableListAdapter { 
    @Override 
    public boolean isChildSelectable(int groupPosition, int childPosition) { 
     return true; 
    } 
    ... 
} 

或者

android:focusable="false" 

expandlist_child_item.xmlCheckBox區間內文件。

我希望這有助於某人。

+0

更改適配器方法的工作非常感謝。 – Keshav1234

+0

歡迎你 – Tufan

1

,則返回true在isChildSelectable 方法替換

@Override 
    public boolean isChildSelectable(int i, int i1) { 
     return false; 
    } 

@Override 
    public boolean isChildSelectable(int i, int i1) { 
     return true; 
    } 
+0

這工作謝謝。 – Keshav1234

相關問題