开发者社区> 问答> 正文

如何使用ArrayAdapter从ArrayList显示ListView中的项目(图像的路径)?

如何使用ArrayAdapter从ArrayList中的ListView中显示项目(图像的路径)?我的代码不起作用-它不打印图像路径列表。


public class MainActivity extends Activity {

    ImageView imageView;
    Button button;

    private static final int PICK_IMAGE = 100;
    Uri imageUri;

    ArrayList<String> urllist = new ArrayList<>();
    ListView list;

    TextView tv;

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

        imageView = (ImageView)findViewById(R.id.imageView);
        button = (Button)findViewById(R.id.buttonLoadPicture);
        list = (ListView) findViewById(R.id.list);
        //tv=(TextView) findViewById(R.id.tv);

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, urllist);
        list.setAdapter(adapter);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                openGallery();

            }
        });
    }
    private void openGallery() {
        Intent gallery = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.INTERNAL_CONTENT_URI);
        startActivityForResult(gallery, PICK_IMAGE);
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data){
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK && requestCode == PICK_IMAGE){
            imageUri = data.getData();
            imageView.setImageURI(imageUri);
            String name = data.getData().getPath();
            urllist.add(name); //here I add each path to list
            //tv.setText(TextUtils.concat(tv.getText(), " ", name));
            Toast.makeText(this, name, Toast.LENGTH_SHORT).show();
        }
    }
}

展开
收起
Puppet 2020-01-15 11:48:22 1005 0
1 条回答
写回答
取消 提交回答
  • 在更新时urllist,还需要通知适配器。为此,请改成您adapter的字段。

    然后,当您向其中添加项目时urllist,别忘了打电话adapter.notifyDataSetChanged()。

    
    ArrayList<String> urllist = new ArrayList<>();
    
    // Make adapter a field
    ArrayAdapter<String> adapter;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        // ...
    
        adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, urllist);
        list.setAdapter(adapter);
    
        // ...
    }
    
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data){
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK && requestCode == PICK_IMAGE){
            // ...
    
            urllist.add(name); //here I add each path to list
            // Notify the adapter
            adapter.notifyDataSetChanged();
        }
    }
    
    2020-01-15 11:48:45
    赞同 展开评论 打赏
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载