自定义ArrayAdapter和自定义布局

时间:2018-02-16 03:53:19

标签: android-layout android-fragments android-arrayadapter

我正在从udacity在线学习android编程,这个讲座非常陈旧,它在Android Studio 1.0中,我使用的是ver 3.0.1,而且代码不起作用,所以我试图使用该代码移动改变一些事情,但卡住了。

问题,我面临的是我正在尝试自定义ArrayAdapter并尝试在其中获取自定义xml布局,但我无法在列表中找到我的xml的名称。这是目前为止的代码。

Java文件:CategoryAdapter.java,MainActivity.java,MainFragment.java

布局文件:activity_main.xml,fragment_main.xml,list_item_forecast.xml。

CategoryAdapter

package com.example.android.sunshine;

import android.content.Context;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;

/**
* Provides the appropriate {@link Fragment} for a view pager.
*/
public class CategoryAdapter extends FragmentPagerAdapter {

/**
 * Create a new {@link CategoryAdapter} object.
 *
 * @param mainActivity
 * @param fm is the fragment manager that will keep each fragment's state in the adapter
 */

    public CategoryAdapter(MainActivity mainActivity, FragmentManager fm) {
        super(fm);
    }

/**
 * Return the {@link Fragment} that should be displayed for the given page number.
 */

    @Override
    public Fragment getItem(int position) {
        return new MainFragment();
    }

/**
 * Return the total number of pages.
 */

    @Override
    public int getCount() {
        return 1;
    }
}

MainActivity

package com.example.android.sunshine;

import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    // Set the content of the activity to use the activity_main.xml layout file
        setContentView(R.layout.activity_main);

    // Find the view pager that will allow the user to swipe between fragments
        ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);

    // Create an adapter that knows which fragment should be shown on each page
        CategoryAdapter adapter = new CategoryAdapter(this, getSupportFragmentManager());

    // Set the adapter onto the view pager
        viewPager.setAdapter(adapter);
    }
}

MainFragment

package com.example.android.sunshine;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import com.example.android.sunshine.R.layout;
import com.example.android.sunshine.MainFragment;

/**
* A simple {@link Fragment} subclass.
*/
public class MainFragment extends Fragment {

    ArrayAdapter<String> mForecastAdapter;

    public MainFragment() {
    // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
        String[] forecastArry = {
            "Today - Sunny - 88/63",
            "Tomorrow - Foggy - 70/40",
            "Wednesday - Cloudy - 72/63",
            "Thursday - Asteroids - 75/65",
            "Friday - Heavy Rain - 65/56",
            "Saturday - Help Trapped In WeatherStation - 60/51",
            "Sunday - Sunny - 80/68"
        };
        List<String> weekForecast = new ArrayList<String>(
            Arrays.asList(forecastArry));

        mForecastAdapter = new ArrayAdapter<String>(
            getContext(),
            R.layout.list_item_forecast,
            R.id.list_item_forecast_textview,
            weekForecast);

        return inflater.inflate(R.layout.fragment_main, container, false);
    }
}

这是我正在解决问题的地方 mForecastAdapter = new ArrayAdapter(getContext(),R.layout.list_item_forecast,R.id.list_item_forecast_textview,weekForecast); 我在任何地方都看不到list_item_forecast而且它是红色的。当我点击它时,它说创建xml,当我说是,那么它说已经存在。

这是我的xml布局代码

activity_main.xml中

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 
    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"
    tools:context="com.example.android.sunshine.MainActivity">

    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</android.support.constraint.ConstraintLayout>

fragment_main.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="16dp"
    android:paddingRight="16dp"
    android:paddingTop="16dp"
    android:paddingBottom="16dp"
    tools:context=".MainActivity">

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

 </FrameLayout>

list_item_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" />

在这里你可以看到list_item_forecast root是TextView。

1 个答案:

答案 0 :(得分:2)

尝试重建项目或创建新项目并执行相同操作。因为我对Android Studio 3.0.1做了同样的事情,并没有收到任何错误。

相关问题