在android中的Listview的每一行启动天文台

时间:2017-08-18 12:44:59

标签: java android listview chronometer

我有一个列表布局中的列表项目我有一个计时器,我需要在每一行开始,其他项目来自数据库,但当活动启动时计时器停留在00:00。请帮助我,我做了很多研究,告诉我可以使用适配器,但我不熟悉它,我不知道如果没有使用适配器,没有其他方法可以做到这一点。

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

1 个答案:

答案 0 :(得分:0)

您需要为ListView使用自定义适配器,并在每行的getView方法中声明Chronometer。

自定义适配器可以像这样:

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

} 

然后声明并使用您的自定义适配器,如:

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

希望有所帮助:)