Click here to Skip to main content
15,895,256 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi
i am getting a listview from webservices..
i need a button to set for it..
since user clicks a button then it should get listed..


guide me please.. this is my code

package info.androidhive.jsonparsing;

import java.util.ArrayList;
import java.util.HashMap;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends ListActivity {

private ProgressDialog pDialog;

// URL to get contacts JSON
private static String url = "http://www.mouthyapp.com/json.php?action=searchUser&keyword=a";

// JSON Node names
private static final String TAG_TEXT_REAL = "userRealName";
private static final String TAG_TEXT_LAST = "userLastName";
private static final String TAG_TEXT_CHAT = "userChatName";
private static final String TAG_ID = "userId";
private static final String TAG_PROFILE = "userProfilePicture";

private static final String TAG_SEARCH = "userProfilePicture";
// contacts JSONArray
JSONArray contacts = null;

// Hashmap for ListView
ArrayList<hashmap><string,>> contactList;

EditText eText;
Button btn;


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

eText = (EditText) findViewById(R.id.inputsearch);
btn = (Button) findViewById(R.id.sample);

btn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// String str = eText.getText().toString();
{
Log.v("EditText",eText.getText().toString());
}
}
});



contactList = new ArrayList<hashmap><string,>>();

ListView lv = getListView();
//
// Calling async task to get json
new GetContacts().execute();
}

/**
* Async task class to get json by making HTTP call
* */
private class GetContacts extends AsyncTask<void,> {

@Override
protected void onPreExecute() {
super.onPreExecute();
// Showing progress dialog
pDialog = new ProgressDialog(MainActivity.this);
pDialog.setMessage("Please wait...");
pDialog.setCancelable(false);
pDialog.show();

}

@Override
protected Void doInBackground(Void... arg0) {
// Creating service handler class instance
ServiceHandler sh = new ServiceHandler();

// Making a request to url and getting response
String jsonStr = sh.makeServiceCall(url, ServiceHandler.GET);

Log.d("Response: ", "> " + jsonStr);

if (jsonStr != null) {
try {
JSONArray array = new JSONArray(jsonStr);

// looping through All Contacts
for (int i = 0; i < array.length(); i++) {
JSONObject c = array.getJSONObject(i);
HashMap<string,> map = new HashMap<string,>();
String name = c.getString(TAG_TEXT_REAL);
String last = c.getString(TAG_TEXT_LAST);
String user = c.getString(TAG_TEXT_CHAT);

Log.v("id", last);
Log.v("name", name);
// tmp hashmap for single contact

// adding each child node to HashMap key => value
map.put(TAG_TEXT_REAL, name);
map.put(TAG_TEXT_LAST, last);
map.put(TAG_TEXT_CHAT, user);

// adding contact to contact list
contactList.add(map);
}
} catch (JSONException e) {
e.printStackTrace();
}
} else {
Log.e("ServiceHandler", "Couldn't get any data from the url");
}

return null;
}

@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
// Dismiss the progress dialog
if (pDialog.isShowing())
pDialog.dismiss();
/**
* Updating parsed JSON data into ListView
* */
ListAdapter adapter = new SimpleAdapter(MainActivity.this,
contactList, R.layout.list_item, new String[] {
TAG_TEXT_REAL, TAG_TEXT_LAST, TAG_TEXT_CHAT },
new int[] { R.id.name, R.id.last, R.id.user });

setListAdapter(adapter);
}

}

}
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900