sr、名前、ボタン、タイマー(テキストビュー)でリストビューを作成しました。カウントダウンが終了した後でなければクリックできません。問題はカウントダウンが行われていてもボタンをクリックできます時間が経過するまで、ボタンはクリックされてはならない。 リストビュー内のボタン数秒後にクリック可能
MainActivity.java私のコード - ある
public class MainActivity extends Activity {
Button btnnext;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnnext=(Button)findViewById(R.id.btn1);
btnnext.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent i=new Intent(MainActivity.this,Second.class);
startActivity(i);
}
});
}
}
Model.java
public class Model {
int sr;
String name;
long expirationTime;
public Model(int sr, String name,long expirationTime) {
this.sr = sr;
this.name = name;
this.expirationTime = expirationTime;
}
public int getSr() {
return sr;
}
public void setSr(int sr) {
this.sr = sr;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public long getExpirationTime() {
return expirationTime;
}
public void setExpirationTime(long expirationTime) {
this.expirationTime = expirationTime;
}
}
Second.java後
public class Second extends Activity{
TextView txtsrno,txtflower,txtstatus,txttimer,txttimeleft;
ListView listView;
ArrayList<Model> flowerlist;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
txtsrno = (TextView) findViewById(R.id.srno);
txtflower = (TextView) findViewById(R.id.name);
txtstatus = (TextView) findViewById(R.id.buy);
txttimeleft=(TextView)findViewById(R.id.remtime);
listView = (ListView) findViewById(R.id.list);
flowerlist = new ArrayList<>();
flowerlist.add(new Model(1, "Rose",System.currentTimeMillis() + 100000));
flowerlist.add(new Model(2, "Sunflower",System.currentTimeMillis() + 10000));
flowerlist.add(new Model(3, "Lotus",System.currentTimeMillis() + 30000));
flowerlist.add(new Model(4, "Jasmine",System.currentTimeMillis() + 20000));
SecondCustom sc = new SecondCustom(this, flowerlist);
listView.setAdapter(sc);
}
}
SecondCustom.java
public class SecondCustom extends BaseAdapter {
Activity a;
ArrayList<Model> flowerlist;
private List<Viewholder> lstHolders;
long timeDiff;
private LayoutInflater lf;
private Handler mHandler = new Handler();
private Runnable updateRemainingTimeRunnable = new Runnable() {
@Override
public void run() {
synchronized (lstHolders) {
long currentTime = System.currentTimeMillis();
for (Viewholder holder : lstHolders) {
holder.updateTimeRemaining(currentTime);
}
}
}
};
public SecondCustom(Activity a, ArrayList<Model> flowerlist) {
this.a = a;
this.flowerlist = flowerlist;
lstHolders = new ArrayList<>();
startUpdateTimer();
}
private void startUpdateTimer() {
Timer tmr = new Timer();
tmr.schedule(new TimerTask() {
@Override
public void run() {
mHandler.post(updateRemainingTimeRunnable);
}
}, 1000, 1000);
}
@Override
public int getCount() {
return flowerlist.size();
}
@Override
public Object getItem(int i) {
return flowerlist.get(i);
}
@Override
public long getItemId(int i) {
return i;
}
public class Viewholder{
TextView srno,name,ltime;
Button buy;
Model mModel;
public void setData(Model item) {
mModel = item;
name.setText(item.name);
updateTimeRemaining(System.currentTimeMillis());
}
public void updateTimeRemaining(long currentTime) {
long timeDiff = mModel.expirationTime - currentTime;
if (timeDiff > 0) {
int seconds = (int) (timeDiff/1000) % 60;
int minutes = (int) ((timeDiff/(1000 * 60)) % 60);
int hours = (int) ((timeDiff/(1000 * 60 * 60)) % 24);
ltime.setText(hours +":" + minutes +":" + seconds);
} else {
ltime.setText("Buy Now!!");
}
}
}
@Override
public View getView(final int i, View convertView, ViewGroup viewGroup) {
Viewholder viewholder = null;
if (convertView == null) {
viewholder = new Viewholder();
LayoutInflater li=a.getLayoutInflater();
convertView = li.inflate(R.layout.secondcustom, viewGroup, false);
viewholder.srno = (TextView) convertView.findViewById(R.id.sr);
viewholder.name = (TextView) convertView.findViewById(R.id.flow);
viewholder.buy = (Button) convertView.findViewById(R.id.st);
viewholder.ltime = (TextView) convertView.findViewById(R.id.lefttime);
convertView.setTag(viewholder);
synchronized (lstHolders) {
lstHolders.add(viewholder);
}
}else {
viewholder = (Viewholder) convertView.getTag();
}
viewholder.setData((Model) getItem(i));
final Model model=flowerlist.get(i);
viewholder.srno.setText(valueOf(flowerlist.get(i).getSr()));
viewholder.name.setText(valueOf(flowerlist.get(i).getName()));
viewholder.ltime.setText(valueOf(flowerlist.get(i).getExpirationTime()));
if(timeDiff==0) {
viewholder.buy.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(a, "Purchased" + " " + flowerlist.get(i).getName(), Toast.LENGTH_LONG).show();
}
});
}
return convertView;
}
}
そのままview.onclicklistenerを使用してください。タイマーが実行されているときに呼び出しview.setclickable(false)を呼び出し、タイマーの呼び出しが終了したらview.setclickable(true) –
コードを実装する方法を教えてください。 – Neha
timeDiffが0になったときにボタンをクリックしますか? –