简体   繁体   中英

how to use separate arraylist for listview and AlertDialog using OnListItemClick?

i Have a two Arraylist to use one Listview.... My question when i click row in listview, second arraylist data want execute on AlertDialog popup box My Main class

public class MainActivity extends ListActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    setListAdapter(new ArrayAdapter<String>(this, 
    android.R.layout.simple_list_item_1,
    getResources().getStringArray(R.array.bus_no)));
  }
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    // TODO Auto-generated method stub
    super.onListItemClick(l, v, position, id);

}}

** Two Array List bus_no and bus_routes* Waht i want is when i click 100 in listview first routes want popup in dialog box. i know insight OnListItemClick() method want implement some code i dont know how to implement code how to execute data from arraylist for my dialogbox

 <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <string-array name="bus_no">
            <item>100</item>
            <item>101</item>
            <item>102</item>
            <item>103</item>
            <item>104</item>
            <item>107</item>
            <item>112</item>
        </string-array>
    </resources>

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <string-array name="bus_routes">
            <item>Pettah to Panadura</item>
            <item>Pettah to Moratuwa</item>
            <item>Kotahena to Moratuwa</item>
            <item>Fort to Borella</item>
            <item>Bambalapitya to Wattala</item>
            <item>Fort to Elakanda</item>
            <item>chilow to ham</item>
        </string-array>
    </resources>

Use AlertDialog.Builder.setItems for showing bus_routes in AlertDialog :

    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
                    MainActivity.this);
    String[] routes=getResources().getStringArray(R.array.bus_routes);
    String[] seleted_routes={routes[position]};
alertDialogBuilder.setItems(seleted_routes,
                                     new DialogInterface.OnClickListener(){
            public void onClick(DialogInterface dialog, int which) {
                ///.. get clicked item here          
            }   
        });
    AlertDialog alertDialog = alertDialogBuilder.create();          
    alertDialog.show();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM