android


Button inside listview clickable after few seconds


I have created listview with sr,name,button and timer(textview).I want button must be clickable only after countdown is over.Problem is that button is clickable even if countdown is going on.The button must not get clicked until time gets over.
Following is my code-
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;
}
}
just change this method:
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);
buy.set.setClickable(false);
} else {
buy.set.setClickable(true);
ltime.setText("Buy Now!!");
}
}
And at this code, remove if condition just call click listener:
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();
}
});
}
try this;
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) {
timeDiff = mModel.expirationTime - currentTime; // Changes -- remove long
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.setclickable(false);
viewholder.buy.setEnabled(false);
}else{
viewholder.buy.setclickable(true);
viewholder.buy.setEnabled(true);
}
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;
}
Try to move click listener code out side if condition and you need to just set Button clickable and enable true/false based on timer value as below :
viewholder.buy.setClickable(timeDiff==0);
viewholder.buy.setEnabled(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();
}
});

Related Links

How to save captured image in custom gallery not in sd card?
How to get external storage mount points
Inflating Custom View into Layout
Initiating product consumption android in app billing
Android application freezes when disconnecting with bluetooth device
Directed to wrong url
How do I use onFocusChangeListener with respect to a several subclassed fragments?
How to restore default WebViewClient in WebView
How to move images one by one using horizontalscrollview in android
null values getting inserted
How to move to next image in ViewPager, if the current image is in zoomed state in TouchImageView
PlusOneActivity not starting when clicking +1 button in Android app
Is there a way not to keep payed files on device using In App Billing on Android?
How to solve app get crashed
Accessing getExternalFilesDir within a WebView
How to make `Spinner onItemSelected()` to update `ListView` items from arrays in `strings.xml`?

Categories

HOME
shell
adal
c++11
eslint
string
merge
ecmascript-6
flex
android-json
crystal-reports-2013
missing-data
banner
webix
teechart
pattern-matching
tput
3nf
qsqlquery
package.json
null-pointer
google-container-registry
swagger-2.0
asp.net-core-webapi
google-maps-autocomplete
springfox
event-store
data.stackexchange.com
ms-access-web-app
asymptotic-complexity
underscore.js
testcomplete
mockjax
cognos-bi
sql-delete
yowsup
fiware-cygnus
latitude-longitude
z-notation
bc
kill
trigonometry
subclipse
ssrs-2014
smoothstate.js
servermanager
openrasta
yii2-api
mongotemplate
java-bytecode-asm
compiler-design
spring-data-couchbase
java-websocket
template-toolkit
trigger.io
chromium-os
envi
mta
ng-grid
freepbx
toast
atan2
tfs-workitem
rate-limiting
batik
dronekit-android
uvc
entity-framework-4.3
execve
ssis-data-flow
appserver
insight.database
wxformbuilder
radiant
jammer
dolby-audio-api
pclzip
wp7test
insert-id
patricia-trie
jquery-blockui
os.system
dajaxice
stress
urlrewriter
flex-mobile
procedures
workflow-services
squishit
zend-rest
blender-2.49
duplex
escrow
unmanagedresources

Resources

Encrypt Message



code
soft
python
ios
c
html
jquery
cloud
mobile