我想用图像和图像填充ListView'上传时间。我已完成图像,但我不知道如何设置图像上传到ListView的时间。我怎么能这样做?
现在这就是我所拥有的:
MainActivity.java
ArrayList<String> pathArray;
Button pickImage;
Uri uri;
ListView l;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pathArray = new ArrayList<>();
pickImage = (Button) findViewById(R.id.buttonPick);
pickImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent pickup = new Intent();
pickup.setType("image/*");
pickup.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(pickup, "Select Image"), 5);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == 5) {
try{
uri = data.getData();
pathArray.add(uri.toString());
l.setAdapter(new MyCustomAdapter(this, pathArray));
}
catch (NullPointerException ex){
ex.printStackTrace();
}
}
}
MyCustomAdapter.java
public class MyCustomAdapter extends BaseAdapter {
ArrayList<String> result;
Context context;
private static LayoutInflater inflater = null;
public MyCustomAdapter(MainActivity mainActivity, ArrayList<String> imageNameList) {
context = mainActivity;
result = imageNameList;
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return result.size();
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
public class Holder{
private TextView textViewTime;
ImageView listViewImage;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Holder holder;
if (convertView == null) {
holder = new Holder();
convertView = inflater.inflate(R.layout.listview_layout, null);
holder.textViewTime = (TextView) convertView.findViewById(R.id.textTime);
holder.listViewImage = (ImageView) convertView.findViewById(R.id.listViewImage);
convertView.setTag(holder);
}
else{
holder = (Holder) convertView.getTag();
}
String path=result.get(position);
/* Time today = new Time(Time.getCurrentTimezone());
today.setToNow();
int day = today.monthDay;
int month = today.month + 1;
int year = today.year; */
holder.listViewImage.setImageURI(Uri.parse(path));
return convertView;
}
}
请帮帮我。
感谢名单。
答案 0 :(得分:0)
您应该将图片路径包装起来并将时间作为对象(如ImageRow)上传,并将pathArray
更改为List<ImageRow>
,并在成功上传图片时,将其替换为onActivityResult()
:< / p>
uri = data.getData();
pathArray.add(uri.toString());
与
uri = data.getData();
pathArray.add(new ImageRow(uri.toString(),System.currentTimeMills()));
然后将您的MyCustomAdapter
课程更改为接收List<ImageRow>
作为第二个参数,并从中获取数据以设置imageview和时间textview。