ExpendableListView:添加名称,并链接

时间:2017-01-16 13:47:38

标签: android android-fragments url hyperlink expandablelistview

我有一个ExpendableListView,我尝试显示名称,点击后转到url,但我不知道这些方法。

这是我的尝试:

List<String> CompagnieReguliere = new ArrayList<String>();
for (HashMap<String,String> e : lesInfosMaritimes){
    String nom = e.get("nom");
    final String url = e.get("url");
    CompagnieReguliere.add(nom);

    View.setOnItemClickListener buttonListener = new View.setOnItemClickListener(){
        @Override
        public void onClick(View v) {
            Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
            startActivity(browserIntent);
        }
    };

    expListView.setOnItemClickListener(buttonListener);
}

我的所有代码:

public class MainInformation extends Fragment{
ExpandableListAdapter listAdapter;
ExpandableListView expListView;
List<String> listDataHeader;
HashMap<String, List<String>> listDataChild;
private ArrayList<HashMap<String,String>> lesInfosCroisieres,lesInfosMaritimes;
private Infos infosCroisieres, infosMaritimes;
private static boolean connected;

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
    // par la suite il faudra ajouter, en pluys de la liste des compagnies etc.etc.etc.
    // rajouter le click qui redirige sur l'url;
    // et peut etre le logo des compagnies.

    View rootView=inflater.inflate(R.layout.information_layout,container,false);

    // get the listview
    expListView = (ExpandableListView) rootView.findViewById(R.id.lvExp);

    // preparing list data
    prepareListData();

    listAdapter = new ExpandableListAdapter(getActivity(), listDataHeader, listDataChild);

    // setting list adapter
    expListView.setAdapter(listAdapter);



    return rootView;
}
private void prepareListData() {

    try {
        infosCroisieres = new Infos();
        infosMaritimes = new Infos();

        infosCroisieres.execute("http://oleane1.sudcorse.cci.fr/cciacs/dataports/compagnies.csv");
        infosMaritimes.execute("http://oleane1.sudcorse.cci.fr/cciacs/dataports/compagniescroisiere.csv");

        infosCroisieres.get();
        infosMaritimes.get();

        lesInfosCroisieres = infosCroisieres.getArrayHashCSV();
        lesInfosMaritimes = infosMaritimes.getArrayHashCSV();
    }
    catch (Exception e){  Log.i("recup1",e.toString()); }

    listDataHeader = new ArrayList<String>();
    listDataChild = new HashMap<String, List<String>>();

    // Adding child data
    listDataHeader.add("Compagnies Croisières / Cruises Companies");
    listDataHeader.add("Compagnies Maritimes / Regular Companies");
    listDataHeader.add("Contact Port Corse-du-Sud / Contact South Corsica's Ports");



    // Adding child data
    List<String> CompagnieCroisiere = new ArrayList<String>();
    for (HashMap<String,String> e : lesInfosCroisieres){
        Log.i("data1", e.get("nom"));
        Log.i("data1", e.get("url"));
        CompagnieCroisiere.add(e.get("nom"));
    }
  /*  CompagnieCroisiere.setOnItemClickListener(new AdapterView.OnItemClickListener(){
        public void onItemClick(AdapterView<?> parent,View view,int position,long id)
        {
            Toast.makeText(getApplicationContext(),"Click ListItem Number "+position, Toast.LENGTH_LONG).show();}
    });*/

   // Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
   // startActivity(browserIntent);

    List<String> CompagnieReguliere = new ArrayList<String>();
    for (HashMap<String,String> e : lesInfosMaritimes) {
        String nom = e.get("nom");
        final String url = e.get("url");
        CompagnieReguliere.add(nom);

        View.setOnItemClickListener(new OnItemClickListener() {
            // handle a click from the size-sorted list. (this is NOT the checkbox click)
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
                startActivity(browserIntent);
            }
        });

    }



    List<String> PortSudCorse = new ArrayList<String>();
    PortSudCorse.add("Port d'Ajaccio");
    PortSudCorse.add("Port de Bonifacio");
    PortSudCorse.add("Port de Porto-Vecchio");
    PortSudCorse.add("Port de Propriano");


    listDataChild.put(listDataHeader.get(0), CompagnieCroisiere); // Header, Child data
    listDataChild.put(listDataHeader.get(1), CompagnieReguliere);
    listDataChild.put(listDataHeader.get(2), PortSudCorse);

}

public void goToUrl (String url){
    Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
    startActivity(browserIntent);
}
}

我使用Fragment,这可能是个问题吗?

1 个答案:

答案 0 :(得分:0)

使用以下代码,它可能对您有帮助。

View.setOnItemClickListener(new OnItemClickListener() {
    // handle a click from the size-sorted list. (this is NOT the checkbox click)
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
       Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
        startActivity(browserIntent);
    }
});