当单击可展开列表视图中的子项时,如何在另一个活动中打开列表视图



我正在为我的学校创建一个应用程序。它应该包含该机构提供的每门课程的课堂讲稿。现在机构中有三个不同的学校,每个学校至少有四个部门,所以我的主要活动有一个可扩展的列表视图,当单击任何学校时,它显示不同的部门。

我希望能够在单击特定子项目(部门)时在列表视图中打开一个新的活动,其中填充了该部门提供的课程。另外,当新的课程列表活动打开时,用户应该能够在另一个活动中查看该课程的课堂笔记,而无需为每个部门和课程创建活动和布局。

这是我的主要活动

public class MainActivity extends Activity {
    ExpandableListAdapter listAdapter;
    ExpandableListView expListView;
    List<String> listDataHeader;
    HashMap<String, List<String>> listDataChild;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // get the listview
        expListView = (ExpandableListView) findViewById(R.id.lvExp);
        // preparing list data
        prepareListData();
        listAdapter = new ExpandableListAdapter(this, listDataHeader, listDataChild);
        // setting list adapter
        expListView.setAdapter(listAdapter);
        // Listview Group click listener
        expListView.setOnGroupClickListener(new OnGroupClickListener() {
            @Override
            public boolean onGroupClick(ExpandableListView parent, View v,
                    int groupPosition, long id) {
                // Toast.makeText(getApplicationContext(),
                // "Group Clicked " + listDataHeader.get(groupPosition),
                // Toast.LENGTH_SHORT).show();
                return false;
            }
        });
        // Listview Group expanded listener
        expListView.setOnGroupExpandListener(new OnGroupExpandListener() {
            @Override
            public void onGroupExpand(int groupPosition) {
                Toast.makeText(getApplicationContext(),
                        listDataHeader.get(groupPosition) + " Expanded",
                        Toast.LENGTH_SHORT).show();
            }
        });
        // Listview Group collasped listener
        expListView.setOnGroupCollapseListener(new OnGroupCollapseListener() {
            @Override
            public void onGroupCollapse(int groupPosition) {
                Toast.makeText(getApplicationContext(),
                        listDataHeader.get(groupPosition) + " Collapsed",
                        Toast.LENGTH_SHORT).show();
            }
        });
        // Listview on child click listener
        expListView.setOnChildClickListener(new OnChildClickListener() {
            @Override
            public boolean onChildClick(ExpandableListView parent, View v,
                    int groupPosition, int childPosition, long id) {
                // TODO Auto-generated method stub
                Toast.makeText(
                        getApplicationContext(),
                        listDataHeader.get(groupPosition)
                                + " : "
                                + listDataChild.get(
                                        listDataHeader.get(groupPosition)).get(
                                        childPosition), Toast.LENGTH_SHORT)
                        .show();
                return false;
            }
        });
    }
    /*
     * Preparing the list data
     */
    private void prepareListData() {
        listDataHeader = new ArrayList<String>();
        listDataChild = new HashMap<String, List<String>>();
        // Adding child data
        listDataHeader.add("School Of Applied Science");
        listDataHeader.add("School Of Business And Management Studies");
        listDataHeader.add("School Of Engineering");
        // Adding child data
        List<String> schoolOfAppliedScience = new ArrayList<String>();
        schoolOfAppliedScience.add("Computer Science");
        schoolOfAppliedScience.add("Statistics");
        schoolOfAppliedScience.add("Science Laboratory Technology");
        schoolOfAppliedScience.add("Hotel Catering And Institutional Management");
        schoolOfAppliedScience.add("Fashion Design And Textiles");
        List<String> BAMS = new ArrayList<String>();
        BAMS.add("Secretaryship And Management Studies");
        BAMS.add("Marketing");
        BAMS.add("Accounting");
        BAMS.add("Purchasing And Supply");
        List<String> schOfEngineering = new ArrayList<String>();
        schOfEngineering.add("Electrical Engineering");
        schOfEngineering.add("Automotive Engineering");
        schOfEngineering.add("Building Technology");
        schOfEngineering.add("Civil Engineering");
        listDataChild.put(listDataHeader.get(0), schoolOfAppliedScience); // Header, Child data
        listDataChild.put(listDataHeader.get(1), BAMS);
        listDataChild.put(listDataHeader.get(2), schOfEngineering);
    }
}

这是我的适配器类

public class ExpandableListAdapter 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 ExpandableListAdapter(Context context, List<String> listDataHeader,
            HashMap<String, List<String>> listChildData) {
        this._context = context;
        this._listDataHeader = listDataHeader;
        this._listDataChild = listChildData;
    }
    @Override
    public Object getChild(int groupPosition, int childPosititon) {
        return this._listDataChild.get(this._listDataHeader.get(groupPosition))
                .get(childPosititon);
    }
    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }
    @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 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 int getGroupCount() {
        return this._listDataHeader.size();
    }
    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }
    @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.lblListHeader);
        lblListHeader.setTypeface(null, Typeface.BOLD);
        lblListHeader.setText(headerTitle);
        return convertView;
    }
    @Override
    public boolean hasStableIds() {
        return false;
    }
    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }
}
    // Listview on child click listener
    expListView.setOnChildClickListener(new OnChildClickListener() {
        @Override
        public boolean onChildClick(ExpandableListView parent, View v,
                int groupPosition, int childPosition, long id) {
            // TODO Auto-generated method stub
if (groupPosition == 0) {
      if (childPosition == 0)
         {
            Intent c = new Intent(getApplicationContext(),ComputerScience.class);
            startActivity(c);
         }
      if (childPosition == 1) {
            Intent c = new Intent(getApplicationContext(), Statistics.class);
            startActivity(c);
        }
        if (childPosition == 2) {
            Intent c = new Intent(getApplicationContext(), ScienceLaboratoryTechnology.class);
            startActivity(c);
        }
        if (childPosition == 3) {
            Intent c = new Intent(getApplicationContext(),HotelCateringAndInstitutionalManagement.class);
            startActivity(c);
        }
        if (childPosition == 4) {
            Intent c = new Intent(getApplicationContext(),FashionDesignAndTextiles.class);
            startActivity(c);
        }
    }
   if (groupPosition == 1) {
      if (childPosition == 0)
         {
            Intent c = new Intent(getApplicationContext(),SecretaryshipAndManagementStudies.class);
            startActivity(c);
         }
      if (childPosition == 1) {
            Intent c = new Intent(getApplicationContext(), Marketing.class);
            startActivity(c);
        }
        if (childPosition == 2) {
            Intent c = new Intent(getApplicationContext(), PurchasingAndSupply.class);
            startActivity(c);
        }
        if (childPosition == 3) {
            Intent c = new Intent(getApplicationContext(),HotelCateringAndInstitutionalManagement.class);
            startActivity(c);
        }
    }
    if (groupPosition == 2) {
        if (childPosition == 0) {
            Intent c = new Intent(getApplicationContext(), AutomotiveEngineering.class);
            startActivity(c);
        }
        if (childPosition == 1) {
            Intent c = new Intent(getApplicationContext(), BuildingTechnology.class);
            startActivity(c);
        }
        if (childPosition == 2) {
            Intent c = new Intent(getApplicationContext(), CivilEngineering.class);
            startActivity(c);
        }
    }
    return false;
}
 });

尝试android.widget.AdapterView.OnItemClickListener。

   expListView.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long arg3) {
            StartIntent();
        }
    });

请看这里的开发者文档


   mExpandableList.setOnChildClickListener(new OnChildClickListener() {
       @Override
       public boolean onChildClick(ExpandableListView parent, View v,int groupPosition, int childPosition, long id) {
          /*Additional info: You can use the view variable(v) here to get the params from the view you are clicking just in case you want to pass any data of that view to new activity(use casting to casting point to a particular view type). then pass that data as extra along with intent */
          Intent intent=new Intent(CurrentClass.this,NewActivity.class)
          startActivity(intent);     
          return true;
        }
    });

注意:不要忘记在manifest

中声明NewActivity

您只需要修改getChildView方法,像这样添加OnClickListener:

     @Override
         public View getChildView(final 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);
         //-----------------------------------------------------------------------------------
         txtListChild.setOnClickListener(new View.OnClickListener() {    
             @Override
             public void onClick(View view) {
//put your groupPosition and childPosition to the intent and start the activity
                 startActivity(YOUR INTENT);
             }    
         }//-----------------------------------------------------------------------------------

         return convertView;
         }

或者,您可以为children layout:

设置tonclicklistener
convertView.setOnClickListener(...)
public class MyExpandableAdapter extends BaseExpandableListAdapter
{
    private Activity activity;
    private ArrayList<Object> childtems;
    private LayoutInflater inflater;
    private ArrayList<String> parentItems, child;
    private Context context;

    // constructor
    public MyExpandableAdapter(ArrayList<String> parents, ArrayList<Object> childern)
    {
        this.parentItems = parents;
        this.childtems = childern;

    }
    public void setInflater(LayoutInflater inflater, Activity activity)
    {
        this.inflater = inflater;
        this.activity = activity;
    }
    // method getChildView is called automatically for each child view.
    //  Implement this method as per your requirement
    @Override
    public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent)
    {
        child = (ArrayList<String>) childtems.get(groupPosition);
        TextView textView = null;
        ImageView img = null;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.child_view, null);
        }
        // get the textView reference and set the value
        textView = (TextView) convertView.findViewById(R.id.textViewChild);
        textView.setText(child.get(childPosition));
        img = (ImageView) convertView.findViewById(R.id.childImage);

            if (childPosition == 0 && groupPosition==0) {
                img.setImageResource(R.drawable.slika1);
            }
            if (childPosition == 1 && groupPosition==0) {
                img.setImageResource(R.drawable.slika2);
            }
            if (childPosition == 2 && groupPosition==0) {
                img.setImageResource(R.drawable.slika1);
            }
            if (childPosition == 3 && groupPosition==0) {
                img.setImageResource(R.drawable.slika2);
            }
            if (childPosition == 4 && groupPosition==0) {
                img.setImageResource(R.drawable.slika1);
            }
            if (childPosition == 5 && groupPosition==0) {
                img.setImageResource(R.drawable.slika2);
            }
            if (childPosition == 6 && groupPosition==0) {
                img.setImageResource(R.drawable.slika1);
            }
            if (childPosition == 7 && groupPosition==0) {
                img.setImageResource(R.drawable.slika2);
            }
            if (childPosition == 8 && groupPosition==0) {
                img.setImageResource(R.drawable.slika1);
            }
            if (childPosition == 0 && groupPosition==1) {
                img.setImageResource(R.drawable.slika2);
            }
            if (childPosition == 1 && groupPosition==1) {
                img.setImageResource(R.drawable.slika1);
            }
            if (childPosition == 2 && groupPosition==1) {
                img.setImageResource(R.drawable.slika2);
            }
            if (childPosition == 3 && groupPosition==1) {
                img.setImageResource(R.drawable.slika1);
            }
            if (childPosition == 0 && groupPosition==2) {
                img.setImageResource(R.drawable.slika2);
            }
            if (childPosition == 1 && groupPosition==2) {
                img.setImageResource(R.drawable.slika1);
            }
            if (childPosition == 2 && groupPosition==2 ) {
                img.setImageResource(R.drawable.slika2);
            }
            if (childPosition == 3 && groupPosition==2) {
                img.setImageResource(R.drawable.slika1);
            }
            if (childPosition == 0 && groupPosition==3) {
                img.setImageResource(R.drawable.slika2);
            }
            if (childPosition == 1 && groupPosition==3) {
                img.setImageResource(R.drawable.slika1);
            }
            if (childPosition == 2 && groupPosition==3) {
                img.setImageResource(R.drawable.slika2);
            }
            if (childPosition == 3 && groupPosition==3) {
                img.setImageResource(R.drawable.slika1);
            }


        // set the ClickListener to handle the click event on child item
       //convertView.setOnClickListener(new View.OnClickListener() {
        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(activity, child.get(childPosition),
                        Toast.LENGTH_SHORT).show();
            }
        });
        return convertView;
    }
    // method getGroupView is called automatically for each parent item
    // Implement this method as per your requirement
    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent)
    {
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.parent_view, null);
        }
        ((CheckedTextView) convertView).setText(parentItems.get(groupPosition));
        ((CheckedTextView) convertView).setChecked(isExpanded);
        return convertView;
    }

    @Override
    public Object getChild(int groupPosition, int childPosition)
    {
        return null;
    }
    @Override
    public long getChildId(int groupPosition, int childPosition)
    {
        return 0;
    }
    @Override
    public int getChildrenCount(int groupPosition)
    {
        return ((ArrayList<String>) childtems.get(groupPosition)).size();
    }
    @Override
    public Object getGroup(int groupPosition)
    {
        return null;
    }
    @Override
    public int getGroupCount()
    {
        return parentItems.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 boolean hasStableIds()
    {
        return false;
    }
    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition)
    {
        return false;
    }
}

相关内容

  • 没有找到相关文章

最新更新