Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7507677
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T22:32:13+00:00 2026-05-29T22:32:13+00:00

iv been having trouble trying to set a listview from a json object from

  • 0

iv been having trouble trying to set a listview from a json object from mySQL, iv given internet permission i have working item_list files, so here is my code to check out

public class MainActivity extends ListActivity {

String result = "";
InputStream is = null;
String storyNames = "";
//String[] STORYLIST = null;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.menu_list);        

    returnJson();
    String[] STORYLIST = new Gson().fromJson(result,String[].class);

    setListAdapter (new ArrayAdapter<String>(this, R.layout.list_item, STORYLIST));

    ListView menulist = getListView();
    menulist.setTextFilterEnabled(true);

    menulist.setOnItemClickListener(new OnItemClickListener() {    
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {      

            if  (position == 0) {
            //  Intent buttona = new Intent(view.getContext(), OneActivity.class);
              //  startActivity(buttona);
            }

            if (position == 1) {
             //   Intent buttonb = new Intent(view.getContext(), TwoActivity.class);
             //   startActivity(buttonb);
            }

            if (position == 2) {
               // Intent buttonc = new Intent(view.getContext(), ThreeActivity.class);
               // startActivity(buttonc);
            }

            if (position == 3) {
                //Intent buttond = new Intent(view.getContext(), FourActivity.class);
                //startActivity(buttond);
            }

        }

});


    //end of oncreate()    
}

public void returnJson(){

    //TextView one = (TextView) findViewById(R.id.textView1);

    try{
            HttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost("http://10.0.2.2/textures_story_list.php");

            HttpResponse response = httpClient.execute(httpPost);
            HttpEntity entity = response.getEntity();
            is = entity.getContent();

    }catch(Exception e) {
    //  one.setText("error3");
    }

    try{


        BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"), 8);                      
        StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
            is.close();
            result = sb.toString();

    }catch(Exception e) {
    //  one.setText("error2");          
        }

    try{
        JSONArray jArray = new JSONArray(result);
        String storyNames = "";
        for(int i = 0;i<jArray.length();i++){
               // storyNames += jArray.getJSONObject(i).getString("story_name") + "\n"; 

        }
    //  one.setText(storyNames);

    }
    catch(JSONException e) {
    //  one.setText("error1");

    }
        return;


//end of returnJson()   
}

//end of class body    
}

i think the problem is with
setListAdapter
or
String[] STORYLIST

can someone please help with whats going wrong?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T22:32:14+00:00Added an answer on May 29, 2026 at 10:32 pm

    I’m not sure what is wrong for you. But this bit of code works very well for me. Its something I put together from a few tutorials.

    public class MainActivity extends ListActivity {
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);  
    
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, getItems());
        setListAdapter(adapter);
    }
    
    private List<String> getItems() {
        List<String> list = new ArrayList<String>();
        List<NameValuePair> pairs = new ArrayList<NameValuePair>();
        pairs.add(new BasicNameValuePair("whatever", "xxxx"));
        JSONArray jArray = connectToServer("http://10.0.2.2/textures_story_list.php", pairs);
        JSONObject json_data = null;
        for(int i=0; i < jArray.length(); i++) {
            try {
                json_data = jArray.getJSONObject(i);
                list.add(json_data.getString("name"));
            } catch (NumberFormatException e) {
                e.printStackTrace();
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        return list;
    }
    
    public static JSONArray connectToServer(String address, List<NameValuePair> valuePairs) {
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost(address);
        try {           
            httppost.setEntity(new UrlEncodedFormEntity(valuePairs));
            HttpResponse response = httpclient.execute(httppost);
            HttpEntity entity = response.getEntity();
            InputStream is = entity.getContent();
            BufferedReader reader = new BufferedReader(new InputStreamReader(is,"UTF8"),8);
            StringBuilder sb = new StringBuilder();
            sb.append(reader.readLine() + "\n");
            String line="0";
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
            is.close();
            String result = sb.toString();
            JSONArray array = new JSONArray(result);
            return array;
        } catch(Exception e){
            Log.e("log_tag", "Error converting result "+e.toString());
            return null;
        }
    }   
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having a little trouble configuring my mail function. I've been trying to set
I have been trying to build SVM classifier but having trouble with predict .
Hello I have been having trouble with this for a while now. I have
I having trouble in dividing the HTML frames. I have been using the following
I am having trouble with a homework question that I've been working at for
I have been having a lot of trouble figuring out how I can access
Been having some trouble trying to integrate the admob sdk into my application to
I'm having trouble trying to set the value of a property after i cast
I have been having trouble with the WPF DataGrid and listbox GridView performance when
Hey everyone, I'm having trouble configuring my remote debugging set up. I've been searching

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.