Add a Basic ListView
1. Add a ListView to XML
2. Set android:id="@+id/myListView" in the ListView.
3. Now open the MainActivity.java file and add the below import to the imports section.
import android.widget.ListView;
4. Add the line: ListView myListView between the public class and the onCreate method.
Defining the myListView variable here will make this available throughout the whole class.
public class MainActivity extends ListActivity {
ListView myListView;
public void onCreate(Bundle savedInstanceState) {
5. Add the bolded code below in the OnCreate method after setContentView and before the end of the method }. This code will link your editText in your main.xml file to the editText in code.
myListView = (ListView) findViewById(R.id.myListView);
6. Compile and run!
Popular Posts
- android:ellipsize - Add an ellipsis(...) in a TextView
- android:textColorHighlight - Set the Color of Highlighted Text in an EditText
- Perform an Action after Enter keypress on EditText
- How to Register an Activity that Responds to NFC tags for URI's that point to a URL
- How to Import a Library
- android:alpha - Set the Opacity/Transparency of Text in TextView/EditText
- Save LogCat to a Text File
- How to Set Permission for I/O Operations over NFC
- android:drawablePadding - Add Padding to a Drawable in a TextView, in XML
- String.valueOf - How to Convert a Double to a String