如何让主页上的按钮更改为其他视图?

时间:2016-03-27 20:57:13

标签: java android xml android-layout rss

代码使用rss feed解析它,然后使用arrayadapter将其显示在链表中,我试图将其链接到其他视图,然后在单击按钮时切换到视图Feed将显示为列表。

onCreate方法:

    public void onCreate(Bundle savedInstanceState) 
    {


    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    final Button button = (Button) findViewById(R.id.current);
    button.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            setContentView(R.layout.current);
            Log.e("Layout= ","Current");
        }
    });
    try
    { 
        setContentView(R.layout.current);
        listv = (ListView) findViewById(R.id.lvst);
        // Get the data from the XML stream as a string
        result =  sourceListingString(sourceListingURL);

        // Do some processing of the data to get the individual parts of the       XML stream
        // At some point put this processing into a separate thread of execution
        RoadWorkslisting = parseData(result);
        // Display the string in the TextView object just to demonstrate this capability
        ArrayAdapter<RoadWorks> arrayAdapter = new ArrayAdapter<RoadWorks>(this, android.R.layout.simple_list_item_1,RoadWorkslisting);
        listv.setAdapter(arrayAdapter);
        setContentView(R.layout.main);
    }
    catch(IOException ae)
    {
        // Handle error
        response.setText("Error");
        // Add error info to log for diagnostics
        errorText.setText(ae.toString());
    } 

   final Button button2 = (Button) findViewById(R.id.planned);
    button2.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            setContentView(R.layout.planned);
            Log.e("Layout= ","Planned");
        }
    });


            try
                {

                setContentView(R.layout.planned);
                listv2 = (ListView) findViewById(R.id.lvst2);

                    // Get the data from the XML stream as a string
                    result2 =  sourceListingString2(sourceListingURL2);

                        // Do some processing of the data to get the individual parts of the XML stream
                        // At some point put this processing into a separate thread of execution
                        PlannedRoadWorkslisting = parseData2(result2);
                            // Display the string in the TextView object just to demonstrate this capability
                            ArrayAdapter<PlannedRoadWorks> arrayAdapter2 = new ArrayAdapter<PlannedRoadWorks>(this, android.R.layout.simple_list_item_1, PlannedRoadWorkslisting);
                                listv2.setAdapter(arrayAdapter2);
                                setContentView(R.layout.main);
                }
                catch(IOException ae)
                    {
                        // Handle error
                        response.setText("Error");
                            // Add error info to log for diagnostics
                        errorText.setText(ae.toString());
                    } 
            }

main.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="wrap_content"
    android:background="@color/cyan"
    android:orientation="vertical" >

    <Button
        android:id="@+id/planned"
        android:layout_width="match_parent"
        android:layout_height="70dp"
        android:text="@string/planned" />

    <Button
        android:id="@+id/current"
        android:layout_width="match_parent"
        android:layout_height="70dp"
        android:text="@string/current" />


    </LinearLayout>

的current.xml

<?xml version="1.0" encoding="utf-8"?>
<ListView
android:id="@+id/lvst"
 xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >

</ListView>

Planned.xml         

<ListView
android:id="@+id/lvst2" 
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<!-- lvst is the planned roadworks -->
</ListView>

0 个答案:

没有答案