Professional Documents
Culture Documents
Pertemuan 9
Pertemuan 9
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;
@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);
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;
@Override
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
adapter.notifyDataSetChanged(); callVolley();
} private void callVolley() {
itemList.clear();
adapter.notifyDataSetChanged(); swipe.setRefreshing(true);
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>