user3520734
user3520734

Reputation:

android 2d array to listview

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)

Upvotes: 0

Views: 2273

Answers (1)

BeLEEver
BeLEEver

Reputation: 289

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

Upvotes: 4

Related Questions