Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/307.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/178.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java ListView CustomAdapter不显示第一个位置结果_Java_Android_Listview_Custom Adapter - Fatal编程技术网

Java ListView CustomAdapter不显示第一个位置结果

Java ListView CustomAdapter不显示第一个位置结果,java,android,listview,custom-adapter,Java,Android,Listview,Custom Adapter,我是Android Studio的新手,通常都会进行编码,我正在尝试显示一个带有颜色图像和颜色名称的自定义列表视图。我使用了一个自定义适配器,用两个数组来扩展视图,其中包含可绘制的图像颜色和名称字符串 我遇到的问题-自定义适配器仅显示从位置1开始的图层。例如,第一层应为img_01和“红色”-显示img_02和“橙色” 我试过调试代码,虽然适配器正在设置位置0的值,但我找不到它不显示的原因(因为其他适配器显示得很好) 图像和名称只是占位符,因此不包括可绘制内容和名称的替代解决方案是不可行的 先谢

我是Android Studio的新手,通常都会进行编码,我正在尝试显示一个带有颜色图像和颜色名称的自定义列表视图。我使用了一个自定义适配器,用两个数组来扩展视图,其中包含可绘制的图像颜色和名称字符串

我遇到的问题-自定义适配器仅显示从位置1开始的图层。例如,第一层应为img_01和“红色”-显示img_02和“橙色”

我试过调试代码,虽然适配器正在设置位置0的值,但我找不到它不显示的原因(因为其他适配器显示得很好)

图像和名称只是占位符,因此不包括可绘制内容和名称的替代解决方案是不可行的

先谢谢你

CustomAdapter.java

public class CustomAdapter extends BaseAdapter {

    Context context;
    int[] images;
    String[] colour;
    LayoutInflater mInflater;

    public CustomAdapter(Context c, int[] i, String[] col) {
        this.context = c; //sets the application context that has been passed to the adapter
        this.images = i; //sets the images array that has been passed to the adapter
        this.colour = col; //sets the colour array that has been passed to the adapter
        mInflater = (LayoutInflater.from(context)); //sets the layout inflater from context
    }

    @Override
    public int getCount() { //total number of elements in the array
        return images.length;
    }

    @Override
    public Object getItem(int position) { //gets the item using the position
        return null;
    }

    @Override
    public long getItemId(int position) { //gets the item position
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) { //this is used to update the view

        convertView = mInflater.inflate(R.layout.custom_layout, null);

        ImageView image = convertView.findViewById(R.id.imageView);

        TextView text = convertView.findViewById(R.id.textView);

        text.setText(colour[position]);
        image.setImageResource(images[position]);
        return convertView;

    }
}
MainActivity.java

public class MainActivity extends AppCompatActivity {

    ListView mListView;

    int[] images = {R.drawable.img01,
    R.drawable.img02,
    R.drawable.img03,
    R.drawable.img04};

    String[] colour = {"Red",
    "Orange",
    "Yellow",
    "Green"};

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

        mListView = findViewById(R.id.listView);

        CustomAdapter customAdapter = new CustomAdapter(getApplicationContext(), images, colour);
        //passes information of images and application context to the item adapter
        mListView.setAdapter(customAdapter);
        //sets the adapter to the listview

    }
}
custom_layout.xml

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


    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginStart="7dp"
        android:layout_marginLeft="7dp"
        android:layout_marginTop="9dp"
        app:srcCompat="@drawable/img01" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginStart="99dp"
        android:layout_marginLeft="99dp"
        android:layout_marginTop="36dp"
        android:text="colourName" />

</RelativeLayout>

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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=".MainActivity">

    <ListView
        android:id="@+id/listView"
        android:layout_width="409dp"
        android:layout_height="729dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

您可以使用RecyclerView来完成

  • 创建自定义类
  • 创建XML布局
  • 
    
  • 创建回收器视图适配器
  • 公共类ExampleAdapter扩展了RecyclerView.Adapter{
    私有ArrayList mExampleList;
    公共静态类ExampleViewHolder扩展了RecyclerView.ViewHolder{
    公共图像视图mImageView;
    公共文本视图mTextView1;
    公共示例ViewHolder(视图项视图){
    超级(项目视图);
    mImageView=itemView.findviewbyd(R.id.imageView);
    mTextView1=itemView.findviewbyd(R.id.textView);
    }
    }
    公共示例适配器(ArrayList示例列表){
    mExampleList=示例列表;
    }
    @凌驾
    公共示例ViewHolder onCreateViewHolder(视图组父级,int-viewType){
    视图v=LayoutInflater.from(parent.getContext()).flate(R.layout.example_项,parent,false);
    ExampleViewHolder evh=新的ExampleViewHolder(v);
    返回evh;
    }
    @凌驾
    BindViewHolder上的公共无效(例如ViewHolder,int位置){
    ExampleItem currentItem=MeExampleList.get(位置);
    holder.mImageView.setImageResource(currentItem.getImageResource());
    holder.mTextView1.setText(currentItem.getText1());
    }
    @凌驾
    public int getItemCount(){
    返回mExampleList.size();
    }
    }
    
  • 您的活动_main.xml
  • 
    
  • MainActivity.java
  • public类MainActivity扩展了AppCompatActivity{
    私人回收视图mRecyclerView;
    专用回收器查看适配器mAdapter;
    private RecyclerView.LayoutManager MLLayoutManager;
    @凌驾
    创建时受保护的void(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ArrayList exampleList=新建ArrayList();
    添加(新的示例项(R.drawable.img01,“红色”);
    添加(新的ExampleItem(R.drawable.img02,“橙色”);
    添加(新的示例项(R.drawable.img03,“黄色”);
    添加(新的示例项(R.drawable.img04,“绿色”);
    mRecyclerView=findViewById(R.id.recyclerView);
    mRecyclerView.setHasFixedSize(true);
    mLayoutManager=新的LinearLayoutManager(此);
    mAdapter=新的ExampleAdapter(exampleList);
    mRecyclerView.setLayoutManager(mllayoutmanager);
    mRecyclerView.setAdapter(mAdapter);
    }
    }
    
    您可以使用RecyclerView来完成

  • 创建自定义类
  • 创建XML布局
  • 
    
  • 创建回收器视图适配器
  • 公共类ExampleAdapter扩展了RecyclerView.Adapter{
    私有ArrayList mExampleList;
    公共静态类ExampleViewHolder扩展了RecyclerView.ViewHolder{
    公共图像视图mImageView;
    公共文本视图mTextView1;
    公共示例ViewHolder(视图项视图){
    超级(项目视图);
    mImageView=itemView.findviewbyd(R.id.imageView);
    mTextView1=itemView.findviewbyd(R.id.textView);
    }
    }
    公共示例适配器(ArrayList示例列表){
    mExampleList=示例列表;
    }
    @凌驾
    公共示例ViewHolder onCreateViewHolder(视图组父级,int-viewType){
    视图v=LayoutInflater.from(parent.getContext()).flate(R.layout.example_项,parent,false);
    ExampleViewHolder evh=新的ExampleViewHolder(v);
    返回evh;
    }
    @凌驾
    BindViewHolder上的公共无效(例如ViewHolder,int位置){
    ExampleItem currentItem=MeExampleList.get(位置);
    holder.mImageView.setImageResource(currentItem.getImageResource());
    holder.mTextView1.setText(currentItem.getText1());
    }
    @凌驾
    public int getItemCount(){
    返回mExampleList.size();
    }
    }
    
  • 您的活动_main.xml
  • 
    
  • MainActivity.java
  • public类MainActivity扩展了AppCompatActivity{
    私人回收视图mRecyclerView;
    专用回收器查看适配器mAdapter;
    private RecyclerView.LayoutManager MLLayoutManager;
    @凌驾
    创建时受保护的void(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ArrayList exampleList=新建ArrayList();
    添加(新的示例项(R.drawable.img01,“红色”);
    添加(新的ExampleItem(R.drawable.img02,“橙色”);
    添加(新的示例项(R.drawable.img03,“黄色”);
    添加(新的示例项(R.drawable.img04,“绿色”);
    mRecyclerView=findViewById(R.id.recyclerView);
    mRecyclerView.setHasFixedSize(true);
    mLayoutManager=新的LinearLayoutManager(此);
    mAdapter=新的ExampleAdapter(exampleList);
    mRec
    
    public class ExampleItem {
        private int mImageResource; //your color image
        private String mText1; //your text
    
        public ExampleItem(int imageResource, String text1) {
            mImageResource = imageResource;
            mText1 = text1;
        }
        public int getImageResource() {
            return mImageResource;
        }
        public String getText1() {
            return mText1;
        }
    }
    
    <?xml version="1.0" encoding="utf-8"?>
    <android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="4dp"
        app:cardCornerRadius="4dp">
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_margin="4dp">
            <ImageView
                android:id="@+id/imageView"
                android:layout_width="50dp"
                android:layout_height="50dp"
                android:padding="2dp" />
            <TextView
                android:id="@+id/textView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentTop="true"
                android:layout_toEndOf="@+id/imageView"
                android:text="Line 1"
                android:textColor="@android:color/black"
                android:textSize="20sp"
                android:textStyle="bold" />
        </RelativeLayout>
    </android.support.v7.widget.CardView>
    
    public class ExampleAdapter extends RecyclerView.Adapter<ExampleAdapter.ExampleViewHolder> {
        private ArrayList<ExampleItem> mExampleList;
        public static class ExampleViewHolder extends RecyclerView.ViewHolder {
            public ImageView mImageView;
            public TextView mTextView1;
    
            public ExampleViewHolder(View itemView) {
                super(itemView);
                mImageView = itemView.findViewById(R.id.imageView);
                mTextView1 = itemView.findViewById(R.id.textView);
            }
        }
        public ExampleAdapter(ArrayList<ExampleItem> exampleList) {
            mExampleList = exampleList;
        }
        @Override
        public ExampleViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.example_item, parent, false);
            ExampleViewHolder evh = new ExampleViewHolder(v);
            return evh;
        }
        @Override
        public void onBindViewHolder(ExampleViewHolder holder, int position) {
            ExampleItem currentItem = mExampleList.get(position);
            holder.mImageView.setImageResource(currentItem.getImageResource());
            holder.mTextView1.setText(currentItem.getText1());
        }
        @Override
        public int getItemCount() {
            return mExampleList.size();
        }
    }
    
    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout 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.application.recyclerviewproject.MainActivity">
        <android.support.v7.widget.RecyclerView
            android:id="@+id/recyclerView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@android:color/darker_gray"
            android:padding="4dp"
            android:scrollbars="vertical" />
    </RelativeLayout>
    
    public class MainActivity extends AppCompatActivity {
        private RecyclerView mRecyclerView;
        private RecyclerView.Adapter mAdapter;
        private RecyclerView.LayoutManager mLayoutManager;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            ArrayList<ExampleItem> exampleList = new ArrayList<>();
            exampleList.add(new ExampleItem(R.drawable.img01, "Red"));
            exampleList.add(new ExampleItem(R.drawable.img02, "Orange"));
            exampleList.add(new ExampleItem(R.drawable.img03, "Yellow"));
            exampleList.add(new ExampleItem(R.drawable.img04, "Green"));
    
            mRecyclerView = findViewById(R.id.recyclerView);
            mRecyclerView.setHasFixedSize(true);
            mLayoutManager = new LinearLayoutManager(this);
            mAdapter = new ExampleAdapter(exampleList);
            mRecyclerView.setLayoutManager(mLayoutManager);
            mRecyclerView.setAdapter(mAdapter);
        }
    }
    
    <ListView
        ...
        android:layout_height="wrap_content"/>