How to Add a ListView using ListActivity
1. Add a ListView to XML file.2. Set the id to android:id="@android:id/list" in the ListView.
3. Set android:drawSelectorOnTop = "fasle" in the ListView.
4. Now open the MainActivity.java file and add the below import to the imports section.
import android.widget.ListView;
import android.app.ListActivity;
5. You will need to extend the ListActivity in your MainActivity.java file. To do this
change this:
public class MainActivity extends Activity{
to this:
public class MainActivity extends ListActivity{
6. Add the line: EditText editText between the public class and the onCreate method.
Defining the editText variable here will make this available throughout the whole class.
public class MainActivity extends Activity {
EditText editText;
public void onCreate(Bundle savedInstanceState) {
7. 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.
editText = (EditText) findViewById(R.id.editText);
8. Run your app to test it. It should display an EditText.
9. Set the default text for the EditText by adding to the main.xml file EditText section, anywhere between the <EditText and the /> tags.
<EditText
...
android:text="your text here"
...
/>
10. Compile and run!