0% found this document useful (0 votes)
40 views5 pages

Pertemuan 9

The document discusses an Android application that loads word lists from a server and displays them in a list. It includes code for an Adapter class that populates the list items from a Data model, a MainActivity class that handles loading data from the server via Volley on refresh and displaying it in the list, and XML layout files for the activity_main and list item views. The application uses a SwipeRefreshLayout to trigger data reloading and a custom Adapter to map the data model to the list rows.

Uploaded by

Aldi Kurniawan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views5 pages

Pertemuan 9

The document discusses an Android application that loads word lists from a server and displays them in a list. It includes code for an Adapter class that populates the list items from a Data model, a MainActivity class that handles loading data from the server via Volley on refresh and displaying it in the list, and XML layout files for the activity_main and list item views. The application uses a SwipeRefreshLayout to trigger data reloading and a custom Adapter to map the data model to the list rows.

Uploaded by

Aldi Kurniawan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Nama : Aldi Kurniawan

NIM : A11.2021.13391

Pertemuan 9
Adapter.java
package com.example.wordlistloader;
import android.app.Activity; import
android.content.Context; import
android.view.LayoutInflater; import
android.view.View; import
android.view.ViewGroup; import
android.widget.BaseAdapter; import
android.widget.TextView;
import java.util.List;

public class Adapter extends BaseAdapter {


Activity activity; List<Data> items;
private LayoutInflater inflater;

public Adapter(Activity activity, List<Data> items) { this.activity = activity;


this.items = items;
}

@Override
public int getCount() { return items.size();
}

@Override
public Object getItem(int position) { return items.get(position);
}

@Override
public long getItemId(int position) { return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) { if (inflater == null)
inflater = (LayoutInflater)
activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

if (convertView == null) convertView = inflater.inflate(R.layout.list, null);

TextView name = (TextView) convertView.findViewById(R.id.name);


Data data = items.get(position);

name.setText(data.getNama());

return convertView;
}
}

Data.java
package com.example.wordlistloader;
public class Data { private String
name;
public Data() {
} public Data(String name) {
this.name = name;
}
public String getNama() { return name;
} public void setNama(String name) {
this.name = name;

}
}

MainActivity.java
package com.example.wordlistloader;

import androidx.appcompat.app.AppCompatActivity;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout; import
android.os.Bundle; import android.util.Log; import android.widget.ListView; import
com.android.volley.RequestQueue; import com.android.volley.Response; import
com.android.volley.VolleyError; import com.android.volley.VolleyLog;
import com.android.volley.toolbox.JsonArrayRequest; import
com.android.volley.toolbox.Volley; import
com.google.android.material.floatingactionbutton.FloatingActionButton; import org.json.JSONArray;
import org.json.JSONException; import org.json.JSONObject; import java.util.ArrayList; import
java.util.List;
import static android.content.ContentValues.TAG;

public class MainActivity extends AppCompatActivity implements SwipeRefreshLayout.OnRefreshListener


{
public static final String URL = "http://192.168.1.11/select.php";
ListView list;
SwipeRefreshLayout swipe;
List<Data> itemList = new ArrayList<Data>();
Adapter adapter;

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

swipe = (SwipeRefreshLayout) findViewById(R.id.swipe); list = (ListView)


findViewById(R.id.list);
adapter = new Adapter(MainActivity.this, itemList);
list.setAdapter(adapter); swipe.setOnRefreshListener(this); swipe.post(new
Runnable() {
@Override public void run() {
swipe.setRefreshing(true); itemList.clear();
adapter.notifyDataSetChanged(); callVolley();
}
});
}
@Override
public void onRefresh() { itemList.clear();

adapter.notifyDataSetChanged(); callVolley();
} private void callVolley() {
itemList.clear();
adapter.notifyDataSetChanged(); swipe.setRefreshing(true);

JsonArrayRequest jArr = new JsonArrayRequest(URL, new


Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) { for (int i = 0; i <
response.length(); i++) { try {
JSONObject obj = response.getJSONObject(i); Data item = new Data();

item.setNama(obj.getString("name"));
itemList.add(item); } catch
(JSONException e) {
e.printStackTrace();
} }
adapter.notifyDataSetChanged();

swipe.setRefreshing(false);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) { VolleyLog.d(TAG, "Error: " +
error.getMessage()); swipe.setRefreshing(false);
}
});
RequestQueue mRequestQueue =
Volley.newRequestQueue(getApplicationContext()); mRequestQueue.add(jArr);
}
}
Activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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">

<androidx.swiperefreshlayout.widget.SwipeRefreshLayout android:id="@+id/swipe"
android:layout_width="match_parent" android:layout_height="wrap_content" >
<ListView
android:id="@+id/list" android:layout_width="match_parent"
android:layout_height="wrap_content" android:divider="@color/black"
android:dividerHeight="2dp" />
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>

</RelativeLayout>

List.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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">

<androidx.swiperefreshlayout.widget.SwipeRefreshLayout android:id="@+id/swipe"
android:layout_width="match_parent" android:layout_height="wrap_content" >
<ListView
android:id="@+id/list"
android:layout_width="match_parent" android:layout_height="wrap_content"
android:divider="@color/black" android:dividerHeight="2dp" />
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
</RelativeLayout>

You might also like