0

Donc, je suis un débutant sur Android, et après avoir lu un tutoriel sur ExpandableList en ligne, j'ai fait une liste FAQ dans mon application.Android - possible d'insérer un parent unique et des éléments enfants uniques à ExpandableList en utilisant le tableau xml?

Cependant, je voudrais savoir s'il est possible d'ajouter des éléments parents uniques et uniques à ExpandableList en lisant xml array? Par exemple, avec un tableau de questions et tableau de réponse dans le fichier XML, et la réponse apparaîtra dans le même ordre de liste de questions, comme ceci:

<string-array name="faq_question"> 
 
     <item>Q1</item> 
 
     <item>Q2</item> 
 
    ... 
 
</string-array> 
 

 
<string-array name="faq_answer"> 
 
     <item>A1</item> 
 
     <item>A2</item> 
 
    ... 
 
</string-array>

Actuellement, je dois mettre la réponse chaîne à leur question comme manuellement ce respectif:

ExpandableListDataPump.java

public class ExpandableListDataPump extends AppCompatActivity { 
 

 
    public static HashMap<String, List<String>> getData() { 
 
     HashMap<String, List<String>> expandableListDetail = new HashMap<String, List<String>>(); 
 

 
     String q1 = MainActivity.getContext().getResources().getString(R.string.faq_q1); 
 
     String q2 = MainActivity.getContext().getResources().getString(R.string.faq_q2); 
 
     String q3 = MainActivity.getContext().getResources().getString(R.string.faq_q3); 
 
     String a1 = MainActivity.getContext().getResources().getString(R.string.faq_a1); 
 
     String a2 = MainActivity.getContext().getResources().getString(R.string.faq_a2); 
 
     String a3 = MainActivity.getContext().getResources().getString(R.string.faq_a3); 
 

 
     List<String> qt3 = new ArrayList<String>(); 
 
     qt3.add(a3); 
 
     List<String> qt2 = new ArrayList<String>(); 
 
     qt2.add(a2); 
 
     List<String> qt1 = new ArrayList<String>(); 
 
     qt1.add(a1); 
 

 
     expandableListDetail.put(q1, qt1); 
 
     expandableListDetail.put(q2, qt2); 
 
     expandableListDetail.put(q3, qt3); 
 
     return expandableListDetail; 
 
    } 
 
}

L'adaptateur - ExpandableListAdapter.java

public class ExpandableListAdapter extends AnimatedExpandableListView.AnimatedExpandableListAdapter { 
 

 
    private Context context; 
 
    private List<String> expandableListTitle; 
 
    private HashMap<String, List<String>> expandableListDetail; 
 

 
    public ExpandableListAdapter(Context context, List<String> expandableListTitle, HashMap<String, List<String>> expandableListDetail) { 
 
     this.context = context; 
 
     this.expandableListTitle = expandableListTitle; 
 
     this.expandableListDetail = expandableListDetail; 
 
    } 
 

 
    @Override 
 
    public Object getChild(int listPosition, int expandedListPosition) { 
 
     return this.expandableListDetail.get(this.expandableListTitle.get(listPosition)) 
 
       .get(expandedListPosition); 
 
    } 
 

 
    @Override 
 
    public long getChildId(int listPosition, int expandedListPosition) { 
 
     return expandedListPosition; 
 
    } 
 

 
    @Override 
 
    public View getRealChildView(int listPosition, int expandedListPosition, boolean isLastChild, View convertView, ViewGroup parent) { 
 
     final String expandedListText = (String) getChild(listPosition, expandedListPosition); 
 
     if (convertView == null) { 
 
      LayoutInflater layoutInflater = (LayoutInflater) this.context 
 
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
 
      convertView = layoutInflater.inflate(R.layout.activity_help_faq_content, null); 
 
     } 
 
     TextView expandedListTextView = (TextView) convertView 
 
       .findViewById(R.id.child_list); 
 
     expandedListTextView.setText(expandedListText); 
 
     return convertView; 
 
    } 
 

 
    @Override 
 
    public int getRealChildrenCount(int listPosition) { 
 
     return this.expandableListDetail.get(this.expandableListTitle.get(listPosition)) 
 
       .size(); 
 
    } 
 

 
    @Override 
 
    public Object getGroup(int listPosition) { 
 
     return this.expandableListTitle.get(listPosition); 
 
    } 
 

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

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

 
    @Override 
 
    public View getGroupView(int listPosition, boolean isExpanded, View convertView, ViewGroup parent) { 
 
     String listTitle = (String) getGroup(listPosition); 
 
     if (convertView == null) { 
 
      LayoutInflater layoutInflater = (LayoutInflater) this.context. 
 
        getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
 
      convertView = layoutInflater.inflate(R.layout.activity_help_faq_title, null); 
 
     } 
 
     TextView listTitleTextView = (TextView) convertView 
 
       .findViewById(R.id.header_list); 
 
     listTitleTextView.setText(listTitle); 
 

 
     ViewFlipper flip = (ViewFlipper) convertView.findViewById(R.id.arrow_flipper); 
 
     if (isExpanded) { 
 
      flip.setDisplayedChild(1); 
 
     } else { 
 
      flip.setDisplayedChild(0); 
 
     } 
 
     return convertView; 
 
    } 
 

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

 
    @Override 
 
    public boolean isChildSelectable(int listPosition, int expandedListPosition) { 
 
     return false; 
 
    } 
 
}

L'activité - HelpFaqActivity.java

public class HelpFaqActivity extends AppCompatActivity { 
 

 
    AnimatedExpandableListView expandableListView; 
 
    ExpandableListAdapter expandableListAdapter; 
 
    List<String> expandableListTitle; 
 
    HashMap<String, List<String>> expandableListDetail; 
 

 
    @Override 
 
    protected void onCreate(Bundle savedInstanceState) { 
 
     super.onCreate(savedInstanceState); 
 
     setContentView(R.layout.activity_help_faq); 
 
     setTitle(R.string.help_faq); 
 
     ActionBar actionBar = getSupportActionBar(); 
 
     actionBar.setDisplayHomeAsUpEnabled(true); 
 

 
     expandableListView = (AnimatedExpandableListView) findViewById(R.id.lvExp); 
 
     expandableListDetail = ExpandableListDataPump.getData(); 
 
     expandableListTitle = new ArrayList<String>(expandableListDetail.keySet()); 
 
     expandableListAdapter = new ExpandableListAdapter(this, expandableListTitle, expandableListDetail); 
 
     expandableListView.setAdapter(expandableListAdapter); 
 

 
     expandableListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() { 
 
      @Override 
 
      public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) { 
 

 
       if (expandableListView.isGroupExpanded(groupPosition)) { 
 
        expandableListView.collapseGroupWithAnimation(groupPosition); 
 
        Log.d("MainActivity", "Collapsing"); 
 
       } else { 
 
        expandableListView.expandGroupWithAnimation(groupPosition); 
 
        Log.d("MainActivity", "Expanding"); 
 
       } 
 
       return true; 
 
      } 
 

 
     }); 
 

 
     expandableListView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() { 
 
      @Override 
 
      public void onGroupExpand(int groupPosition) { 
 
      } 
 
     }); 
 

 
     expandableListView.setOnGroupCollapseListener(new ExpandableListView.OnGroupCollapseListener() { 
 
      @Override 
 
      public void onGroupCollapse(int groupPosition) { 
 
      } 
 
     }); 
 

 
     expandableListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() { 
 
      @Override 
 
      public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) { 
 
       return false; 
 
      } 
 
     }); 
 
    } 
 
}

S'il vous plaît laissez-moi savoir s'il y a des moyens de mettre en œuvre cela, ou de meilleurs moyens de faire le même résultat. Merci.

Répondre

0

Envoyer le contexte à votre classe de l'activité principale:

expandableListDetail = ExpandableListDataPump.getData(getApplicationContext()); 

et changer votre ExpandableListDataPump à ceci:

public class ExpandableListDataPump extends AppCompatActivity { 

    public static HashMap<String, List<String>> getData(Context context) { 
     HashMap<String, List<String>> expandableListDetail = new HashMap<String, List<String>>(); 

     String[] questions = context.getResources().getStringArray(R.array.faq_question); 
     String[] answers = context.getResources().getStringArray(R.array.faq_answer); 

     //add error checking in case the length of questions and answers do not match 
     for (int i = 0; i < questions.length; i++) { 
      List<String> answerList = new ArrayList<>(); 
      answerList.add(answers[i]); 
      expandableListDetail.put(questions[i], answerList); 
     } 
     return expandableListDetail; 
    } 
}