0

so i've got this piece of code and i want it to output attr1 and attr2 in the listview, but the current error i get is: cannot resolve constructor. Here is the piece of code:

 private String[][] content = {
        {"attr1", "url1"},
        {"atrr2", "url2"}
};

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_activity);
    ListView lv = (ListView) findViewById(R.id.lv);
    for(int i = 0; i < content.length; i++) {
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, content[i][0]);
        lv.setAdapter(adapter);
    }


}

Hopefully somebody could help me, thanks in advance (sorry for the bad english)

6
  • If you want just attr1, attr2 to be displayed, just use String[] content={"attr1","attr2"}; First item will be attr1 and second one attr2 Commented Feb 14, 2015 at 19:49
  • No. I still want to use the url's when the item is clicked Commented Feb 14, 2015 at 19:54
  • How would android set two texts in a single textView? Use android.R.layout.simple_list_item_2 instead of android.R.layout.simple_list_item_1 Commented Feb 14, 2015 at 19:58
  • Why don't you create a custom class for content and use that to populate the ListView? Commented Feb 14, 2015 at 19:58
  • @Apurva I tried that but I still get the same error. Commented Feb 14, 2015 at 20:01

1 Answer 1

4

Move the creation of the list outside your for loop like so:

    ListView lv = (ListView) findViewById(R.id.lv);
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1);
    for(int i = 0; i < content.length; i++) {
        adapter.add(content[i][0]);
    }
    lv.setAdapter(adapter);

You can get the index of the clicked item and use that information to access your array again and get the second piece of information

Sign up to request clarification or add additional context in comments.

1 Comment

Move lv.setAdapter(adapter); out of the loop

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.