ListView in android from 2 dynamic strings arrays

后端 未结 3 870
生来不讨喜
生来不讨喜 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:41

    Create a class like:

    class Link{
        public String name;
        public String link;
    }
    

    You can then create a custom listview which extends 'ArrayAdapter' and override 'getView' as well as 'onItemSelected'. In both of these method you will be able to get the item using the 'position' parameter.

    0 讨论(0)
  • 2021-01-29 08:45

    Use an array adapter to populate the listview from s1, and in the click handler for the listview find the URL for the given list position, and fire off an intent to the browser.

    For an example of using an array adapter see the API demos and in particular list1.

    For an example of setting an activity as an onItemClickListener see https://github.com/nikclayton/android-squeezer/blob/cache-server-data/src/com/danga/squeezer/AlbumsListActivity.java#L88 and https://github.com/nikclayton/android-squeezer/blob/cache-server-data/src/com/danga/squeezer/AlbumsListActivity.java#L288.

    0 讨论(0)
  • 2021-01-29 08:54
    public class MyActivity extends ListActivity{
    
    private ArrayList<String> urls = new ArrayList<String>();
    private ArrayList<String> names = new ArrayList<String>();
    
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    
        urls.add("http://www.google.com");
        names.add("google");
    
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(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);
    }
    

    }

    0 讨论(0)
提交回复
热议问题