如何使用 ArrayAdapter 在 ArrayList 中的 ListView 中显示项目(图像的路径)?
Posted
技术标签:
【中文标题】如何使用 ArrayAdapter 在 ArrayList 中的 ListView 中显示项目(图像的路径)?【英文标题】:How to display items (paths of images) in ListView from ArrayList using ArrayAdapter? 【发布时间】:2020-05-01 15:55:47 【问题描述】:如何使用 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();
【问题讨论】:
也许你必须调用 notifyDataSetChanged 方法 【参考方案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();
【讨论】:
【参考方案2】:@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
Toast.makeText(this, name, Toast.LENGTH_SHORT).show();
// assign urllist to adapter
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, urllist);
list.setAdapter(adapter);
【讨论】:
以上是关于如何使用 ArrayAdapter 在 ArrayList 中的 ListView 中显示项目(图像的路径)?的主要内容,如果未能解决你的问题,请参考以下文章
为啥在 onCreate() 方法中初始化 Array Adapter 很重要?