android中的自定义列表视图不起作用。应用程式停止运作。图像视图未显示图像

问题描述 投票:0回答:3

我是android studio的新手,正努力学习它。我试图使用自定义适配器创建列表视图以显示图像和文本。但它停止工作。我在互联网上尝试了所有方法,但没有成功。我发现错误出在ImageView.setImageResource(R.drawable.ic_launcher)行的公共View getView(int position,View view,ViewGroup parent)方法中;但是我正在努力解决它。请帮忙。

public class MyListAdapter extends ArrayAdapter<String> {
private final Activity context;
private final String[] ProgName;
private final Integer[] ProgImages;

public MyListAdapter(Activity context, String[] progName, Integer[] progImages) {
    super(context,R.layout.activity_image_list,progName);
    this.context = context;
    this.ProgName = progName;
    this.ProgImages = progImages;

}

    public View getView(int position,View view,ViewGroup parent){


    LayoutInflater inflater = context.getLayoutInflater ();
    View rowView=inflater.inflate (R.layout.activity_image_list,null,true);
    TextView txtTitle = (TextView)rowView.findViewById (R.id.txtName);
    ImageView ImageView = (ImageView)rowView.findViewById (R.id.ImageList1);

    ImageView.setImageResource (ProgImages[position]);
    txtTitle.setText ((ProgName[position]));

    ImageView.setImageResource (R.drawable.ic_launcher);


    return rowView;
    }

MainActivity.java

public class MainActivity extends AppCompatActivity {

ListView list;
Context context;
ArrayList Prglist;
private static final String TAG = "MainActivity";

public static Integer[] ProgImages = {
        R.drawable.ic_launcher1, 
R.drawable.ic_launcher1,R.drawable.ic_launcher1,
R.drawable.ic_launcher1
};

public static String[] ProgName = {
        "Java", "C", "Pascal", "Python"
};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate (savedInstanceState);
    setContentView (R.layout.activity_main);
    MyListAdapter adapter=new MyListAdapter(this, ProgName, ProgImages);

    list=(ListView)findViewById(R.id.list);
    list.setAdapter(adapter);

}}

我是android studio的新手,正努力学习它。我试图使用自定义适配器创建列表视图以显示图像和文本。但它停止工作。我尝试了互联网上的所有内容,但没有成功。我有...

android android-imageview listadapter
3个回答
0
投票

如果ProgImages数组是资源(例如R.drawable.icon),则可以使用getResources方法。尝试这个;


0
投票

尝试使用像毕加索或滑翔这样的照片库来查看图像。


0
投票

似乎您在imageview中使用了Drawable图像。也许试试这个...

© www.soinside.com 2019 - 2024. All rights reserved.