I am working on a shopping cart assignment for that I have created a custom ListView that contains the Picture, Price and Add to Cart button in each row. I also have a textview
Make sure you have reference to your TextView in xml.
Connect it (using findViewById()
) inside onCreate()
of Activity
only, and you may pass it to your Adapter
(i.e making a class field and setter getter method), if Adapter
is a separate class.
and dont do this inside getView()
of Adapter, remove this line.
final TextView tv = (TextView)convertView.findViewById(R.id.tvCount);
you will pass your TextView
from Activity
to Adapter
using constructor or setter getter method, will keep a reference using class Adapter's
class level variable and the what you will do inside onClick()
of Button
is:
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
count++;
yourClassLevelTextView.setText(String.valueOf(count));
}});
how you will pass TextView to Adapter
public class YourAdapter extends BlahBlahBlah
{
private TextView yourClassLevelTextView;
public void setTextView(TextView textViewFromActivity)
{
this.yourClassLevelTextView = textViewFromActivity;
}
}
and in Activity
do something like this:
YourAdapter adapter = new YourAdapter(blah, blah, blah);
adapter.setTextView(yourTextViewInActivity);
listView.setAdapter(adapter);
we have to set value runOnUiThread of that Controls. like this.. May this help you.
btn.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v) {
count++;
setTimertext(count);
}
});
public void setTimertext(String strValue) {
runOnUiThread(new Runnable() {
public void run() {
txt.setText(String.valueOf(strValue));
}
});
}
Hi I guess count textview in your code not belongs to holder that count text view also should display through holder
Your tv TextView
isn't in the list row layout(where you search for it right now). You'll have to look for it in the activity layout if you declared it there(or in the layout where you declared it):
final TextView tv = (TextView)findViewById(R.id.tvCount);