Android ListView understanding

I have a question about ListView and how to use it. My problem is that my View list is only part of the view, and I'm not sure how to do it.

public class MainActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    ListView myListView = (ListView) findViewById(R.id.ListView01);
    String[] strings = new String[]{"Test1","Test2"};
    ArrayAdapter<String> myArrayAdapter= new ArrayAdapter<String>(this, R.id.ListView01,strings);
    myListView.setAdapter(myArrayAdapter);

I think the problem is "this" in myArrayAdapter !?

+3
source share
1 answer

The layout resource identifier that you must pass to the ArrayAdapter is the layout that is used to render each item in the list, not the layout for the list itself. Android provides some layout resources for common cases. Try using:

ArrayAdapter<String> myArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, strings);
+7
source

Source: https://habr.com/ru/post/1716358/


All Articles