Android的传递JSON数据到列表视图 [英] android passing json data into listview

查看:190
本文介绍了Android的传递JSON数据到列表视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我真的不熟悉,当谈到的ListView 和列表适配器,所以我要在这里需要一点点帮助。我需要有人来告诉我什么是错我的code和也我能做些什么来解决这个问题。当我写这篇code我是新来的的ListView 所以我道歉,如果我的code太疯狂了。

I'm really not to familiar when it comes to ListView and list adapters so I am going to need a little help here. I need somebody to tell me what is wrong with my code and also what I can do to fix this problem. When I wrote this code I was new to the ListView so I apologize if my code seems crazy.

主要活动 code:

public class MainActivity extends Activity {

    EditText et;
    Button getanswer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        Button getanswer = (Button) findViewById(R.id.button1);
        getanswer.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            new asynctask().execute();
            Intent i = new Intent(MainActivity.this, ListView.class);
            startActivity(i);
        }

        });
    } 
}

      class asynctask extends AsyncTask<String, Void, JSONObject> {

             private static final String TAG_RESULTS = "ResultsSet";
             private static final String TAG_SUBJECT = "Subject";
             private static final String TAG_NUMANSWERS = "NumAnswers";
             private static final String TAG_QUESTION = "Question";
             private static final String TAG_QUESTION_CONTENT = "Content";
             private static final String TAG_QUESTION_CHOSENANSWER = "ChosenAnswer";
             private static final String TAG_QUESTION_ANSWERS = "Answers";
            public JSONArray json;

         @Override
         protected JSONObject doInBackground(String... params) {

             JSONParsser jParser = new JSONParsser();

             String URI = "http://answers.yahooapis.com/AnswerService/V1/questionSearch?appid=dF39jyvV34FlZGhhMRBNHhILlsMJWLgBhxp4YrXImT2EOFprfUK3f4w2eGKjC5TC0A&query=sort&type=resolved&results&output=json";

             JSONObject json = jParser.getJSONFromURI(URI);

             return json ;

         }{ }
             @Override
             protected void onPostExecute(JSONObject json) {

                 JSONArray ResultsSet = null; {
                 try {
                     ResultsSet = json.getJSONArray(TAG_RESULTS);
                } catch (JSONException e) {
                   e.printStackTrace();
             }

            return;
        }}}

的ListView code:

public class ListView extends ListActivity {

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


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

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

        String TAG_SUBJECT = "Subject";
        String TAG_NUMANSWERS = "NumAnswers";

        ResultsSet.add(map);


        ListAdapter adapter = new SimpleAdapter(this, ResultsSet,
                R.layout.listview,
                 new String[] { TAG_SUBJECT, TAG_NUMANSWERS }, new int[] {
                R.id.Subject, R.id.NumAnswers });


        setListAdapter(adapter);
    }
}

ListView.xml布局文件

ListView.xml layout file

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/Subject"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:padding="10dip"
        android:textSize="16dip"
        android:textStyle="bold" >

    <TextView
        android:id="@+id/NumAnswers"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:padding="10dip"
        android:textSize="16dip"
        android:textStyle="bold" >

    </TextView>

    <ScrollView
        android:id="@+id/scrollView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" >
        </LinearLayout>
    </ScrollView>

    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </ListView>

</TextView>

这是我的堆栈跟踪它的pretty长

here is my stacktrace it's pretty long

07-25 12:27:45.055: E/AndroidRuntime(1356): FATAL EXCEPTION: main
07-25 12:27:45.055: E/AndroidRuntime(1356): java.lang.ClassCastException: android.widget.TextView cannot be cast to android.view.ViewGroup
07-25 12:27:45.055: E/AndroidRuntime(1356):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:747)
07-25 12:27:45.055: E/AndroidRuntime(1356):     at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
07-25 12:27:45.055: E/AndroidRuntime(1356):     at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
07-25 12:27:45.055: E/AndroidRuntime(1356):     at android.widget.SimpleAdapter.createViewFromResource(SimpleAdapter.java:121)
07-25 12:27:45.055: E/AndroidRuntime(1356):     at android.widget.SimpleAdapter.getView(SimpleAdapter.java:114)
07-25 12:27:45.055: E/AndroidRuntime(1356):     at android.widget.AbsListView.obtainView(AbsListView.java:2159)
07-25 12:27:45.055: E/AndroidRuntime(1356):     at android.widget.ListView.makeAndAddView(ListView.java:1831)
07-25 12:27:45.055: E/AndroidRuntime(1356):     at android.widget.ListView.fillDown(ListView.java:674)
07-25 12:27:45.055: E/AndroidRuntime(1356):     at android.widget.ListView.fillFromTop(ListView.java:735)
07-25 12:27:45.055: E/AndroidRuntime(1356):     at android.widget.ListView.layoutChildren(ListView.java:1652)
07-25 12:27:45.055: E/AndroidRuntime(1356):     at android.widget.AbsListView.onLayout(AbsListView.java:1994)
07-25 12:27:45.055: E/AndroidRuntime(1356):     at android.view.View.layout(View.java:14008)
07-25 12:27:45.055: E/AndroidRuntime(1356):     at android.view.ViewGroup.layout(ViewGroup.java:4373)
07-25 12:27:45.055: E/AndroidRuntime(1356):     at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
07-25 12:27:45.055: E/AndroidRuntime(1356):     at android.view.View.layout(View.java:14008)
07-25 12:27:45.055: E/AndroidRuntime(1356):     at android.view.ViewGroup.layout(ViewGroup.java:4373)
07-25 12:27:45.055: E/AndroidRuntime(1356):     at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1663)
07-25 12:27:45.055: E/AndroidRuntime(1356):     at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1521)
07-25 12:27:45.055: E/AndroidRuntime(1356):     at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
07-25 12:27:45.055: E/AndroidRuntime(1356):     at android.view.View.layout(View.java:14008)
07-25 12:27:45.055: E/AndroidRuntime(1356):     at android.view.ViewGroup.layout(ViewGroup.java:4373)
07-25 12:27:45.055: E/AndroidRuntime(1356):     at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
07-25 12:27:45.055: E/AndroidRuntime(1356):     at android.view.View.layout(View.java:14008)
07-25 12:27:45.055: E/AndroidRuntime(1356):     at android.view.ViewGroup.layout(ViewGroup.java:4373)
07-25 12:27:45.055: E/AndroidRuntime(1356):     at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1892)
07-25 12:27:45.055: E/AndroidRuntime(1356):     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1711)
07-25 12:27:45.055: E/AndroidRuntime(1356):     at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:989)
07-25 12:27:45.055: E/AndroidRuntime(1356):     at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4351)
07-25 12:27:45.055: E/AndroidRuntime(1356):     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
07-25 12:27:45.055: E/AndroidRuntime(1356):     at android.view.Choreographer.doCallbacks(Choreographer.java:562)
07-25 12:27:45.055: E/AndroidRuntime(1356):     at android.view.Choreographer.doFrame(Choreographer.java:532)
07-25 12:27:45.055: E/AndroidRuntime(1356):     at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
07-25 12:27:45.055: E/AndroidRuntime(1356):     at android.os.Handler.handleCallback(Handler.java:725)
07-25 12:27:45.055: E/AndroidRuntime(1356):     at android.os.Handler.dispatchMessage(Handler.java:92)
07-25 12:27:45.055: E/AndroidRuntime(1356):     at android.os.Looper.loop(Looper.java:137)
07-25 12:27:45.055: E/AndroidRuntime(1356):     at android.app.ActivityThread.main(ActivityThread.java:5041)
07-25 12:27:45.055: E/AndroidRuntime(1356):     at java.lang.reflect.Method.invokeNative(Native Method)
07-25 12:27:45.055: E/AndroidRuntime(1356):     at java.lang.reflect.Method.invoke(Method.java:511)
07-25 12:27:45.055: E/AndroidRuntime(1356):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
07-25 12:27:45.055: E/AndroidRuntime(1356):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
07-25 12:27:45.055: E/AndroidRuntime(1356):     at dalvik.system.NativeStart.main(Native Method)

对不起,不问一个问题日志堆栈跟踪的正确方法和抱歉,但我只是想知道为什么我得到这些错误,怎么可以prevent它。

Sorry for not asking a question the proper way and sorry for the log stacktrace but I just want to know why am I getting these errors and how can I prevent it.

推荐答案

您例外无关用JSON解析,它与布局的ListView你做。我的猜测是某处布局文件,你有这样的事情

Your exception has nothing to do with JSON parsing, it has to do with the Layout for your ListView. My guess is that somewhere in your Layout file you have something like this

<TextView
  <!-- params -->
  >
  <!-- More Stuff -->
</TextView>

由于TextView中不能有它内部的任何其他意见那是不可能的。使在布局 R.Layout.listview A 的ViewGroup 的LinearLayout ,然后应用codeMagic的有关请求和解析建议。

That is not possible because TextView cannot have any other views inside of it. Make the layout in R.Layout.listview a ViewGroup such as a LinearLayout and then apply codeMagic's suggestions about requesting and parsing.

编辑:我这个编辑第二次,因为我没有读到的布局非常接近第一次。根本的问题是,你有一个的TextView 这是你的顶级元素,它包含在它的其他元素。然后,在通胀的时候,LayoutInflater试图夸大你的布局(不要担心这是如何发生),它是看到嘿,这个顶层布局有其内其他孩子,让我将其转换为一个的ViewGroup 因此我可以添加的孩子。然而,的TextView 不是一个视图组,没有孩子,让 ClassCastException异常被抛出。

I'm editing this a second time because I didn't read that layout close enough the first time. The underlying problem is that you have a TextView which is your top level element and contains other elements within it. Then, at inflation time, the LayoutInflater is trying to inflate your layout (don't worry about how this happens), and it is seeing "oh hey, this top level layout has other children within it, let me cast it to a ViewGroup so I can add children. However, a TextView is not a view group and has no children, so that ClassCastException gets thrown.

更重要的是,它好像你不完全了解列表上的适配器是如何工作的。你需要有一个单独的布局,其中包含如何在 ListView的任何一个项目查找信息;那么你需要映射到该布局中引用TextViews。当您安装适配器时,的ListView 中的每一个项目膨胀您在 setAdapter 通话指出,较小的布局和设置的TextView 在那只小布局。然后,这些小的布局被放置在另一个的顶部的一个,使列表中。

More importantly, it seems like you don't fully understand how Adapters on lists work. You need to have a separate layout which contains information on how any one item in the ListView looks; then your mapping needs to reference TextViews within THAT layout. When you attach the adapter, every item within the ListView inflates the smaller layout that you pointed to in your setAdapter call and sets the TextViews in that smaller layout. These smaller layouts are then placed one on top of the other to make your list.

这篇关于Android的传递JSON数据到列表视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆