I am working on activity which request one php file on server & this php file will return me one JSONArray
having JSONObjects
as its elements. I get this jArray
& extract out its contents e.g., all jsonObjects
. each jsonObject have data of one specific user like uniqueId, name, address, contact details etc... I showed a list of all users. I need when I click on list item it will show me complete profile of the concern user...& for that i put these extracted jsonObjects
in another JSONObject
having uniqueId as name & the corresponding extracted jsonObject
as its value.
My list :
this!
When i click on one item of this list I get its uniqueId
what I need is, all keys of jsonObject
having JsonObject
as its values.
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long id) {
ItemBean bean = (ItemBean) adapter.getItem(position);
Toast.makeText(this," FanId => " + bean.getUid() +" \n FanName => " + bean.getTitle(),
Toast.LENGTH_SHORT).show();
Iterator<String> userKeys=extracted();//here i get-> Null PointerException!
while(fKeys.hasNext()){
System.out.println("Key:"+userKeys.next());
}
}
//iterator that return me keys(e.g., all uids of users)
@SuppressWarnings("unchecked")
private Iterator<String> extracted() {
Iterator<String> keys = jFansData.keys();
return keys;
}
12-10 20:45:55.833: ERROR/AndroidRuntime(907): Caused by: java.lang.NullPointerException
12-10 20:45:55.833: ERROR/AndroidRuntime(907): at com.technotalkative.listview5.MainActivity.extracted(MainActivity.java:87)
12-10 20:45:55.833: ERROR/AndroidRuntime(907): at com.technotalkative.listview5.MainActivity.onCreate(MainActivity.java:51)
12-10 20:45:55.833: ERROR/AndroidRuntime(907): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
I am not getting what I do wrong over here, any solution please...
Here is the Complete Code Of My Class->
public class MainActivity extends Activity implements OnItemClickListener {
String url,fd;
ListView mFansListView;
JSONObject jFan_Data;//contain data of an indivisual fan
JSONObject jFansData;//contain data of an all fans
ListViewCustomAdapter adapter;
LayoutInflater lInflater;
private ArrayList<Object> fansList;
private ItemBean bean;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
prepareArrayLits();
mFansListView = (ListView) findViewById(android.R.id.list);
adapter = new ListViewCustomAdapter(this, fansList);
mFansListView.setAdapter(adapter);//come null pointer exception when no fan data is returned! hendle it...
mFansListView.setOnItemClickListener(this);
}
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long id) {
// TODO Auto-generated method stub
ItemBean bean = (ItemBean) adapter.getItem(position);
Toast.makeText(this," FanId => " + bean.getUid() +" \n UID => " + bean.getTitle(), Toast.LENGTH_SHORT).show();
Iterator<String> fKeys=extracted();
while(fKeys.hasNext()){
System.out.println("Key:"+fKeys.next());
}
}
//....................................................................
//iterator that return me keys(e.g., all uids of fans) through which i match clicked lists id
@SuppressWarnings("unchecked")
private Iterator<String> extracted() {
Iterator<String> keys = jFansData.keys();//I get NullPointerExcertion over here...???
return keys;
}
//.............................................................................
/* *Method used to prepare the ArrayList,
*/
public void prepareArrayLits()
{
url="http://192.168.200.111/ManU/location1_and.php";
HttpClient client = new DefaultHttpClient();
HttpConnectionParams.setConnectionTimeout(client.getParams(), 10000);
HttpResponse response;
JSONObject json = new JSONObject();
try {
HttpPost post=new HttpPost(url);
/**! Currently all these parameters are hard coded...
* In future i have to use services of LocationManager to get my current location!
*/
json.put("radius", 1000);
json.put("lat", 12.9716060);
json.put("lang", 77.5903760);
json.put("myid", "h9ow0");
post.setHeader("json", json.toString());
StringEntity se=new StringEntity(json.toString());
se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
post.setEntity(se);
response=client.execute(post);
/* Checking response */
// Get the data in the entity
InputStream in=response.getEntity().getContent();
//convert it to string...
fd=convertStreamToString(in);
Log.i("Read from Server", fd);
//Checking that fd is array containing data of all users or have no data...
String no_data="No Data";
if(no_data==fd){
Log.i("NoRecords", "No Users Near you... Try over long distance!");
}else {
JSONArray jArray = new JSONArray(fd);//contain array returned by "php" having data of all users
try{
fansList = new ArrayList<Object>();
for(int i=0;i<jArray.length();i++){
//will return the data of each row fetched from JSONArray returned by location1.php
String data_of_each_user=jArray.get(i).toString();
Log.i("Data Of User at index "+i+" is", data_of_each_user);
//I put the object at index "i" into JSONObject & retrieve data from name-value pair
jFan_Data=jArray.getJSONObject(i);//data of User at index i in array
AddObjectToList(jFan_Data.getString("uniqid").toString(),R.drawable.ic_add, jFan_Data.getString("name"), jFan_Data.getString("distance"));
try{
/** this JSONObject hold data of all users taking UniqueId of user as key &
* JSONObject at index i in the array returned by the php as the value!!! */
jFansData.put(jFan_Data.get("uniqid").toString(), jFan_Data);//why i getting NullPointerException Over Here...???
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
//..............................................................................
// Add one item into the Array List
public void AddObjectToList(String uid,int image, String title, String desc)
{
bean = new ItemBean();
bean.setUid(uid);
bean.setDescription(desc);
bean.setImage(image);
bean.setTitle(title);
fansList.add(bean);
}
// -------------------------------------------------------------------------
//convert respons into human readable formate
private static 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();
}
}
as i mentioned above, I need to show the complete profile of the user when i click on list item... So, What i do to solve this problem!!! ???