Search code examples
androidarrayslistviewbaseadapter

How to pass more than one array to populate list view in android?


I want to populate ListView with Images and Images' uploaded time. I have done with the images but i have no idea about how to set time when image is uploaded to the ListView. How can I do that?

Right now this is what I have:

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;
    }
}

Please help me.

thanx.


Solution

  • You should wrap your image path and upload time as an object,like ImageRow,and change your pathArray to List<ImageRow> and when you upload image successfully,replace this in onActivityResult():

    uri = data.getData();
    pathArray.add(uri.toString());
    

    with

    uri = data.getData();
    pathArray.add(new ImageRow(uri.toString(),System.currentTimeMills()));
    

    then change your MyCustomAdapter class to receive List<ImageRow> as second parameter and get data from this to set imageview and time textview.