ListView in android from 2 dynamic strings arrays

后端 未结 3 911
生来不讨喜
生来不讨喜 2021-01-29 08:15

I have two string arrays, here s1[] contains a list of names and s2[] contains URL\'s associated with the respective name, now i need to populate ListView, with the names and on

3条回答
  •  北荒
    北荒 (楼主)
    2021-01-29 08:54

    public class MyActivity extends ListActivity{
    
    private ArrayList urls = new ArrayList();
    private ArrayList names = new ArrayList();
    
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    
        urls.add("http://www.google.com");
        names.add("google");
    
        ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, android.R.id.text1, names);
        setListAdapter(adapter);
    
    }
    
    protected void onListItemClick (ListView l, View v, int position, long id){
        String url = urls.get(position);
    
        Intent i = new Intent(Intent.ACTION_VIEW);
        i.setData(Uri.parse(url));
        startActivity(i);
    }
    

    }

提交回复
热议问题