无法在Listview中使用CustomAdapter

时间:2017-11-29 08:57:53

标签: java android listview arraylist

我是Android编程新手,我即将创建listview应用,它将显示带有图片,标题,评分和流派的电影列表。 当我运行应用程序时,我看到白色屏幕,这是空的。布局没有问题,我创建了两个布局,一个是activity_main,另一个是{{1} }。

MainActivity.java

custom_layout

CustomAdapter.java

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.ListViewCompat;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import java.util.ArrayList;
import java.util.Arrays;

public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    int[] images = {R.drawable.dfg,R.drawable.godf,R.drawable.godf2,R.drawable.thedark,R.drawable.twelv,R.drawable.shindl};
    String[] movie_titles = getResources().getStringArray(R.array.Movie_titles);
    String[] movie_ratings=getResources().getStringArray(R.array.Movie_ratings);
    String[] movie_janrs=getResources().getStringArray(R.array.Movie_janrs);
    setContentView(R.layout.activity_main);
    ListView mylist = (ListView) findViewById(R.id.list);
    ArrayList<String> rowItems = new ArrayList<>();
    ArrayAdapter<String> myadapter = new ArrayAdapter<>
(this,android.R.layout.simple_list_item_1,rowItems);
    if(mylist!=null){
        mylist.setAdapter(myadapter);
    }
}
}

RowItem.java

import android.content.Context;
import android.support.annotation.IdRes;
import android.support.annotation.LayoutRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
public class CustomAdapter extends ArrayAdapter<RowItem> {


public CustomAdapter(@NonNull Context context, @LayoutRes int resource, @NonNull List<RowItem> objects) {
    super(context, resource, objects);
}

@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
    RowItem rowItem = getItem(position);
    if(convertView ==null){
        LayoutInflater vi = LayoutInflater.from(getContext());
        convertView = vi.inflate(R.layout.custom_layout,null);
        //convertView = LayoutInflater.from(getContext()).inflate(R.layout.custom_layout,parent,false);
    }
    if(rowItem!=null){
        ImageView tvImage = (ImageView) convertView.findViewById(R.id.shekil);
        TextView tvTittle = (TextView) convertView.findViewById(R.id.ad);
        TextView tvRating = (TextView) convertView.findViewById(R.id.rating);
        TextView tvJanr = (TextView) convertView.findViewById(R.id.janr);
        if(tvImage!=null && tvTittle!=null && tvRating!=null && tvJanr!=null){
            tvImage.setImageResource(rowItem.getImages());
            tvTittle.setText(rowItem.getMovie_titles());
            tvRating.setText(rowItem.getMovide_ratings());
            tvJanr.setText(rowItem.getMovide_janrs());
        }
    }

    return convertView;
}

1 个答案:

答案 0 :(得分:1)

问题在这里

ArrayAdapter<String> myadapter = new ArrayAdapter<>
 (this,android.R.layout.simple_list_item_1,rowItems);  
 if(mylist!=null){       
    mylist.setAdapter(myadapter);
 }

您正在使用arrayadapter而不是CustomAdapter。

CustomAdapter myadapter = new CustomAdapter(this,R.layout.custom_layout,rowItems);
     if(mylist!=null){
         mylist.setAdapter(myadapter);

您的CustomAdapter应该是这样的:

public CustomAdapter(Context context, int textViewResourceId, ArrayList<RowItem> items) {
        super(context, textViewResourceId, items);
    }

似乎您在问题上分享的内容以及您在图片上分享的内容是不同的。上面的代码具有相同的构造函数,不会产生任何问题。

修改

  

在您的MainActivity.java中,

替换

ArrayList<String> rowItems = new ArrayList<>();

ArrayList<RowItem> rowItems = new ArrayList<>();