如何在Android Studio中创建列表视图?

如何在Android Studio中创建列表视图?,android,android-activity,android-studio,Android,Android Activity,Android Studio,我正试图为我的Android项目创建一个列表视图,但不幸的是,当我导入这些库时: import android.app.ListActivity; import android.os.Bundle; import android.widget.ArrayAdapter; 上面说这些是“未使用的导入语句”!所以我不能使用“setListAdapter()”函数或其他类似的函数 我的代码是 package irstudents.amirsardari.www.ie_v10; import

我正试图为我的Android项目创建一个列表视图,但不幸的是,当我导入这些库时:

import android.app.ListActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
上面说这些是“未使用的导入语句”!所以我不能使用“setListAdapter()”函数或其他类似的函数

我的代码是

    package irstudents.amirsardari.www.ie_v10;

import android.app.Activity;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.support.v4.widget.DrawerLayout;

import android.widget.ArrayAdapter; //It alerts that are Unused import Satatement
import android.widget.TextView; //It alerts that are Unused import 
import android.app.ListActivity; //It alerts that are Unused import 
import android.os.Bundle; //It alerts that are Unused import 

public class MainActivity extends AppCompatActivity
        implements NavigationDrawerFragment.NavigationDrawerCallbacks {

    /**
     * Fragment managing the behaviors, interactions and presentation of the navigation drawer.
     */
    private NavigationDrawerFragment mNavigationDrawerFragment;

    /**
     * Used to store the last screen title. For use in {@link #restoreActionBar()}.
     */
    private CharSequence mTitle;

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

        mNavigationDrawerFragment = (NavigationDrawerFragment)
                getSupportFragmentManager().findFragmentById(R.id.navigation_drawer);
        mTitle = getTitle();

        // Set up the drawer.
        mNavigationDrawerFragment.setUp(
                R.id.navigation_drawer,
                (DrawerLayout) findViewById(R.id.drawer_layout));

    }

    @Override
    public void onNavigationDrawerItemSelected(int position) {
        // update the main content by replacing fragments
        FragmentManager fragmentManager = getSupportFragmentManager();
        fragmentManager.beginTransaction()
                .replace(R.id.container, PlaceholderFragment.newInstance(position + 1))
                .commit();
    }

    public void onSectionAttached(int number) {
        switch (number) {
            case 1:
                mTitle = getString(R.string.my_profile);
                break;
            case 2:
                mTitle = getString(R.string.About);
                break;
            case 3:
                mTitle = getString(R.string.action_settings);
                break;
        }
    }

    public void restoreActionBar() {
        ActionBar actionBar = getSupportActionBar();
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
        actionBar.setDisplayShowTitleEnabled(true);
        actionBar.setTitle(mTitle);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        if (!mNavigationDrawerFragment.isDrawerOpen()) {
            // Only show items in the action bar relevant to this screen
            // if the drawer is not showing. Otherwise, let the drawer
            // decide what to show in the action bar.
            getMenuInflater().inflate(R.menu.main, menu);
            restoreActionBar();
            return true;
        }
        return super.onCreateOptionsMenu(menu);
    }

    @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);
    }

    /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment {
        /**
         * The fragment argument representing the section number for this
         * fragment.
         */
        private static final String ARG_SECTION_NUMBER = "section_number";

        /**
         * Returns a new instance of this fragment for the given section
         * number.
         */
        public static PlaceholderFragment newInstance(int sectionNumber) {
            PlaceholderFragment fragment = new PlaceholderFragment();
            Bundle args = new Bundle();
            args.putInt(ARG_SECTION_NUMBER, sectionNumber);
            fragment.setArguments(args);
            return fragment;
        }

        public PlaceholderFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_main, container, false);
            return rootView;
        }

        @Override
        public void onAttach(Activity activity) {
            super.onAttach(activity);
            ((MainActivity) activity).onSectionAttached(
                    getArguments().getInt(ARG_SECTION_NUMBER));
        }
    }

}
CustomAdapter类:-

import android.content.Context;
import android.content.res.Resources;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;


public class CustomAdapter extends BaseAdapter
{

    Context context;
    String[] rooms;
    public CustomAdapter(Context c)
    {
        context = c;
        Resources res = c.getResources();
        rooms = res.getStringArray(R.array.images);
    }
    @Override
    public int getCount() {
        return rooms.length;
    }

    @Override
    public Object getItem(int position) {
        return rooms[position];
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view =  inflater.inflate(R.layout.single_row,parent,false);
        TextView txt = (TextView) view.findViewById(R.id.txt_item);
        String temp = rooms[position];
        txt.setText(temp);
        return view;
    }
}
主要活动类别:-

public class MainActivity extends ActionBarActivity
{


    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ListView listView = (ListView) findViewById(R.id.listView);
        listView.setAdapter(new CustomAdapter(this));
     }

}
将以下内容添加到string.xml中:-

<string-array name="images">
        <item>Living Room</item>
        <item>Master Bedroom</item>
        <item>Children Bedroom</item>
        <item>Kitchen1</item>
        <item>Kitchen2</item>
        <item>Gallery</item>
</string-array>

客厅
主卧
儿童卧室
厨房1
厨房2
画廊
将以下内容添加到您的activity_main.xml中:-

<ListView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:dividerHeight="0.1dp"
    android:divider="#666666"
    android:id="@+id/listView" />

single_row.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">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:textSize="20sp"
        android:gravity="start"
        android:text="Large Text"
        android:typeface="serif"
        android:id="@+id/txt_item"
        android:padding="3dp"
        android:paddingLeft="5dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />
</RelativeLayout>


您不需要导入android.app.ListActivity或使用扩展ListActivity是的,使用公共类MainActivity扩展ListActivity而不是AppCompatiActivity在android studio中自动导入请参见:并且我从未在package name中看到下划线:-)导入列表视图而不是appcomapct activity使用列表activity具有讽刺意味的是,因为你应该发布一行带解释的代码!如果你读了上面的评论,你就会知道OP试图导入一些他应该实际扩展的东西,这表明OP对android来说是非常新的,因此几行解释性代码是不够的。当你得到有效的代码时,然后他/她会逐行分析代码,如果OP仍然不理解代码,那么我们会在这里提供帮助。这是你的方法,但我认识的许多人比英语解释更了解代码。无论如何,让我们同意不同意!