0

I have list items on one list layout among items i have there is a chronometer that i need to start on each row ,other items come from database, but when activity launches Chronometer stays on 00:00. Please help me , i did many research some tell me that in can use adapter but i am not familiar with it, i don't know if there is not another way to do it without using adapter .

MainActivity .java:

     private ArrayList<HashMap<String, String>> userList;

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

           myList = (ListView)findViewById(R.id.listView);

     inflatedView = 
    getLayoutInflater().inflate(R.layout.payement_timing_list_adapter_view, null);
       chronometer = (Chronometer) 
       inflatedView.findViewById(R.id.chronometer);


          if (userList.size() != 0) {
        //Set the User Array list in ListView
        adapter = new SimpleAdapter(getApplicationContext(), userList, R.layout.payement_timing_list_adapter_view,
                new String[]{"Driver_fullname", "plate_no", "parking_name"},
                new int[]{R.id.drivername, R.id.plateno, R.id.pname});
        myList.setAdapter(adapter);

          }
       myList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
       @Override
          public void onItemClick(AdapterView<?> parent,
                                View view, int position, long id) {
                Log.d("My POSITION",""+position);              
   ((Chronometer)inflatedView.findViewById(R.id.chronometer)).myList.
    (position).start();
            chronometer.start();
        }
           });
Niragire Sam
  • 1
  • 1
  • 6

1 Answers1

0

You will need to use a custom adapter for your ListView, and declare the Chronometer in the getView method for each row.

A custom adapter could be onle like this:

public class CustomAdapter extends BaseAdapter{   

  ArrayList<HashMap<String, String>> users;
  Context context;
  private static LayoutInflater inflater=null;

  public CustomAdapter(MainActivity mainActivity, ArrayList<HashMap<String, String>> usersList) {
    // TODO Auto-generated constructor stub
    users = usersList;
    context=mainActivity;
    inflater = ( LayoutInflater )context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  }

  @Override
  public int getCount() {
    // TODO Auto-generated method stub
    return items.length;
  }

  @Override
  public Object getItem(int position) {
    // TODO Auto-generated method stub
    return position;
  }

  @Override
  public long getItemId(int position) {
    // TODO Auto-generated method stub
    return position;
  }

  public class Holder
  {
    TextView tv;
    ...
    Chronometer cr;
  }

  @Override
  public View getView(final int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    Holder holder=new Holder();
    View rowView;

     //Create a custom layout for your row view with the chronometer on it and get it here
     rowView = inflater.inflate(R.layout.custom_row_item, null);

     holder.tv=(TextView) rowView.findViewById(R.id.textView1);
     ...
     holder.cr=(Chronometer) rowView.findViewById(R.id.chronometer);  
     cr.start();

     holder.tv.setText("GET THE TEXT YOU NEED FROM USERS LIST HERE");   

     return rowView;
  }

} 

Then declare and use your custom adapter like:

CustomAdapter myAdapter = new CustomAdapter(YourActivity.this, userList);
myList.setAdapter(myAdapter);

Hope it helps :)

kodartcha
  • 1,063
  • 12
  • 23
  • Thank you , but i don't know which items to be returned, return items.length; items shows an error , and i don't know what to return here – Niragire Sam Aug 21 '17 at 08:37