As the simple ListView, custom ListView also uses Adapter classes which added the content from data source (such as string array, array, database etc). Adapter bridges data between an AdapterViews and other Views
Clu List Xml code:
<?xml version="1.0" encoding="utf-8"?>
<ListView 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="wrap_content"
android:dividerHeight="10dp"
android:divider="@android:color/transparent"
android:padding="16dp"
android:id="@+id/list"
android:orientation="vertical"
tools:context="com.deeparora.po.CluList">
</ListView>
Java code for CLU List:
package com.deeparora.po;
import android.app.ProgressDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class CluList extends AppCompatActivity {
ListView listView;
ArrayList<UserActivity> userList;
CustomAdapter adapter;
UserActivity user;
List<User> data;
//ArrayList<UserActivity> data1;
TextView txt;
void initlist() {
// adapter = new CustomAdapter(this, R.layout.listitem, userList);
// listView.setAdapter(adapter);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_clu_list);
Intent rcv = getIntent();
//txt = (TextView) findViewById(R.id.textView43);
String id = rcv.getStringExtra("keyName");
//Toast.makeText(getApplicationContext(), id+" no", Toast.LENGTH_LONG).show();
listView = (ListView) findViewById(R.id.list);
Log.e("kp",id);
new AsyncLogin1().execute(id);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Explict Way
// groupId , itemId, order, title
/*menu.add(1,101,0,"All Songs");
menu.add(1,102,0,"Artists");
menu.add(2,201,0,"Recently Played");
menu.add(2,202,0,"Favourites");*/
// Implict Way | IOC (Inversion Of Control)
getMenuInflater().inflate(R.menu.menu,menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
//int gid = item.getGroupId();
int id = item.getItemId(); // fetch the id on which user has done selection
/*switch (id){
case 101:
Toast.makeText(this,"You Selected All Songs",Toast.LENGTH_LONG).show();
break;
case 102:
Intent intent = new Intent(ActivityOne.this,ActivityTwo.class);
startActivity(intent);
break;
case 201:
break;
case 202:
break;
}*/
switch (id){
case R.id.allSongs:
Toast.makeText(this,"Logout",Toast.LENGTH_LONG).show();
SharedPreferences sp =getSharedPreferences("loginpref",MODE_PRIVATE);
SharedPreferences.Editor editor = sp.edit();
editor.clear();
editor.commit();
finish();
Intent intent = new Intent(CluList.this, POMainActivity.class);
startActivity(intent);
break;
case R.id.contact:
Intent intent1 = new Intent(CluList.this, CONTACT.class);
startActivity(intent1);
break;
case R.id.aboutus:
Intent intent2 = new Intent(CluList.this, Aboutus.class);
startActivity(intent2);
break;
}
return super.onOptionsItemSelected(item);
}
private class AsyncLogin1 extends AsyncTask<String, String, String> {
ProgressDialog pdLoading = new ProgressDialog(CluList.this);
HttpURLConnection conn;
URL url = null;
@Override
protected void onPreExecute() {
super.onPreExecute();
//this method will be running on UI thread
pdLoading.setMessage("\tLoading...");
pdLoading.setCancelable(false);
pdLoading.show();
}
@Override
protected String doInBackground(String... params) {
String response = params[0];
StringBuilder sbk = new StringBuilder();
try {
//String urla = "http://192.168.42.66:8080/clu.php";
String urla = "http://202.164.39.172:2345/test/training/POREPORT/clu.php";
String data1 = "";
URL x = new URL(urla);
HttpURLConnection connection = (HttpURLConnection) x.openConnection();
connection.setRequestMethod("POST");
connection.setDoOutput(true);
connection.setDoInput(true);
OutputStream OS = null;
OS = connection.getOutputStream();
BufferedWriter bufferedWriter = null;
bufferedWriter = new BufferedWriter(new OutputStreamWriter(OS, "UTF-8"));
data1 = URLEncoder.encode("Id", "UTF-8") + "=" + URLEncoder.encode(response, "UTF-8");
bufferedWriter.write(data1);
bufferedWriter.flush();
bufferedWriter.close();
OS.close();
//fetching response
InputStreamReader inp = new InputStreamReader(connection.getInputStream());
Scanner scnrp = new Scanner(inp);
while (scnrp.hasNext()) {
sbk.append(scnrp.nextLine());
}
// return sbk.toString();
} catch (Exception e) {
Log.d("tag", e + "");
}
// return sbk.toString();
return sbk.toString();
}
// Pass data to onPostExecute method
@Override
protected void onPostExecute(String result) {
//this method will be running on UI thread
// Toast.makeText(getApplicationContext(), result + "", Toast.LENGTH_LONG).show();
pdLoading.dismiss();
String country="";
userList = new ArrayList<>();
/// pdLoading.dismiss();
try {
//JSONObject jobj = new JSONObject( URLDecoder.decode( result, "UTF-8" ));
JSONArray jArray = new JSONArray(result);
// user = new UserActivity();
Log.e("jj7", result);
for (int i = 0; i < jArray.length(); i++) {
JSONObject json_data = jArray.getJSONObject(i);
user = new UserActivity();
user.setClu_id(json_data.getString("Id"));
user.setClu_name(json_data.getString("CLUApplyName"));
user.setClu_licence(json_data.getString("Name_of_applicant"));
user.setClu_district(json_data.getString("District"));
userList.add(user);
// country = json_data.getString("District");
//Log.e("jj89", country);
//data.add(user);
}
adapter = new CustomAdapter(getApplication(), R.layout.listitem, userList);
adapter.notifyDataSetChanged();
listView.setAdapter(adapter);
//txt.setText(user.getClu_name());
// Log.e("j90", userList + "");
//adapter.setDropDownViewResource(R.layout.spinner_textview);
// sp2.setAdapter(adapter);
}
catch (JSONException e) {
Toast.makeText(CluList.this, e.toString(), Toast.LENGTH_LONG).show();
}
}
}
}
Comments
Post a Comment