无法在android中设置适配器两个列表

时间:2015-01-18 07:53:12

标签: java android xml listview

我创建了一个xml文件,其中显示了两个列表视图,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#fff" >

<LinearLayout
    android:id="@+id/top_layover"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:background="@drawable/blue_gradient"
    android:orientation="horizontal" >

    <TextView
        style="@style/BlackText"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:gravity="center"
        android:text="זמני יום חול"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</LinearLayout>

    <LinearLayout
    android:id="@+id/top_layover2"
    android:layout_below="@id/top_layover"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_marginTop="5dp"
    android:background="@drawable/blue_gradient"
    android:orientation="horizontal" >

    <TextView
        style="@style/BlackText"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:gravity="center"
        android:text="ראשון עד חמישי"
        android:textSize="16sp"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</LinearLayout>

    <LinearLayout 
        android:id="@+id/lists"
        android:layout_below="@+id/top_layover2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:weightSum="100"
        android:orientation="vertical">

    <LinearLayout 
        android:id="@+id/list1_la"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="40"
        >

<ListView
    android:id="@+id/weeklylist"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"

    android:background="#fff"
    android:divider="@android:color/transparent"
    android:scrollbars="none" />

</LinearLayout>

        <LinearLayout
    android:id="@+id/bottom_layover"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/list1_la"
    android:layout_alignParentLeft="true"
    android:layout_marginTop="5dp"
    android:background="@drawable/blue_gradient"
    android:orientation="horizontal"
    android:layout_weight="1" >

    <TextView
        style="@style/BlackText"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:gravity="center"
        android:text="יום שישי"
        android:textSize="16sp"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</LinearLayout>

        <LinearLayout 
        android:id="@+id/list2_la"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/bottom_layover"
        android:layout_weight="59">

<ListView
    android:id="@+id/weeklylist2"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"

    android:background="#fff"
    android:divider="@android:color/transparent"
    android:scrollbars="none" />

</LinearLayout>

我试图设置我在java类中创建的适配器,但每次我启动应用程序崩溃的活动时都会:

package com.example.neotavraham;

import java.util.ArrayList;
import java.util.HashMap;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.example.neotavraham.Messages.LoadComments;

import android.app.ListActivity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.AdapterView.OnItemClickListener;

public class WeeklyPrays extends ListActivity{

    ListView lv;

// Progress Dialog
    private ProgressDialog pDialog;

    //php read comments script

    //localhost :  
    //testing on your device
    //put your local ip instead,  on windows, run CMD > ipconfig
    //or in mac's terminal type ifconfig and look for the ip under en0 or en1
   // private static final String READ_COMMENTS_URL = "http://xxx.xxx.x.x:1234/webservice/comments.php";

    //testing on Emulator:
    private static final String READ_WEEKLY_PRAYS_URL = "http://www.neotavraham.netne.net/webservice/weeklyprays.php";

  //testing from a real server:
    //private static final String READ_COMMENTS_URL = "http://www.mybringback.com/webservice/comments.php";

  //JSON IDS:
    private static final String TAG_SUCCESS = "success";
    private static final String TAG_PRAY = "pray";
    private static final String TAG_TIME = "time";
    private static final String TAG_PLACE = "place";
    private static final String TAG_POSTS = "posts";
    private static final String TAG_POST_ID = "post_id";

    //it's important to note that the message is both in the parent branch of 
    //our JSON tree that displays a "Post Available" or a "No Post Available" message,
    //and there is also a message for each individual post, listed under the "posts"
    //category, that displays what the user typed as their message.


   //An array of all of our comments
    private JSONArray mComments = null;
    //manages all of our comments in a list.
    private ArrayList<HashMap<String, String>> mCommentList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //note that use read_comments.xml instead of our single_post.xml
        setContentView(R.layout.read_weekly_times);


        lv = (ListView) findViewById(R.id.weeklylist);
    }

    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        //loading the comments via AsyncTask
        new LoadWeeklyPrays().execute();

    }

     /**
         * Retrieves recent post data from the server.
         */
        public void updateJSONdata() {

            // Instantiate the arraylist to contain all the JSON data.
            // we are going to use a bunch of key-value pairs, referring
            // to the json element name, and the content, for example,
            // message it the tag, and "I'm awesome" as the content..

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

            // Bro, it's time to power up the J parser 
            JSONParser jParser = new JSONParser();
            // Feed the beast our comments url, and it spits us
            //back a JSON object.  Boo-yeah Jerome.
            JSONObject json = jParser.getJSONFromUrl(READ_WEEKLY_PRAYS_URL);

            //when parsing JSON stuff, we should probably
            //try to catch any exceptions:
            try {

                //I know I said we would check if "Posts were Avail." (success==1)
                //before we tried to read the individual posts, but I lied...
                //mComments will tell us how many "posts" or comments are
                //available
                mComments = json.getJSONArray(TAG_POSTS);


                // looping through all posts according to the json object returned
                for (int i = 0; i < mComments.length(); i++) {

                    JSONObject c = mComments.getJSONObject(i);

                    //gets the content of each tag
                    String pray = c.getString(TAG_PRAY);
                    String time = c.getString(TAG_TIME);
                    String place = c.getString(TAG_PLACE);


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

                    map.put(TAG_PRAY, pray);
                    map.put(TAG_TIME, time);
                    map.put(TAG_PLACE, place);




                    // adding HashList to ArrayList
                    mCommentList.add(map);

                    //annndddd, our JSON data is up to date same with our array list
                }

            } catch (JSONException e) {
                e.printStackTrace();
            }
        }

    /**
         * Inserts the parsed data into the listview.
         */
        private void updateList() {
            // For a ListActivity we need to set the List Adapter, and in order to do
            //that, we need to create a ListAdapter.  This SimpleAdapter,
            //will utilize our updated Hashmapped ArrayList, 
            //use our single_post xml template for each item in our list,
            //and place the appropriate info from the list to the
            //correct GUI id.  Order is important here.
            ListAdapter adapter = new SimpleAdapter(this, mCommentList,
                    R.layout.single_time_box, new String[] { TAG_PRAY, TAG_TIME,TAG_PLACE
                             }, new int[] { R.id.pray, R.id.pray_time,R.id.place});

            // I shouldn't have to comment on this one:

            lv.setAdapter(adapter);
            // Optional: when the user clicks a list item we 
            //could do something.  However, we will choose
            //to do nothing...
            /*ListView lv = getListView();

            lv.setOnItemClickListener(new OnItemClickListener() {

                @Override
                public void onItemClick(AdapterView<?> parent, View view,
                        int position, long id) {

                    // This method is triggered if an item is click within our
                    // list. For our example we won't be using this, but
                    // it is useful to know in real life applications.

                }
            });*/
        }    

    public class LoadWeeklyPrays extends AsyncTask<Void, Void, Boolean> {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(WeeklyPrays.this);
            pDialog.setMessage("Loading weekly prays...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(true);
            pDialog.show();
        }
        @Override
        protected Boolean doInBackground(Void... arg0) {
            //we will develop this method in version 2
            updateJSONdata();

            return null;

        }


        @Override
        protected void onPostExecute(Boolean result) {
            super.onPostExecute(result);
            pDialog.dismiss();
          //we will develop this method in version 2
            updateList();
        }
    }

  }

这是logCat:

01-18 10:02:26.531: W/ApplicationPackageManager(3788): getCSCPackageItemText()
01-18 10:02:26.601: E/MoreInfoHPW_ViewGroup(3788): Parent view is not a TextView
01-18 10:02:26.801: D/OpenGLRenderer(3788): Enabling debug mode 0
01-18 10:02:34.531: W/ApplicationPackageManager(3788): getCSCPackageItemText()
01-18 10:02:34.546: E/MoreInfoHPW_ViewGroup(3788): Parent view is not a TextView
01-18 10:02:34.571: D/AbsListView(3788): Get MotionRecognitionManager
01-18 10:02:34.586: D/AbsListView(3788): Get MotionRecognitionManager
01-18 10:02:34.591: D/AndroidRuntime(3788): Shutting down VM
01-18 10:02:34.591: W/dalvikvm(3788): threadid=1: thread exiting with uncaught exception (group=0x41825c08)
01-18 10:02:34.596: E/AndroidRuntime(3788): FATAL EXCEPTION: main
01-18 10:02:34.596: E/AndroidRuntime(3788): Process: com.example.neotavraham, PID: 3788
01-18 10:02:34.596: E/AndroidRuntime(3788): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.neotavraham/com.example.neotavraham.WeeklyPrays}: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
01-18 10:02:34.596: E/AndroidRuntime(3788):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2305)
01-18 10:02:34.596: E/AndroidRuntime(3788):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2363)
01-18 10:02:34.596: E/AndroidRuntime(3788):     at android.app.ActivityThread.access$900(ActivityThread.java:161)
01-18 10:02:34.596: E/AndroidRuntime(3788):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1265)
01-18 10:02:34.596: E/AndroidRuntime(3788):     at android.os.Handler.dispatchMessage(Handler.java:102)
01-18 10:02:34.596: E/AndroidRuntime(3788):     at android.os.Looper.loop(Looper.java:157)
01-18 10:02:34.596: E/AndroidRuntime(3788):     at android.app.ActivityThread.main(ActivityThread.java:5356)
01-18 10:02:34.596: E/AndroidRuntime(3788):     at java.lang.reflect.Method.invokeNative(Native Method)
01-18 10:02:34.596: E/AndroidRuntime(3788):     at java.lang.reflect.Method.invoke(Method.java:515)
01-18 10:02:34.596: E/AndroidRuntime(3788):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
01-18 10:02:34.596: E/AndroidRuntime(3788):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
01-18 10:02:34.596: E/AndroidRuntime(3788):     at dalvik.system.NativeStart.main(Native Method)
01-18 10:02:34.596: E/AndroidRuntime(3788): Caused by: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
01-18 10:02:34.596: E/AndroidRuntime(3788):     at android.app.ListActivity.onContentChanged(ListActivity.java:243)
01-18 10:02:34.596: E/AndroidRuntime(3788):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java)
01-18 10:02:34.596: E/AndroidRuntime(3788):     at android.app.Activity.setContentView(Activity.java:2010)
01-18 10:02:34.596: E/AndroidRuntime(3788):     at com.example.neotavraham.WeeklyPrays.onCreate(WeeklyPrays.java:68)
01-18 10:02:34.596: E/AndroidRuntime(3788):     at android.app.Activity.performCreate(Activity.java:5426)
01-18 10:02:34.596: E/AndroidRuntime(3788):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
01-18 10:02:34.596: E/AndroidRuntime(3788):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2269)
01-18 10:02:34.596: E/AndroidRuntime(3788):     ... 11 more
01-18 10:03:05.831: I/Process(3788): Sending signal. PID: 3788 SIG: 9

我可以看到它说:01-18 10:02:34.596: E/AndroidRuntime(3788): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.neotavraham/com.example.neotavraham.WeeklyPrays}: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'但为什么?如何使用具有不同名称的列表视图?

updateList()函数中我正在执行lv.setAdapter(adapter)但由于某种原因它无效。我有另一个活动,只有一个列表视图,所以我正在做lv = getListView(),它工作正常..但我不想在此活动中使用此功能......

0 个答案:

没有答案