数据绑定帮助Android初学者

时间:2015-11-30 06:06:58

标签: android data-binding

我正在遵循我的朋友给我的Udacity教程。我觉得这些东西有点旧,因为我现在在android工作室上有很多新选项。

以下是我的代码

主要活动类

    package app.com.example.sheraz.sunshinev_2;

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
    public static class PlaceholderFragment extends Fragment {

        public PlaceholderFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.content_main, container, false);
            String[] foreCastArray = {
                    "Today - Sunny - 86/63",
                    "Tomorrow - Cloudy - 88/73",
                    "Wednesday - Rainy - 98/50",
                    "Thursday - Foggy - 86/63",
                    "Friday - Cloudy - 86/63",
                    "Saturday - Sunny - 86/63",
                    "Sunday - Sunny - 86/63",
            };

            List<String> weekForecast = new ArrayList<String>(Arrays.asList(foreCastArray));

            ArrayAdapter<String> forecastAdapter = new ArrayAdapter<String>(
                    getActivity(), //Context Activity or Touch Activity.
                    R.layout.list_item_forecast,  // Layout file Made for this adapter
                    R.id.list_item_forecast_textview, // Layout id to register with this adapter in above file.
                    weekForecast //Data
            );

            //List view for each data to be populated
            ListView listView = (ListView) rootView.findViewById(R.id.listview_forecast);
            //Attaching to adapter
            listView.setAdapter(forecastAdapter);

            //Returns Views after data is populated.
            return rootView;
        }
    }
}

activitymain.xml我没有对此进行任何更改。

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context="app.com.example.sheraz.sunshinev_2.MainActivity">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

    </android.support.design.widget.AppBarLayout>

    <include layout="@layout/content_main" />

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="@dimen/fab_margin"
        android:src="@android:drawable/ic_dialog_email" />

</android.support.design.widget.CoordinatorLayout>

context.xml中

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="app.com.example.sheraz.sunshinev_2.MainActivity"
    tools:showIn="@layout/activity_main">

    <ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/listview_forecast"
        />
</FrameLayout>

最后。物料清单forecast.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:minHeight="?android:attr/listPreferredItemHeight"
    android:gravity="center_vertical"
    android:id="@+id/list_item_forecast_textview"
    android:padding="20dp"
    android:textSize="22sp"
    android:textStyle="bold"
    android:text="Tomorrow - Cloudy - 88/73"

    >

</TextView>

我无法将数据绑定到我的应用程序,我从头开始尝试了3次教程,但我似乎无法知道,我缺少什么。我对android很新,所以很多人都会感激不尽。

2 个答案:

答案 0 :(得分:0)

请阅读适配器以绑定数据。

Android提供了几个适配器子类,可用于检索不同类型的数据并构建AdapterView的视图(即ListViewGridView)。常见的适配器有ArrayAdapterBase AdapterCursorAdapterSimpleCursorAdapterSpinnerAdapterWrapperListAdapter

最简单的是Arrayadapter

String[] mobileArray = {"Android","IPhone","WindowsMobile","Blackberry","WebOS","Ubuntu","Windows7","Max OS X"};

ArrayAdapter adapter = new ArrayAdapter<String>(this, R.layout.activity_listview, mobileArray);  

ListView listView = (ListView) findViewById(R.id.mobile_list);

listView.setAdapter(adapter);

答案 1 :(得分:0)

诀窍是理解新的片段系统以前我有三个片段,我使用了错误的API,API 13是姜饼,并不支持碎片活动。

使用API​​ 15或更高版本,代码可以正常运行。我猜错了。