【发布时间】:2014-09-09 09:26:02
【问题描述】:
问题是我正在尝试通过 listview 更新所有按钮,按钮在我编写的 CustomAdapter 中,但按钮没有被更新。所以想法是更改 ListView 中所有按钮的文本并在单击按钮后显示计时器,谁能告诉我做错了什么?代码如下:
public class CustomAdapter extends BaseAdapter {
Context context;
LayoutInflater inflater;
ArrayList<HashMap<String, String>> data;
ImageLoader imageLoader;
HashMap<String, String> resultp = new HashMap<String, String>();
int secs, mins;
public CustomAdapter(Context context,
ArrayList<HashMap<String, String>> arraylist) {
this.context = context;
data = arraylist;
imageLoader = new ImageLoader(context);
}
@Override
public int getCount() {
return data.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
public View getView(final int position, View convertView, ViewGroup parent) {
final Button buttonShare;
TextView title;
ImageView poster;
View itemView = null;
if (convertView == null) {
inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
itemView = inflater.inflate(R.layout.ads_layout, parent, false);
} else {
itemView = convertView;
}
resultp = data.get(position);
// Настраиваем текстовые поля
title = (TextView) itemView.findViewById(R.id.title);
buttonShare = (Button) itemView.findViewById(R.id.postButton);
// ImageView
poster = (ImageView) itemView.findViewById(R.id.adImage);
title.setText(resultp.get(AdsFragment.TAG_TITLE));
imageLoader.DisplayImage(resultp.get(AdsFragment.TAG_PHOTO), poster);
buttonShare.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
Boolean posted = AdsFragment.getPosted();
if (posted == false) {
context.startService(new Intent(context, BroadcastService.class));
Log.i("SERVICE", "Started service");
cdt.start();
//updateGUI(buttonShare);
} else {
Log.i("POST", "WAS POSTED!!!");
new AlertDialog.Builder(context).setTitle("LALKA")
.setMessage("LALKA").setCancelable(true).show();
}
}
});
CountDownTimer cdt = new CountDownTimer(20000, 1000) {
@Override
public void onTick(long millisUntilFinished) {
secs = (int) (millisUntilFinished / 1000);
mins = secs / 60;
secs = secs % 60;
buttonShare.setText(" " + mins + " : " + String.format("%02d", secs));
}
@Override
public void onFinish() {
}
};
return itemView;
}
}
【问题讨论】:
-
你有
//updateGUI(buttonShare);评论了方法调用 -
@SagarPilkhwal 我暂时不使用此方法,我需要在所有按钮中启动计时器。
-
所以你想在每个按钮被点击时启动一个倒计时?
-
@SagarPilkhwal 是的,并从 listview 更新所有按钮上的文本。
标签: java android listview baseadapter