This is the list class.
public class list {
public String title;
public String no;
public list( String title,String no) {
super();
this.title = title;
this.no=no;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getNo() {
return no;
}
public void setNo(String no) {
this.no = no;
}
@Override
public String toString() {
return title + "\n" ;
}
}
Here is the list_Adapter class.
public class list_Adapter extends ArrayAdapter<list> {
Context context;
int layoutResourceId;
public list_Adapter(Context context, int layoutResourceId, List<list> items) {
super(context, layoutResourceId, items);
this.layoutResourceId = layoutResourceId;
this.context = context;
}
/*private view holder class*/
private class ViewHolder {
TextView txtTitle;
TextView txtNo;
}
ViewHolder holder = null;
public View getView(int position, View convertView, ViewGroup parent) {
final list lists = getItem(position);
LayoutInflater mInflater = (LayoutInflater) context
.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = mInflater.inflate(R.layout.items_row, null);
holder = new ViewHolder();
holder.txtTitle = (TextView) convertView.findViewById(R.id.textTitle);
holder.txtNo=(TextView) convertView.findViewById(R.id.no);
convertView.setTag(holder);
} else
holder = (ViewHolder) convertView.getTag();
Button addi=(Button)convertView.findViewById(R.id.addi);
addi.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String s;
s= holder.txtNo.getText().toString();
int c=Integer.parseInt(s);
c=c+1;
Log.e("rgr",Integer.toString(c));
lists.setNo(Integer.toString(c));
holder.txtNo.setText(lists.getNo());
}
});
holder.txtTitle.setText(lists.getTitle());
holder.txtNo.setText(lists.getNo());
return convertView;
}
}
Main Class -
public class Main extends AppCompatActivity implements FragmentDrawer.FragmentDrawerListener, AdapterView.OnItemClickListener {
private Toolbar toolbar;
private FragmentDrawer drawerFragment;
private ListView listView1;
static int position;
List<list> rowitems;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.cart);
Intent intent=getIntent();
position=intent.getExtras().getInt("position");
toolbar = (Toolbar) findViewById(R.id.tool_bar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowHomeEnabled(true);
rowitems = new ArrayList<list>();
listView1 = (ListView) findViewById(R.id.listView);
list_Adapter adapter = new list_Adapter(this,
R.layout.items_row, MyAdaptertwo.rowitems);
listView1.setAdapter(adapter);
listView1.setOnItemClickListener(this);
drawerFragment = (FragmentDrawer)
getSupportFragmentManager().findFragmentById(R.id.fragment_navigation_drawer);
drawerFragment.setUp(R.id.fragment_navigation_drawer, (DrawerLayout) findViewById(R.id.drawer_layout), toolbar);
drawerFragment.setDrawerListener(this);
}
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
}
public void onDrawerItemSelected(View view, int position) {
}
In the list_Adapter class there is a button. On click of the button the number present in the textview txtNo should increase by one. In the logcat I can see the value increasing but the text in the textview is remaining constant. I want the text to change as and when the button is clicked.
In your list_Adapter class, replace
holder.txtNo.setText(lists.getNo());
with
list_Adapter.this.notifyDatasetChanged();
This tells the list, that data in the adapter has changed, and so it should refresh itself. By refreshing, the holder objects are also rebuilt, so it is not necessary to update the data there.