Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>this code will be helpful for you. comments are inline</p> <pre><code> public class HelloActivity extends Activity { private InputStream is = null; private StringBuilder sb=null; private String resultString = null; private JSONArray jArray; private String categoryList = ""; private String[] categoryListArray ; public HelloActivity() { } /** * Called with the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); getCategoryList();//get the category list createCategoryView(); } /* * based on the data retrived from the database, the list of category names will be showed here */ private void createCategoryView() { // TODO Auto-generated method stub ScrollView sv = new ScrollView(this); LinearLayout ll = new LinearLayout(this); ll.setOrientation(LinearLayout.VERTICAL); sv.addView(ll); TextView tv = new TextView(this); tv.setText("Foods :-)"); ll.addView(tv); for(int i = 0; i &lt; categoryListArray.length; i++) { CheckBox cb = new CheckBox(this); cb.setText(categoryListArray[i]); ll.addView(cb); } Button b = new Button(this); b.setText("Find Shops"); b.setOnClickListener(new OnClickListener() { public void onClick(View v) { // TODO Auto-generated method stub Toast.makeText(getBaseContext(), categoryList ,Toast.LENGTH_LONG).show(); } }); ll.addView(b); this.setContentView(sv); } /* * For getting the category list from the database * I used php script here, which will return JSON object contains categoryId and CategoryName */ private void getCategoryList() { // TODO Auto-generated method stub ArrayList&lt;NameValuePair&gt; nameValuePairs = new ArrayList&lt;NameValuePair&gt;(); //http post try{ HttpClient httpClient = new DefaultHttpClient(); HttpPost httpPost = new HttpPost("http://192.168.11.156/city.php"); nameValuePairs.add(new BasicNameValuePair("requestText", "requestText")); httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); HttpResponse response = httpClient.execute(httpPost); HttpEntity entity = response.getEntity(); is = entity.getContent(); }catch(Exception ex){ Log.e("log_tag", "Error in http connection"+ex.toString()); } //convert response to string try{ BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8); sb = new StringBuilder(); sb.append(reader.readLine() + "\n"); String line="0"; while ((line = reader.readLine()) != null) { sb.append(line + "\n"); } is.close(); resultString=sb.toString(); }catch(Exception e){ Log.e("log_tag", "Error converting result "+e.toString()); } //paring data int categoryId; String categoryName; try{ jArray = new JSONArray(resultString); JSONObject json_data=null; categoryListArray = new String[jArray.length()]; for(int i=0;i&lt;jArray.length();i++){ json_data = jArray.getJSONObject(i); categoryId=json_data.getInt("categoryId"); categoryName=json_data.getString("categoryName"); categoryListArray[i] = categoryName; categoryList = categoryList + categoryName; if(i != jArray.length()-1){ categoryList += ","; } } } catch(JSONException e1){ Toast.makeText(getBaseContext(), "No Categories are found" ,Toast.LENGTH_LONG).show(); } catch (ParseException e1) { e1.printStackTrace(); } } } </code></pre> <p>Thanks</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload