0

在此处输入图像描述我的应用程序的很大一部分是从网站获取数据。数据显示在我的 logcat 中,绿色没有错误,但不会显示在我的 android 视图中。我已经尝试并搜索了一周,或者没有运气。这是我的课。

public class Json extends ListActivity {


ArrayList<HashMap<String, String>> jsonParser = new ArrayList<HashMap<String, String>>();

ListView lv ;
private static final String jsonFilePath = "http://xda.olinksoftware.com/leaderboard/all";

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.json);


    new ProgressTask(Json.this).execute();
 }

  private class ProgressTask extends AsyncTask<String, Void, Boolean> {

private ProgressDialog dialog;

   public ProgressTask(Json json) {

   Log.i("1", "Called");
   context = json;
   dialog = new ProgressDialog(context);
  }


  private Context context;


   protected void onPreExecute() {
   this.dialog.setMessage("Progress start");
   this.dialog.show();
   }

    @Override
    protected void onPostExecute(final Boolean success) {
        if (dialog.isShowing()) {
            dialog.dismiss();
        }



        ListAdapter adapter = new SimpleAdapter(context, jsonParser,
                R.layout.listitem, new String[] { TAG_NAME, TAG_SCORE,
                 }, new int[] {
                        R.id.score, R.id.name, 
     });

        setListAdapter(adapter);

        // selecting single ListView item
         lv = getListView();


    }



    @Override
   protected Boolean doInBackground(final String... args) {

       new JSONParser();




       try {
                    BufferedReader reader = null;
                    String jsonString = "";
                    StringBuffer buffer = new StringBuffer();
                    try{
                        URL url = new URL(jsonFilePath);
                        reader = new BufferedReader(new InputStreamReader(url.openStream()));
                        int read;
                        char[] chars = new char[1024];
                        while ((read = reader.read(chars)) != -1)
                            buffer.append(chars, 0, read);
                    }finally {
                    if (reader != null)
                        reader.close();
                    }   

                    jsonString = buffer.toString();
                    try{
                        JSONParser jsonParser = new JSONParser();
                        JSONArray leaderboard = (JSONArray)jsonParser.parse(jsonString);
                        for(int i = 0;i<leaderboard.size();i++){
                            JSONObject user = (JSONObject)leaderboard.get(i);
                            System.out.println((i+1) + ". " + user.get("forumName") + " (" + user.get("score") + ")");                            
                        }
                    }catch(ParseException pe){
                        System.out.println("position: " + pe.getPosition());
                        System.out.println(pe);
                    }

            } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }}  

  }

这是我的xml。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<!-- Main ListView 
     Always give id value as list(@android:id/list)
-->
<ListView
    android:id="@android:id/list"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"/>
</LinearLayout>



<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">

 <!-- Product id (pid) - will be HIDDEN - used to pass to other activity -->
<TextView
android:id="@+id/name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:visibility="gone" />

  <!-- Name Label -->
<TextView
android:id="@+id/score"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingTop="6dp"
android:paddingLeft="6dp"
android:textSize="17sp"
android:textStyle="bold"/>
</LinearLayout>

任何帮助是极大的赞赏。我知道我的 listview 做错了什么,因为它也可以作为在 eclipse 中运行的直接 java 应用程序。

这是我正在抓取的数据,此时我只取两个值。“论坛用户”和“分数”

[{"userId":"3579348","userURL":"http:\/\/forum.xda-developers.com\/member.php?u=3579348","forumName":"newtoroot","totalPosts":"5074","postsPerDay":"5.14","totalThanks":"18302","joinDate":"2011-01-29","yearsJoined":"2","referrals":"4","friendCount":"38","recognizedDeveloper":"1","recognizedContributor":"0","recognizedThemer":"0","moderator":"0","recognizedEliteDeveloper":"0","romCount":"48","kernelCount":"0","tutorialCount":"0","modCount":"1","themeCount":"0","score":"302","userName":"","password":""},{"userId":"1596076","userURL":"http:\/\/forum.xda-developers.com\/member.php?u=1596076","forumName":"il Duce","totalPosts":"16335","postsPerDay":"9.75","totalThanks":"15799","joinDate":"2009-02-25","yearsJoined":"4","referrals":"2","friendCount":"83","recognizedDeveloper":"1","recognizedContributor":"0","recognizedThemer":"0","moderator":"1","recognizedEliteDeveloper":"0","romCount":"1","kernelCount":"1","tutorialCount":"0","modCount":"0","themeCount":"0","score":"132","userName":"","password":""},{"userId":"2930301","userURL":"http:\/\/forum.xda-developers.com\/member.php?u=2930301","forumName":"fernando sor","totalPosts":"8967","postsPerDay":"7.93","totalThanks":"4549","joinDate":"2010-09-07","yearsJoined":"3","referrals":"2","friendCount":"29","recognizedDeveloper":"0","recognizedContributor":"0","recognizedThemer":"1","moderator":"0","recognizedEliteDeveloper":"0","romCount":"1","kernelCount":"0","tutorialCount":"5","modCount":"2","themeCount":"15","score":"120","userName":"fernando sor","password":""},{"userId":"3220669","userURL":"http:\/\/forum.xda-developers.com\/member.php?u=3220669","forumName":"1975jamie","totalPosts":"582","postsPerDay":"0.56","totalThanks":"127","joinDate":"2010-11-23","yearsJoined":"2","referrals":"0","friendCount":"0","recognizedDeveloper":"1","recognizedContributor":"0","recognizedThemer":"0","moderator":"0","recognizedEliteDeveloper":"0","romCount":"4","kernelCount":"0","tutorialCount":"0","modCount":"0","themeCount":"0","score":"46","userName":"1975jamie","password":""},{"userId":"2552854","userURL":"http:\/\/forum.xda-developers.com\/member.php?u=2552854","forumName":"jeffsanace","totalPosts":"2797","postsPerDay":"2.25","totalThanks":"2836","joinDate":"2010-05-05","yearsJoined":"3","referrals":"0","friendCount":"12","recognizedDeveloper":"0","recognizedContributor":"1","recognizedThemer":"0","moderator":"0","recognizedEliteDeveloper":"0","romCount":"0","kernelCount":"0","tutorialCount":"0","modCount":"0","themeCount":"0","score":"37","userName":"","password":""},{"userId":"2067958","userURL":"http:\/\/forum.xda-developers.com\/member.php?u=2067958","forumName":"eg1122","totalPosts":"1200","postsPerDay":"0.82","totalThanks":"1695","joinDate":"2009-10-05","yearsJoined":"3","referrals":"0","friendCount":"6","recognizedDeveloper":"0","recognizedContributor":"0","recognizedThemer":"0","moderator":"0","recognizedEliteDeveloper":"0","romCount":"0","kernelCount":"0","tutorialCount":"0","modCount":"2","themeCount":"0","score":"20","userName":"","password":""},{"userId":"3042344","userURL":"http:\/\/forum.xda-developers.com\/member.php?u=3042344","forumName":"dfuse06","totalPosts":"3331","postsPerDay":"3.08","totalThanks":"2270","joinDate":"2010-10-11","yearsJoined":"2","referrals":"1","friendCount":"29","recognizedDeveloper":"0","recognizedContributor":"0","recognizedThemer":"0","moderator":"0","recognizedEliteDeveloper":"0","romCount":"0","kernelCount":"0","tutorialCount":"0","modCount":"1","themeCount":"0","score":"17","userName":"","password":""},{"userId":"1070340","userURL":"http:\/\/forum.xda-developers.com\/member.php?u=1070340","forumName":"chrisloveskaos","totalPosts":"215","postsPerDay":"0.11","totalThanks":"8","joinDate":"2008-07-08","yearsJoined":"5","referrals":"0","friendCount":"7","recognizedDeveloper":"0","recognizedContributor":"0","recognizedThemer":"0","moderator":"0","recognizedEliteDeveloper":"0","romCount":"1","kernelCount":"0","tutorialCount":"0","modCount":"0","themeCount":"0","score":"14","userName":"","password":""},{"userId":"2688514","userURL":"http:\/\/forum.xda-developers.com\/member.php?u=2688514","forumName":"GooTz66","totalPosts":"999","postsPerDay":"0.84","totalThanks":"70","joinDate":"2010-06-25","yearsJoined":"3","referrals":"0","friendCount":"7","recognizedDeveloper":"0","recognizedContributor":"0","recognizedThemer":"0","moderator":"0","recognizedEliteDeveloper":"0","romCount":"0","kernelCount":"0","tutorialCount":"0","modCount":"0","themeCount":"0","score":"7","userName":"","password":""},{"userId":"2141845","userURL":"http:\/\/forum.xda-developers.com\/member.php?u=2141845","forumName":"Kush.Kush\u00c2\u0099","totalPosts":"86","postsPerDay":"0.06","totalThanks":"0","joinDate":"2009-11-09","yearsJoined":"3","referrals":"0","friendCount":"16","recognizedDeveloper":"0","recognizedContributor":"0","recognizedThemer":"0","moderator":"0","recognizedEliteDeveloper":"0","romCount":"0","kernelCount":"0","tutorialCount":"0","modCount":"0","themeCount":"0","score":"6","userName":"","password":""}]
4

2 回答 2

0

Why is this line

     setListAdapter(adapter);

Before this

    // selecting single ListView item
     lv = getListView();

Also, the LogCat you posted is showing the results using the "info" filter only. Try looking at the verbose view to make sure no exceptions that you're missing.

于 2013-10-14T00:52:48.867 回答
0

我为解决这个问题所做的工作几乎是重新开始。我添加了一个 JSONParser 类

public class JSONParser {

static InputStream is = null;
static JSONArray jarray = null;
static String json = "";

// constructor
public JSONParser() {

}

public JSONArray getJSONFromUrl(String url) {

       StringBuilder builder = new StringBuilder();
        HttpClient client = new DefaultHttpClient();
        HttpGet httpGet = new HttpGet(url);
        try {
          HttpResponse response = client.execute(httpGet);
          StatusLine statusLine = response.getStatusLine();
          int statusCode = statusLine.getStatusCode();
          if (statusCode == 200) {
            HttpEntity entity = response.getEntity();
            InputStream content = entity.getContent();
            BufferedReader reader = new BufferedReader(new InputStreamReader(content));
            String line;
            while ((line = reader.readLine()) != null) {
              builder.append(line);
            }
          } else {
            Log.e("==>", "Failed to download file");
          }
        } catch (ClientProtocolException e) {
          e.printStackTrace();
        } catch (IOException e) {
          e.printStackTrace();
        }

    // try parse the string to a JSON object
    try {
        jarray = new JSONArray( builder.toString());
    } catch (JSONException e) {
        Log.e("JSON Parser", "Error parsing data " + e.toString());
    }

    // return JSON String
return jarray;

和一个 JsonActivity

public class MainActivity extends ListActivity {
private static String url = "website";

private static final String TAG_VTYPE = "forumName";
private static final String TAG_VCOLOR = "score";
private static final String TAG_THANKS = "totalThanks";
private static final String TAG_POSTS = "totalPosts";
private static final String TAG_JOIN_DATE = "joinDate";
private static final String TAG_ROM_COUNT = "romCount";
private static final String TAG_THEME_COUNT = "themeCount";
private static final String TAG_MOD_COUNT = "modCount";
private static final String TAG_KERNEL_COUNT = "kernelCount";
private static final String TAG_TUTORIAL_COUNT = "tutorialCount";
private static final String TAG_DEV = "recognizedDeveloper";
private static final String TAG_THEMER = "recognizedThemer";
private static final String TAG_MODERATOR = "moderator";
private static final String TAG_RDEV = "recognizedEliteDeveloper";
private static final String TAG_RCOD = "recognizedContributor";

ArrayList<HashMap<String, String>> jsonlist = new ArrayList<HashMap<String, String>>();
private View header;
ListView lv ;
LayoutInflater Inflater;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_view);
    Inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);


    new ProgressTask(MainActivity.this).execute();
}

private class ProgressTask extends AsyncTask<String, Void, Boolean> {
    private ProgressDialog dialog;



    private ListActivity activity;

    // private List<Message> messages;
    public ProgressTask(ListActivity activity) {
        this.activity = activity;
        context = activity;
        dialog = new ProgressDialog(context);

    }

    /** progress dialog to show user that the backup is processing. */

    /** application context. */
    private Context context;


    protected void onPreExecute() {
        this.dialog.setMessage("Progress start");
        this.dialog.show();
    }

    @Override
    protected void onPostExecute(final Boolean success) {
        if (dialog.isShowing()) {
            dialog.dismiss();
        }



        View header = Inflater.inflate(R.layout.header_view_name, null);
        ListAdapter adapter = new SimpleAdapter(context, jsonlist,
                R.layout.list_item, new String[] { TAG_VTYPE, TAG_VCOLOR, TAG_THANKS, TAG_POSTS, TAG_JOIN_DATE, TAG_ROM_COUNT,
                TAG_THEME_COUNT, TAG_MOD_COUNT, TAG_KERNEL_COUNT, TAG_DEV, TAG_TUTORIAL_COUNT, TAG_THEMER, TAG_MODERATOR, TAG_RDEV, TAG_RCOD,
                         }, new int[] {
                        R.id.vehicleType, R.id.vehicleColor, R.id.totalThanks, R.id.totalPosts, R.id.joinDate, R.id.romCount,
                        R.id.themeCount, R.id.kernelCount, R.id.modCount, R.id.tutorialCount, R.id.moderator, R.id.rThemer, R.id.rDev,
                        R.id.rCon, R.id.rEliteDev,
                        });


        lv = getListView();

        lv.addHeaderView(header);

        setListAdapter(adapter);

        // selecting single ListView item


        // Launching new screen on Selecting Single ListItem
        lv.setOnItemClickListener(new OnItemClickListener() {

                         ;

        @Override
         public void onItemClick(AdapterView<?> parent, View view, int position,
              long id) {
            // getting values from selected ListItem
                String forumName = ((TextView) view.findViewById(R.id.vehicleType)).getText().toString();
                String score = ((TextView) view.findViewById(R.id.vehicleColor)).getText().toString();
                String totalThanks = ((TextView) view.findViewById(R.id.totalThanks)).getText().toString();
                String totalPosts = ((TextView) view.findViewById(R.id.totalPosts)).getText().toString();
                String joinDate = ((TextView) view.findViewById(R.id.joinDate)).getText().toString();
                String romCount = ((TextView) view.findViewById(R.id.romCount)).getText().toString();               
                String themeCount = ((TextView) view.findViewById(R.id.themeCount)).getText().toString();
                String kernelCount = ((TextView) view.findViewById(R.id.kernelCount)).getText().toString();
                String modCount = ((TextView) view.findViewById(R.id.modCount)).getText().toString();   
                String tutorialCount = ((TextView) view.findViewById(R.id.tutorialCount)).getText().toString();
                String moderator = ((TextView) view.findViewById(R.id.moderator)).getText().toString();
                String recognizedThemer = ((TextView) view.findViewById(R.id.rThemer)).getText().toString();
                String recognizedDeveloper = ((TextView) view.findViewById(R.id.rDev)).getText().toString();
                String recognizedContributor = ((TextView) view.findViewById(R.id.rCon)).getText().toString();
                String recognizedEliteDeveloper = ((TextView) view.findViewById(R.id.rEliteDev)).getText().toString();





                // Starting new intent





                Intent in = new Intent(getApplicationContext(), SingleMenuItemActivity.class);
                    in.putExtra(TAG_VTYPE, forumName);
                    in.putExtra(TAG_VCOLOR, score);
                    in.putExtra(TAG_THANKS, totalThanks);
                    in.putExtra(TAG_POSTS, totalPosts);
                    in.putExtra(TAG_JOIN_DATE, joinDate);
                    in.putExtra(TAG_ROM_COUNT, romCount);           
                    in.putExtra(TAG_THEME_COUNT, themeCount);
                    in.putExtra(TAG_MOD_COUNT, modCount);
                    in.putExtra(TAG_KERNEL_COUNT, kernelCount);

                    in.putExtra(TAG_TUTORIAL_COUNT, tutorialCount);
                    in.putExtra(TAG_DEV, recognizedDeveloper);
                    in.putExtra(TAG_THEMER, recognizedThemer);
                    in.putExtra(TAG_MODERATOR, moderator);
                    in.putExtra(TAG_RDEV, recognizedEliteDeveloper);
                    in.putExtra(TAG_RCOD, recognizedContributor);







                    startActivity(in);              


                        }});}




    protected Boolean doInBackground(final String... args) {

            JSONParser jParser = new JSONParser();

            // getting JSON string from URL
            JSONArray json = jParser.getJSONFromUrl(url);

            for (int i = 0; i < json.length(); i++) {

                try {
                    JSONObject c = json.getJSONObject(i);
                    String forumName = c.getString(TAG_VTYPE);
                    String score = c.getString(TAG_VCOLOR);
                    String totalThanks = c.getString(TAG_THANKS);
                    String totalPosts = c.getString(TAG_POSTS);
                    String joinDate = c.getString(TAG_JOIN_DATE);
                    String romCount = c.getString(TAG_ROM_COUNT);
                    String themeCount = c.getString(TAG_THEME_COUNT);
                    String modCount = c.getString(TAG_MOD_COUNT);
                    String kernelCount = c.getString(TAG_KERNEL_COUNT);
                    String tutorialCount = c.getString(TAG_TUTORIAL_COUNT);
                    String recognizedDeveloper = c.getString(TAG_DEV);
                    String recognizedThemer = c.getString(TAG_THEMER);
                    String moderator = c.getString(TAG_MODERATOR);
                    String recognizedEliteDeveloper = c.getString(TAG_RDEV);
                    String recognizedContributor = c.getString(TAG_RCOD);



                    HashMap<String, String> map = new HashMap<String, String>();

                    // adding each child node to HashMap key => value
                    map.put(TAG_VTYPE, forumName);
                    map.put(TAG_VCOLOR, score);
                    map.put(TAG_THANKS, totalThanks);
                    map.put(TAG_POSTS, totalPosts);
                    map.put(TAG_JOIN_DATE, joinDate);
                    map.put(TAG_ROM_COUNT, romCount);
                    map.put(TAG_THEME_COUNT, themeCount);
                    map.put(TAG_MOD_COUNT, modCount);
                    map.put(TAG_KERNEL_COUNT, kernelCount);
                    map.put(TAG_TUTORIAL_COUNT, tutorialCount);
                    map.put(TAG_DEV, recognizedDeveloper);
                    map.put(TAG_THEMER, recognizedThemer);
                    map.put(TAG_MODERATOR, moderator);
                    map.put(TAG_RDEV, recognizedEliteDeveloper);
                    map.put(TAG_RCOD, recognizedContributor);


                    jsonlist.add(map);
                } catch (JSONException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            return null;

        }}} 

并将它与我所有值的列表视图和 xml 绑定在一起。结果真的很酷。我在每个值上添加了一个 onclick 以显示更多单独的数据

于 2013-10-16T00:44:33.703 回答