【问题标题】:using a string array from a SQL DB to make a listview decoding jSon使用 SQL DB 中的字符串数组制作列表视图解码 JSON
【发布时间】:2012-02-15 06:43:33
【问题描述】:

您好,我一直在尝试从 SQL DB 中获取 listView,但在尝试将字符串数组与数组适配器链接时,我不断收到错误,错误在行,

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

ListView menulist = getListView();

这里是我的一些代码,任何帮助将不胜感激

    public class MainActivity extends Activity {
/** Called when the activity is first created. */

static String result = "";
InputStream is = null;
static String storyNames = "";

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    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()   
}
String[] STORYLIST = new Gson().fromJson(result,String[].class);

//end of class body    
}

iv 改成这个了,还是不行

    public class MainActivity extends ListActivity {
/** Called when the activity is first created. */

static String result = "";
InputStream is = null;
static String storyNames = "";

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

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

    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()   
}
String[] STORYLIST = new Gson().fromJson(storyNames,String[].class);

//end of class body    
}

【问题讨论】:

    标签: android sql json listview android-arrayadapter


    【解决方案1】:

    更改“setListAdapter (new ArrayAdapter(this, R.layout.list_item, STORYLIST));”的调用在你填充了 STORYLIST 数组之后......当你在 oncreate 中调用 setlistadapter 时,STORYLIST 仍然是空的(null)..

    【讨论】:

    • 我也有 public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);设置内容视图(R.layout.main); ListView menulist = getListView(); setListAdapter (new ArrayAdapter(this, R.layout.list_item, STORYLIST)); menulist.setTextFilterEnabled(true);
    • 你是否得到一个空指针异常......如果是这样......将“setListAdapter(new ArrayAdapter(this, R.layout.list_item, STORYLIST));”更改为“String”之后的行[] STORYLIST = new Gson().fromJson(storyNames,String[].class);".. 并将整个代码放在 create 方法中.. 我认为它有效.. 因为我做了同样的事情 =]
    【解决方案2】:

    首先获取列表视图 ListView menulist = getListView(); 然后设置适配器 setListAdapter(new ArrayAdapter(this, R.layout.list_item, STORYLIST));

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-07-24
      • 2016-10-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-10
      • 2020-03-29
      相关资源
      最近更新 更多