Difference between revisions of "Android Studio: ListView lagi"

From OnnoWiki
Jump to navigation Jump to search
 
Line 4: Line 4:
  
  
  package com.example.ListDisplay;
+
  package com.example.listdisplay;
 +
 +
import androidx.appcompat.app.AppCompatActivity;
 
   
 
   
 
  import android.os.Bundle;
 
  import android.os.Bundle;
Line 11: Line 13:
 
  import android.widget.ArrayAdapter;
 
  import android.widget.ArrayAdapter;
 
  import android.widget.ListView;
 
  import android.widget.ListView;
 +
 +
public class MainActivity extends AppCompatActivity {
 +
 +
    String[] mobileArray = {"Android","IPhone","WindowsMobile","Blackberry",
 +
            "WebOS","Ubuntu","Windows7","Max OS X"};
 
   
 
   
public class ListDisplay extends Activity {
+
    @Override
    // Array of strings...
+
    protected void onCreate(Bundle savedInstanceState) {
    String[] mobileArray = {"Android","IPhone","WindowsMobile","Blackberry",
+
        super.onCreate(savedInstanceState);
      "WebOS","Ubuntu","Windows7","Max OS X"};
+
       
   
+
setContentView(R.layout.activity_main);
    @Override
+
    protected void onCreate(Bundle savedInstanceState) {
+
        ArrayAdapter adapter = new ArrayAdapter<String>(this,
      super.onCreate(savedInstanceState);
+
                R.layout.activity_listview, mobileArray);
      setContentView(R.layout.activity_main);
+
     
+
        ListView listView = (ListView) findViewById(R.id.mobile_list);
      ArrayAdapter adapter = new ArrayAdapter<String>(this,  
+
        listView.setAdapter(adapter);
          R.layout.activity_listview, mobileArray);
+
    }
     
 
      ListView listView = (ListView) findViewById(R.id.mobile_list);
 
      listView.setAdapter(adapter);
 
    }
 
 
  }
 
  }
 
  
  
 
==activity_main.xml==
 
==activity_main.xml==
  
  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
+
  <?xml version="1.0" encoding="utf-8"?>
     xmlns:tools="http://schemas.android.com/tools"
+
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
+
   
    android:layout_height="match_parent"
+
xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
+
      
    tools:context=".ListActivity" >
+
xmlns:tools="http://schemas.android.com/tools"
 +
    android:layout_width="match_parent"
 +
    android:layout_height="match_parent"
 +
    tools:context=".MainActivity">
 +
 +
    <ListView
 +
        android:id="@+id/mobile_list"
 +
        android:layout_width="match_parent"
 +
        android:layout_height="wrap_content" >
 +
    </ListView>  
 
   
 
   
    <ListView
+
  </androidx.constraintlayout.widget.ConstraintLayout>
      android:id="@+id/mobile_list"
 
      android:layout_width="match_parent"
 
      android:layout_height="wrap_content" >
 
    </ListView>
 
 
 
  </LinearLayout>
 
 
 
  
 
==strings.xml==
 
==strings.xml==
  
<?xml version="1.0" encoding="utf-8"?>
 
 
  <resources>
 
  <resources>
    <string name="app_name">ListDisplay</string>
+
    <string name="app_name">ListDisplay</string>
    <string name="action_settings">Settings</string>
+
    <string name="action_settings">Settings</string>
 
  </resources>
 
  </resources>
  
Line 62: Line 65:
  
 
  <?xml version="1.0" encoding="utf-8"?>
 
  <?xml version="1.0" encoding="utf-8"?>
<!--  Single List Item Design -->
 
 
 
  <TextView xmlns:android="http://schemas.android.com/apk/res/android"
 
  <TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/label"
+
    android:id="@+id/label"
    android:layout_width="fill_parent"
+
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
+
    android:layout_height="fill_parent"
    android:padding="10dip"
+
    android:padding="10dip"
    android:textSize="16dip"
+
    android:textSize="16sp"
    android:textStyle="bold" >
+
    android:textStyle="bold" >
 
  </TextView>
 
  </TextView>
  

Latest revision as of 14:04, 6 March 2022


ListDisplay.java

package com.example.listdisplay;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class MainActivity extends AppCompatActivity {

    String[] mobileArray = {"Android","IPhone","WindowsMobile","Blackberry",
            "WebOS","Ubuntu","Windows7","Max OS X"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
setContentView(R.layout.activity_main);

        ArrayAdapter adapter = new ArrayAdapter<String>(this,
                R.layout.activity_listview, mobileArray);

        ListView listView = (ListView) findViewById(R.id.mobile_list);
        listView.setAdapter(adapter);
    }
}


activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    
xmlns:app="http://schemas.android.com/apk/res-auto"
   
xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <ListView
        android:id="@+id/mobile_list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </ListView> 

</androidx.constraintlayout.widget.ConstraintLayout>

strings.xml

<resources>
    <string name="app_name">ListDisplay</string>
    <string name="action_settings">Settings</string>
</resources>


activity_listview.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/label"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="10dip"
    android:textSize="16sp"
    android:textStyle="bold" >
</TextView>

Referensi