在ListView中加载多个图像



我正在学习NewBoston教程(https://www.youtube.com/watch?v=nOdSARCVYic&list=PL6gx4Cwl9DGBsvRxJJOzG4r4k_zLKrnxl&索引=48)

他展示了如何将图像放入列表,但他从未展示如何为每段文本分配不同的图像。

这是我的MainActivity

public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    String[] Jobsites = {"River Park Place", "Mayfair", "Jameson House"};
    ListAdapter jobsiteAdapter = new CustomAdapter(this, Jobsites);
    ListView jobsiteListView = (ListView) findViewById(R.id.jobsiteListView);
    jobsiteListView.setAdapter(jobsiteAdapter);
    jobsiteListView.setOnItemClickListener(
            new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    String jobsite = String.valueOf(parent.getItemAtPosition(position));
                    //Toast.makeText(MainActivity.this, jobsite, Toast.LENGTH_LONG).show();
                    if (jobsite == "River Park Place"){
                        //Perform segue to the proper view where employess can sign in
                        //******************************************
                        System.out.println("*****************");
                        System.out.println("Attempting to segue");
                        System.out.println("*****************");

                        //******************************************
                    }else{
                        System.out.println("*****************");
                        System.out.println("These jobsites aren't avaliable yet!");
                        System.out.println("*****************");
                        Toast.makeText(MainActivity.this, "**These Sites aren't avaliable yet!**", Toast.LENGTH_LONG).show();
                    }
                }
            }
        );
    }
}

在视频中,我们制作了一个处理图像的自定义视图。这是代码。

class CustomAdapter extends ArrayAdapter<String> {

public CustomAdapter(Context context, String[] jobsites) {
    super(context,R.layout.custom_row ,jobsites);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater jobsiteInflater = LayoutInflater.from(getContext());
    View customView = jobsiteInflater.inflate(R.layout.custom_row, parent, false);
    String singleJobsiteItem = getItem(position);
    ImageView josbiteImage = (ImageView) customView.findViewById(R.id.josbiteImage);
    josbiteImage.setImageResource(R.drawable.riverparkplace);

    return customView;
    }
}

我还有另外两张图片要添加到列表中最下面的两项文本中。现在,它只是一遍又一遍地为列表中的所有三行加载相同的图片。

让我一步一步地指导您。在我们继续之前,您需要了解ListViewArrayAdapter会用您指定的数据填充每一行。换句话说,您希望像处理Jobsites字符串数组一样将图像传递给适配器。

  1. 定义一个包含字符串(Jobsites)和要分配给它的图像simple wrapper object

    public class SimpleObject {
        private String jobSite;
        private int imageID; // your R.drawable.image
        public SimpleObject(String jobSite, int imageID) {
            this.jobSite = jobSite;
            this.imageID = imageID;
        }
        public String getJobSite() {
            return jobSite;
        }
        public int getImageID() {
            return imageID;
        }
    }
    
  2. 初始化适配器要使用的SimpleObject阵列。在主活动的onCreate()中,执行以下操作:

    ArrayList<SimpleObject> objectList = new ArrayList<>();
    objectList.add(new SimpleObject("River Park Place", R.drawable.image1);
    objectList.add(new SimpleObject("Mayfair", R.drawable.image2);
    // the list goes on....
    
  3. 现在,将CustomAdapter更改为保持SimpleObject而不是String:

    class CustomAdapter extends ArrayAdapter<SimpleObject> {
    
        public CustomAdapter(Context context, ArrayList<SimpleObject> objectList) {
            super(context,R.layout.custom_row ,objectList);
        }
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            LayoutInflater jobsiteInflater = LayoutInflater.from(getContext());
            View customView = jobsiteInflater.inflate(R.layout.custom_row, parent, false);
            // Get the SimpleObject
            SimpleObject item = (SimpleObject) getItem(position);
            String singleJobsiteItem = item.getJobSite(); // get the String
            ImageView josbiteImage = (ImageView) customView.findViewById(R.id.josbiteImage);
            josbiteImage.setImageResource(item.getImageID()); // get the image ID and assign it to jobsiteImage :)
    
            return customView;
        }
    }
    
  4. 现在,请确保在主活动中使用新的SimpleObject列表初始化适配器:

    ListAdapter jobsiteAdapter = new CustomAdapter(this, objectList);
    

您需要实现baseadapter,因为图像

相关内容

  • 没有找到相关文章

最新更新