我遇到列表問題。我的列表由兩行組成。首先是關於對象的簡單信息。當我點擊第一個項目時出現了第二行。它有一個計時器,一切按預期工作,但重新獲得的時間開始更快。我知道有必要清理清單,或者我只是做錯了什麼。請幫幫我。 這是我的代碼:Android自定義列表視圖使用計時器查看
public class MainActivity extends Activity {
ArrayList<Childrens> arrayList = new ArrayList<Childrens>();
ChilndrensAdapter adapter;
ListView listView;
private static Context context;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context = MainActivity.this;
final Childrens ls = new Childrens();
arrayList = ls.getListView();
adapter = new ChilndrensAdapter(getApplicationContext(), arrayList);
listView = (ListView) findViewById(R.id.listView);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if (arrayList.get(position).status == false) {
Log.d("True", " True");
adapter.setSelecterIndex(position);
adapter.toggleSelected(new Integer(position));
adapter.startUpdateTimer(position);
adapter.notifyDataSetChanged();
} else if (arrayList.get(position).status == true) {
Log.d("False", " False");
adapter.setSelecterIndex(position);
adapter.toggleSelected(new Integer(position));
adapter.stopUpdateTimer(position);
adapter.notifyDataSetChanged();
}
}
});
}
public static Context getContext() {
return context;
}
}
適配器:
public class ChilndrensAdapter extends BaseAdapter {
ArrayList<Childrens> arrayList;
Context context;
private LayoutInflater cInflater;
public ArrayList<Integer> selectedIds = new ArrayList<Integer>();
private ArrayList<ViewHolder> lstHolders;
private static final int END = 0;
private static final int START = 1;
Timer tmr = new Timer();
private Handler mHandler = new Handler();
private Runnable updateRemainingTimeRunnable = new Runnable() {
@Override
public void run() {
//Log.d("Runnable","Runnable");
synchronized (lstHolders) {
long currentTime = 0L;
//long currentTime = System.currentTimeMillis();
for (ViewHolder holder : lstHolders) {
holder.updateTimeRemaining(currentTime);
}
}
}
};
public ChilndrensAdapter(Context context, ArrayList<Childrens> arrayList) {
this.context = context;
this.arrayList = arrayList;
this.cInflater = LayoutInflater.from(context.getApplicationContext());
lstHolders = new ArrayList<>();
// startUpdateTimer();
}
public void startUpdateTimer(final int positon) {
arrayList.get(positon).status = true;
tmr.schedule(new TimerTask() {
@Override
public void run() {
//Log.d("tmr","tmr");
mHandler.post(updateRemainingTimeRunnable);
// arrayList.get(positon).time = arrayList.get(positon).time + 1;
}
}, 1000, 1000);
for (int i = 0; i < lstHolders.size(); i++) {
//Log.d("Holders: ","H: "+ i+" " + lstHolders.get(i) +" Size: " + lstHolders.size() + " Time: "+arrayList.get(positon).time);
}
}
public void stopUpdateTimer(int position) {
arrayList.get(position).time = 0;
arrayList.get(position).status = false;
// Log.d("Timer Run:", "Time is:" + lstHolders.size());
}
@Override
public int getCount() {
if (arrayList == null) {
return 0;
}
return arrayList.size();
}
public void setSelecterIndex(int ind) {
notifyDataSetChanged();
}
public void toggleSelected(Integer position) {
if (selectedIds.contains(position)) {
selectedIds.remove(position);
} else {
selectedIds.add(position);
}
}
@Override
public int getItemViewType(int position) {
if (selectedIds.contains(position)) {
return 1;
} else
return 0;
}
@Override
public int getViewTypeCount() {
return 2;
}
@Override
public Childrens getItem(int position) {
return arrayList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
Childrens item = (Childrens) getItem(position);
int type = getItemViewType(position);
if (convertView == null) {
switch (type) {
case END:
holder = new ViewHolder();
convertView = cInflater.inflate(R.layout.listview_row_normal, parent, false);
holder.name = (TextView) convertView.findViewById(R.id.name);
holder.info = (TextView) convertView.findViewById(R.id.name_control);
convertView.setTag(holder);
break;
case START:
holder = new ViewHolder();
convertView = cInflater.inflate(R.layout.listview_row_start, parent, false);
holder.name = (TextView) convertView.findViewById(R.id.name_start);
holder.button = (ImageView) convertView.findViewById(R.id.dialog_button);
holder.holderTimer = (TextView) convertView.findViewById(R.id.answerTime);
convertView.setTag(holder);
synchronized (lstHolders) {
lstHolders.add(holder);
}
break;
}
} else {
holder = (ViewHolder) convertView.getTag();
}
switch (type) {
case END:
holder.name.setText(item.name);
holder.info.setText("Time");
break;
case START:
holder.setData(getItem(position));
holder.name.setText(item.name);
holder.button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final Dialog dialog = new Dialog(MainActivity.getContext());
dialog.setTitle("Position " + position);
dialog.setContentView(R.layout.dialog);
dialog.show();
}
});
break;
}
return convertView;
}
public class ViewHolder {
private TextView name;
private TextView info;
public TextView holderTimer;
public ImageView button;
Childrens mChildrens;
public void setData(Childrens item) {
mChildrens = item;
updateTimeRemaining(System.currentTimeMillis());
}
public void updateTimeRemaining(long currentTime) {
int sec = (mChildrens.time) % 60;
int min = (mChildrens.time/60) % 60;
holderTimer.setText(String.format("%02d", min) + ":" + String.format("%02d", sec));
// holderTimer.setText("Time: " + min +":"+ sec);
}
}
}
對象:
public class Childrens {
public String name;
public int id;
public long answerTime = 0;
public boolean status;
public int time;
final static Childrens CHILDRENS_STATE = new Childrens();
Childrens(String n, int id, long answerTime) {
this.name = n;
this.id = id;
this.answerTime = answerTime;
}
Childrens() {
}
public String getName() {
return name;
}
public int getId() {
return id;
}
public static Childrens getInstance() {
return CHILDRENS_STATE;
}
public ArrayList<Childrens> getListView() {
ArrayList<Childrens> arrayList = new ArrayList<>();
for (int i = 1; i <= 10; i++) {
Childrens c = new Childrens();
c.status = false;
c.name = "Child: " + i;
c.time = 0;
arrayList.add(c);
//arrayList.add(new Childrens("Tata"+ i, i, answerTime));
}
return arrayList;
}
}