Showing posts with label Listview. Show all posts
Showing posts with label Listview. Show all posts

Android Json parsing Example with listview?



The process of parsing JSON in Android is pretty simple, thankfully. We'll be using JSONObject for all the parsing goodness - there are also some other JSON classes, but we'll just go through the basic ones to give you a general idea of how it works.

Activity.Java


public class AndroidJSONParsingActivity extends Activity {
private ProgressDialog dialog;
private String string,id,name,mail;  
private ListView listview;
private ArrayList<SearchParent> arrayParents;
//private ArrayList<String> arrayChildren; 

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dialog = new ProgressDialog(this);
string = "http://api.androidhive.info/contacts/";
 
listview = (ListView) findViewById(R.id.listView);
listview.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View view, int position, long id) {
//   Auto-generated method stub
Object  ob = (Object) view.getTag();  
if(ob != null) {
TotalhistoryInfo parent = (TotalhistoryInfo) view.getTag();  
String name = parent.getPage_name();  
String mail = parent.getPage_date();
String ID = parent.getId();
Log.i("details=>", ID + name + mail);  
}
}
});
new uploadaudio().execute();
}
private void ProgressBar_show() {

((ProgressDialog) dialog).setTitle("JsonParsing");  
((ProgressDialog) dialog).setMessage("Your content is loading.. \nPlease wait..");
((ProgressDialog) dialog).setIndeterminate(true);
dialog.setCancelable(false);
dialog.show();
}

private void ProgressBar_hide() {
dialog.dismiss();
}
class uploadaudio extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
ProgressBar_show();
}

@Override
protected String doInBackground(String... params) {
load_JsonSections();
return null;
}
@SuppressLint("SetJavaScriptEnabled")
protected void onPostExecute(String result) {  
super.onPostExecute(result);
 
Log.i("onPostExecute==> ", arrayParents.toString());
AddedHistoryAdapter adapter = new AddedHistoryAdapter(AndroidJSONParsingActivity.this, R.layout.row_acc_info, arrayParents );   
listview.setAdapter(adapter); 
ProgressBar_hide();  
}
}
  
private void load_JsonSections() {
arrayParents = new ArrayList<SearchParent>();  
HttpClient httpclient = new DefaultHttpClient();
HttpGet httpget = new HttpGet(string);
HttpResponse response;
try {
response = httpclient.execute(httpget);  
HttpEntity entity = response.getEntity();  

if (entity != null) {

InputStream instream = entity.getContent();
String result = convertStreamToString(instream);
JSONObject json = new JSONObject(result);
 
JSONArray jsonOnb = json.getJSONArray("contacts");  
for (int i = 0; i < jsonOnb.length(); i++) {
JSONObject _Jsonquery = jsonOnb.getJSONObject(i);  

if (_Jsonquery.has("id")) {
 
id = _Jsonquery.getString("id");
name = _Jsonquery.getString("name");
mail = _Jsonquery.getString("email");  
arrayParents.add(new SearchParent(name, mail, id));  
}  
}
instream.close();
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
}

private String convertStreamToString(InputStream is) {

BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();

String line = null;
try {
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return sb.toString();
}
}

AdapterActivity.java



public class AddedHistoryAdapter extends ArrayAdapter<SearchParent> {
//private final static String TAG = AccountsAdapter.class.getSimpleName();
private ArrayList<SearchParent> _valu;
private Context context;

public AddedHistoryAdapter(Context _context, int _names, ArrayList<SearchParent> valu) {
super(_context, R.layout.row_acc_info, valu);
context = _context;
_valu = valu;
Log.i("_value= ", _valu.toString());
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
//View view = convertView;
if (convertView == null) {
LayoutInflater vi = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = vi.inflate(R.layout.row_acc_info, parent, false);
}

TextView acc_id = (TextView) convertView.findViewById(R.id.textView_id);  
TextView acc_name = (TextView) convertView.findViewById(R.id.textView1);  
TextView acc_date = (TextView) convertView.findViewById(R.id.textView2);  
 
SearchParent acc_info = _valu.get(position);    
String name = acc_info.getName();
String date = acc_info.getMail();
String ID = acc_info.getId();
acc_name.setText(name);
acc_date.setText(date);
acc_id.setText(ID); 
TotalhistoryInfo total_acc = new TotalhistoryInfo();
total_acc.setPage_name(name);
total_acc.setPage_date(date);
total_acc.setId(ID);
convertView.setTag(total_acc);  
return (convertView);
}  
}

SearchParent.java


public class SearchParent {  
private String Id;
private String name;
private String mail;
public String getId() {
return Id;
}
public void setId(String id) {
this.Id = id;
}
public SearchParent(String i, String d, String p) {
this.mail = d;
this.name = i;
this.Id = p;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getMail() {
return mail;
}
public void setMail(String mail) {
this.mail = mail;
}

TotalHistoryInfo.java


public class TotalhistoryInfo {
private String id;
private String page_name;
private String page_date;

public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getPage_date() {
return page_date;
}
public void setPage_date(String page_date) {
this.page_date = page_date;
}
public String getPage_name() {
return page_name;
}
public void setPage_name(String acc_info) {
this.page_name = acc_info;
}
}

Phone Contacts Display in Listview using Adapter and asynctask?

Today we discuss the one of the important topic is How to get the phone contacts,
We can get contacts using  using this CURSOR and another topic is ContactsContract this two functions are very important to get the contacts.

Let's Start for coding.

MainActivity.java 


      Button button_add.setOnClickListener(new OnClickListener() {
               @Override
       public void onClick(View v) {       
          LoadContactsAyscn lca = new LoadContactsAyscn();
          lca.execute();
    }
});
  1.   class LoadContactsAyscn extends AsyncTask<Void, Void, ArrayList<Contacts_Item>> {
    ProgressDialog progressdialog;

    @Override
    protected void onPreExecute() {
    super.onPreExecute();
    progressdialog = ProgressDialog.show(Contact1.this, "Loading Contacts", "Please Wait");
    }

    @Override
    protected ArrayList<Contacts_Item> doInBackground(Void... params) {
      Cursor cursor = getContentResolver().query(
    ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);

    while (cursor.moveToNext()) {
    contactName = cursor
    .getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
    phNumber = cursor
    .getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));                
       
    Contacts_Item weather = new Contacts_Item();
      weather.setName(contactName);
    weather.setType(phNumber);
    contacts_list.add(weather);
      }
    cursor.close();
    return contacts_list;
    }

    @Override
    protected void onPostExecute(final ArrayList<Contacts_Item> news_list) {
       super.onPostExecute(news_list);
      progressdialog.cancel();
       MySimpleArrayAdapter adapter = new MySimpleArrayAdapter(Contact1.this, R.layout.adapter, news_list);
    listview.setAdapter(adapter);
    listview.setOnItemClickListener(new OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> arg0, View view, int position, long arg3) {
    Object  ob = (Object) view.getTag();  
    if(ob != null)  {
    Contacts_Item Details = (Contacts_Item) view.getTag();  
    String name = Details.getName();
    String contactno = Details.getType();
      Toast.makeText(Contact1.this, "position ==> " +position + name + " ==> "+contactno , Toast.LENGTH_SHORT).show();
    Intent intent = new Intent(Contact1.this, CustomerData1.class);  
    Bundle bundle = new Bundle();
    bundle.putInt("position", position);
    intent.putExtra("contactname", name);
    intent.putExtra("phnumber", contactno);
    startActivity(intent);
      }  
    }
    });
    }
    }

activity_main.xml


  1.  
  2. <?xml version="1.0" encoding="utf-8"?>
  3. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  4.     android:layout_width="match_parent"
  5.     android:layout_height="match_parent"
  6.     android:orientation="vertical" >

  7.     <LinearLayout
  8.         android:id="@+id/linearLayout1"
  9.         android:layout_width="match_parent"
  10.         android:layout_height="50dp"
  11.         android:layout_alignParentLeft="true"
  12.         android:layout_alignParentRight="true"
  13.         android:background="@drawable/bar" >

  14.         <TextView
  15.             android:id="@+id/contacts"
  16.             android:layout_width="match_parent"
  17.             android:layout_height="wrap_content"
  18.             android:layout_gravity="center"
  19.             android:gravity="center"
  20.             android:text="Contacts"
  21.             android:textColor="#FFFFFF"
  22.             android:textSize="30sp"
  23.             android:textStyle="bold" />
  24.     </LinearLayout>

  25.     <ListView
  26.         android:id="@+id/listView1"
  27.         android:layout_width="match_parent"
  28.         android:layout_height="wrap_content"
  29.         android:divider="#000000"
  30.         android:dividerHeight="2px" >
  31.     </ListView>

  32. </LinearLayout>

MySimpleArrayAdapter.java 



  1. public class MySimpleArrayAdapter extends ArrayAdapter<Contacts_Item> {
  2.   private final Context context;
  3.   private final ArrayList<Contacts_Item> values;  

  4.   public MySimpleArrayAdapter(Context context, int custom, ArrayList<Contacts_Item> news_list) {
  5.  super(context, R.layout.adapter, news_list);
  6.    this.context = context;
  7.  this.values = news_list;
  8.   }
  9.   
  10.   @Override
  11.   public View getView(int position, View convertView, ViewGroup parent) {  
  12.  if(convertView == null) {
  13.  LayoutInflater inflater = (LayoutInflater) context .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  14.  convertView = inflater.inflate(R.layout.adapter, parent, false);    
  15.  }  
  16.  
  17.  TextView textView = (TextView) convertView.findViewById(R.id.name);
  18.  TextView textView1 = (TextView) convertView.findViewById(R.id.number);
  19.  
  20.  Contacts_Item acc_info = new Contacts_Item();
  21.  acc_info = values.get(position);  
  22.    
  23.  String name = acc_info.getName();  
  24.  String number = acc_info.getType();
  25.  textView.setText(name);
  26.  textView1.setText(number);  
  27.  
  28.  convertView.setTag(acc_info);   
  29.  return convertView;
  30.   }

Here is the class for we can set and get the values.

 Contacts_Item.java 


  1. public class Contacts_Item {
  2.  
  3. private String Name;
  4. private String Number;

  5. public String getType() {
  6. return Number;
  7. }
  8. public void setType(String type) {
  9. this.Number = type;
  10. }  
  11. public String getName() {
  12. return Name;
  13. }
  14. public void setName(String name) {
  15. Name = name;
  16. }


Output


Select DateRange UsingRangePicker.

  /* * This Method is for select range from picker. * */ private fun selectDateRangeUsingRangePicker () { pageNumber = 1 val displ...