How update ListView from my Custom Adapter - java

I just wanna update my ListView, but I cant. I dont know what. What did I do Wrong? I guess that the Adapter that I created is missing something to return the real adapter that I can handle.
Home.java (MainActivity)
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
MenuItem item = navigation.getMenu().findItem(R.id.navigation_home);
item.setCheckable(true);
item.setChecked(true);
BoxStore boxStore = AppMain.getBoxStore();
equipamentoBox = boxStore.boxFor(Equipamento.class);
lancamentoBox = boxStore.boxFor(Lancamento.class);
loadObjects();
////FOCUS HERE/////------------------------------
List<Equipamento> equipamentos = new ArrayList<>();
EquipamentoAdapter adapter;adapter = new EquipamentoAdapter(this, equipamentos);
listEquipamentos = (ListView) findViewById(R.id.listEquipamentos);
listEquipamentos.setAdapter(adapter);
registerForContextMenu(listEquipamentos);
}
EquipamentoAdapter.JAVA
public class EquipamentoAdapter extends ArrayAdapter<Equipamento> {
private final Activity context;
private final List<String> idArray = new ArrayList<String>();
private final List<String> qtdArray = new ArrayList<String>();
private final ArrayList<String> nomeArray = new ArrayList<String>();
private List<Equipamento> equipamentos = new ArrayList<>();
public EquipamentoAdapter(Activity context, List<Equipamento> equipamentos) {
super(context, R.layout.listview_row, equipamentos);
for (Iterator iterator = equipamentos.iterator(); iterator.hasNext(); ) {
Equipamento equipamento = (Equipamento) iterator.next();
this.idArray.add(Integer.toString((int)equipamento.getId()));
this.qtdArray.add(Integer.toString(equipamento.getQuantidade()));
this.nomeArray.add(equipamento.getNome());
}
this.context = context;
this.equipamentos = equipamentos;
}
public void callDialogTransaction(Equipamento equipamento) {
AlertDialog.Builder mBuilder = new AlertDialog.Builder(getContext());
LayoutInflater inflater = (LayoutInflater) context.getSystemService( Context.LAYOUT_INFLATER_SERVICE );
View mView = inflater.inflate(R.layout.dialog_lancamento,null);
TextView title = (TextView) mView.findViewById(R.id.txtTitle);
final EditText quantidade = (EditText) mView.findViewById(R.id.edtQtd);
final EditText Observacao = (EditText) mView.findViewById(R.id.edtObs);
Button addTransaction = (Button) mView.findViewById(R.id.btnAddTranD);
title.setText(equipamento.getNome());
addTransaction.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view) {
if(!quantidade.getText().toString().isEmpty()){
Toast.makeText(getContext(), "Success!", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getContext(), "Erro. Fill everything.", Toast.LENGTH_SHORT).show();
}
}
});
mBuilder.setView(mView);
AlertDialog dialog = mBuilder.create();
dialog.show();
}
public View getView(final int position, View view, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View rowView = inflater.inflate(R.layout.listview_row,null,true);
//this code gets references to objects in the listview_row.xml file
TextView txtQtd,txtName;
txtQtd = (TextView) rowView.findViewById(R.id.txtQtd);
txtName = (TextView) rowView.findViewById(R.id.txtName);
final ImageButton btnAddTransaction = (ImageButton) rowView.findViewById(R.id.btnAddTransaction);
//this code sets the values of the objects to values from the arrays
txtQtd.setText(qtdArray.get(position));
txtName.setText(nomeArray.get(position));
btnAddTransaction.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Equipamento equipamento = equipamentos.get(position);
callDialogTransaction(equipamento);
Animation animation = new AlphaAnimation(1.0f,0.8f);
animation.setDuration(100);
btnAddTransaction.startAnimation(animation);
}
});
return rowView;
}
}
I read that I could try to use adapter.notifyDataSetChanged(); but its not working. Also I tryed to add this on EquipamentoAdapter.java and call from my MainActivity when I needed to refresh, but it didn work as well. I dont know why. Everything seems right.
public void refreshData(){
this.equipamentos.clear();
for(Equipamento equipamento : equipamentoBox.getAll()){
this.equipamentos.add(equipamento);
}
notifyDataSetChanged();
}

I'll suggest the following changes:
Reference the equipamento object directly from List inside the getView the so that the getView function becomes
public View getView(final int position, View view, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View rowView = inflater.inflate(R.layout.listview_row,null,true);
//this code gets references to objects in the listview_row.xml file
TextView txtQtd,txtName;
txtQtd = (TextView) rowView.findViewById(R.id.txtQtd);
txtName = (TextView) rowView.findViewById(R.id.txtName);
final ImageButton btnAddTransaction = (ImageButton) rowView.findViewById(R.id.btnAddTransaction);
//this code sets the values of the objects to values from the arrays
Equipamento equipamento = equipamentos.get(position);
txtQtd.setText(String.valueOf(equipamento.getId()));
txtName.setText(String.valueOf(equipamento.getQuantidade()));
btnAddTransaction.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Equipamento equipamento = equipamentos.get(position);
callDialogTransaction(equipamento);
Animation animation = new AlphaAnimation(1.0f,0.8f);
animation.setDuration(100);
btnAddTransaction.startAnimation(animation);
}
});
return rowView;
}
Set the Items count with the getCount method
public int getCount(){
return equipamentos.size();
}
with these, calling notifyDataSetChanged(); should update the list without need to reinitialize the adapter.

One thing that you can do is reinflaiting the adapter, try this
public void refreshData(){
this.equipamentos.clear();
for(Equipamento equipamento : equipamentoBox.getAll()){
this.equipamentos.add(equipamento);
}
EquipamentoAdapter adapter;adapter = new EquipamentoAdapter(this, equipamentos);
listEquipamentos.setAdapter(adapter);
}

GUYS, I've noticed that if I use:
equipamentos.clear(); //Clear List
for(Equipamento equipamento : equipamentoBox.getAll()){
equipamentos.add(equipamento); //Populate List
}
adapter = null;
adapter = new EquipamentoAdapter((Activity) Home.this, equipamentos);
listEquipamentos.setAdapter(adapter);
adapter.notifyDataSetChanged();
Its gonna work. But this seems VERY wrong in terms of performance. My application is small but I dont want to make bad practices.

Create a method to replace the data and check the size of your adapter after you add new elements.
Add something like this to the adapter:
public void replaceData(List<Equipamento> equipamentos) {
this.equipamentos = equipamentos;
notifyDataSetChanged();
}
#Override
public int getCount() {
return equipamentos.size();
}
Then check the size from the Activity:
adapter.getCount();

As your following logic is in constructor of adapter-
enter code here
for (Iterator iterator = equipamentos.iterator(); iterator.hasNext(); ) {
Equipamento equipamento = (Equipamento) iterator.next();
this.idArray.add(Integer.toString((int)equipamento.getId()));
this.qtdArray.add(Integer.toString(equipamento.getQuantidade()));
this.nomeArray.add(equipamento.getNome());
}
after notifyDataSetChange, adapter is not called so you can do 2 things -
1) Initialize the Adapter as #Gastón Saillén answered.
2) Put that in some method and call it before you call notifydatasetchange.

Related

CustomListAdapater SharedPreferences Not Saving State when closing App

I want the notes I set in the CustomListAdapter to be there until the user removes them. Whether the user closes the app, phone restarts or anything else the notes they have added need to remain there until removed. I have tried to do this by getting the Sharepreferences in my tab and setting them in the CustomListAdapter but they don't save:
I added a counter so I could retrieve the value at a later stage to remove and call the method addnote in the customerListAdapter to set the SharedPreferences.
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
final View v = inflater.inflate(R.layout.tab3, container, false);
final EditText notes = (EditText) v.findViewById(R.id.editText);
listView=(ListView)v.findViewById(R.id.list);
final int cross = R.drawable.cross;
notesofrules = new ArrayList<String>(); //initial data list
pref = getContext().getSharedPreferences("MyPref", MODE_PRIVATE);
editor = pref.edit();
adapter = new CustomListAdapter(getActivity(), notesofrules, cross);
listView=(ListView) v.findViewById(R.id.list);
listView.setAdapter(adapter); //set the adapter once, only manipulate the data within
Button button = (Button)v.findViewById(R.id.button3);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v)
{
String newNote = notes.getText().toString();
adapter.addNote(newNote, counter, editor); //add new note to the adapter list
counter++;
adapter.notifyDataSetChanged(); //very important to notify adapter and refresh the listview
notes.setText("");
}
});
return v;
}
CustomListAdapter:
public class CustomListAdapter extends ArrayAdapter<String> {
private final Activity context;
private ArrayList<String> notes = new ArrayList<>();
private ImageView image;
private int imageCross; //make this a list if you have multiple images and add similar to notes list
public CustomListAdapter(Activity context, ArrayList<String> notes, int imageCross) {
super(context, R.layout.item,notes);
// TODO Auto-generated constructor stub
this.context=context;
this.notes = notes;
this.imageCross = imageCross;
}
public View getView(final int position, View view, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
final View rowView = inflater.inflate(R.layout.item, null, false);
final TextView ruleNotesSet = (TextView) rowView.findViewById(R.id.textView1);
image = (ImageView) rowView.findViewById(R.id.icon);
image.setImageResource(imageCross);
image.setOnClickListener(new Button.OnClickListener(){
#Override
public void onClick(View v){
notes.remove(position);
notifyDataSetChanged();
}
});
ruleNotesSet.setText(notes.get(position));
return rowView;
}
public void addNote(String data, int position, SharedPreferences.Editor editor) {
editor.putString(Integer.toString(position), data);
editor.commit();
notes.add(data);
}
}
Can't see where I have gone wrong, how can I set them and then remove them within the onClick in the customListAdapter?
Edit:
I have added this within Tab:
adapter.getNotes(pref);
listView.setAdapter(adapter);
and this is the getNotes method in the CustomListAdapter:
public void getNotes(SharedPreferences pref)
{
for(String note : notes) {
pref.getString(note, note);
}
}
Still not setting the state back once closed .
I also edited the addNote method:
editor.putString(data, data);
u add this code to save the data u want
SharedPreferences.Editor editor = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE).edit();
editor.putInt("position", position);
editor.apply();
and to get
SharedPreferences prefs = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE);
Int position = prefs.getInt("positon", null);
and use it
Managed to do this within the Tab class. Setting the sharedpreferences as the user clicks the button. Then checking if it is null when creating the view again. Then getting all the preferences and storing it into a Map and passing it to the adapter within onCreate. Whoop. Hope it helps someone one day.
String notesInStorage = pref.getString(Integer.toString(counter), newNote);
if(notesInStorage != null)
{
Map<String,?> keys = pref.getAll();
for(Map.Entry<String,?> entry : keys.entrySet()){
notesofrules.add(entry.getValue().toString());
adapter = new CustomListAdapter(getActivity(), notesofrules, cross);
listView.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
}

Android/Java - Clicking a image in a listview

I made a listview in Android Studio. The listview has a image in every item but I don't know how to make it clickable. I did browse the internet for a solution but the hard part seems to be implementing it in my own code. I cannot figure that out.
Image in question = ex_img
-- Keep in mind when the image is clicked, it should also know its position in the list.
Thanks for reading and I hope you can help me out.
Adapter for list:
public class CustomList extends ArrayAdapter<String> {
private String[] ex_name;
private String[] ex_diff;
private String[] ex_muscle;
private String[] ex_dpr;
private Integer[] ex_img;
private Activity context;
public CustomList(Activity context, String[] ex_name, String[] ex_diff, String[] ex_muscle, String[] ex_dpr,
Integer[] ex_img) {
super(context, R.layout.row_layout, ex_name);
this.context = context;
this.ex_name = ex_name;
this.ex_muscle = ex_muscle;
this.ex_diff = ex_diff;
this.ex_dpr = ex_dpr;
this.ex_img = ex_img;
}
//LIST --> XML
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View listViewItem = inflater.inflate(R.layout.row_layout, null, true);
TextView list_name = (TextView) listViewItem.findViewById(R.id.ex_name);
TextView list_diff = (TextView) listViewItem.findViewById(R.id.ex_diff);
TextView list_muscle = (TextView) listViewItem.findViewById(R.id.ex_muscle);
ImageView list_image = (ImageView) listViewItem.findViewById(R.id.ex_img);
TextView list_dpr = (TextView) listViewItem.findViewById(R.id.ex_dpr);
list_name.setText(ex_name[position]);
list_muscle.setText(ex_muscle[position]);
list_diff.setText(ex_diff[position]);
list_dpr.setText(ex_dpr[position]);
list_image.setImageResource(ex_img[position]);
return listViewItem;
}
}
Part from MainActivity that might be needed:
private Integer img[] = {
R.drawable.ic_favorite_white_24dp,
R.drawable.ic_location_on_white_24dp,
R.drawable.ic_update_white_24dp,
R.drawable.ic_local_dining_white_24dp,
R.drawable.ic_local_dining_white_24dp
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
CustomList customList = new CustomList(this, name, diff, muscle, dpr, img);
listView = (ListView) findViewById(R.id.listview);
listView.setAdapter(customList);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Toast.makeText(getApplicationContext(),"You Clicked "+name[i],Toast.LENGTH_SHORT).show();
}
});
}
Add onClickListener to your ImageView in getView() -
list_image.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Here you have the position too.
});
Just make the position parameter final in getView()

How to fix Android Java ListView Adapter

I need help to fix the following problem:
When i scroll down my ListView Adapter the list that contains the country change to only one country.
This is my Adapter:
public class Nraeby_ListViewAdapter extends BaseAdapter {
private String Liked;
Context mContext;
// Declare Variables
LayoutInflater inflater;
private ArrayList<HashMap<String, String>> data;
ImageLoader imageLoader;
HashMap<String, String> resultp = new HashMap<String, String>();
public Nraeby_ListViewAdapter(Context context,
ArrayList<HashMap<String, String>> arraylist) {
this.data = arraylist;
mContext = context;
imageLoader = new ImageLoader(mContext);
inflater = LayoutInflater.from(mContext);
}
#Override
public int getCount() {
return data.size();
}
#Override
public Object getItem(int position) {
return data.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
public class ViewHolder {
// Declare Variables
TextView rank;
TextView country;
TextView population;
test.Droidlogin.CircleImage flag;
test.Droidlogin.material.AnimateCheckBox checkBox;
ImageButton btnFavourite;
}
public View getView(final int position, View view, ViewGroup parent) {
final ViewHolder holder;
if (view == null) {
holder = new ViewHolder();
view = inflater.inflate(R.layout.nearby_listview_item, null);
// Get the position
resultp = data.get(position);
// Locate the TextViews in nearby_listview_item.xmltem.xml
holder.rank = (TextView) view.findViewById(R.id.rank);
holder.country = (TextView) view.findViewById(R.id.country);
// Locate the ImageView in nearby_listview_item.xmltem.xml
holder.flag = (test.Droidlogin.CircleImage) view.findViewById(R.id.flag);
holder.checkBox = (test.Droidlogin.material.AnimateCheckBox) view.findViewById(R.id.checkbox);
holder.btnFavourite = (ImageButton) view.findViewById(R.id.like);
view.setTag(holder);
} else {
holder = (ViewHolder) view.getTag();
}
// Capture position and set results to the TextViews
holder.rank.setText(resultp.get(NearbyUsers.RANK));
holder.country.setText(resultp.get(NearbyUsers.COUNTRY));
// Capture position and set results to the ImageView
// Passes flag images URL into ImageLoader.class
imageLoader.DisplayImage(resultp.get(NearbyUsers.FLAG), holder.flag);
TinyDB tinydb = new TinyDB(mContext);
Liked = tinydb.getString("MyUsers");
//This handle and change icon when click on.
holder.btnFavourite.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
TinyDB tinydb = new TinyDB(mContext);
holder.btnFavourite.setImageResource(R.drawable.icon_liked);
tinydb.putString("MyUsers",resultp.get(NearbyUsers.COUNTRY));
holder.btnFavourite.setImageResource(R.drawable.icon_liked);
}
});
// Capture ListView item click
view.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// Get the position
Intent intent = new Intent(mContext, SingleItemViewNearbyProfile.class);
// Pass all data rank
intent.putExtra("rank", resultp.get(NearbyUsers.RANK));
// Pass all data country
intent.putExtra("country", resultp.get(NearbyUsers.COUNTRY));
// Pass all data population
intent.putExtra("population",resultp.get(NearbyUsers.POPULATION));
// Pass all data flag
intent.putExtra("flag", resultp.get(NearbyUsers.FLAG));
// Start SingleItemView Class
mContext.startActivity(intent);
}
});
return view;
}
}
pls i need help on how to fix the error
so that when scroll down it will show the list of all country and a button
You should move the line in getView to outside the if block. Like this
final ViewHolder holder;
resultp = data.get(position);
if (view == null) {
///
}
You are only updating the resultp when you create a new view. For a recycled view, you are using a stale data and that's the reason you see some incorrect country data after you scroll.

How do I get notifyDatasetChanged() to work with a ListAdapter?

Right now I use setAdapter to update my ListView, but I think the proper way is to use notifiyDatasetChanged() and I can't get that to work in my main class (it's in the adapter). Here is the error:
The method notifyDatasetChanged() is undefined for the type ListAdapter
I'm guessing there is a better way of doing this - can anyone point me in the right direction?
Here's the relevant parts of my code:
public class ScoreList extends SherlockFragmentActivity {
private ListView listViewScore;
static List<Score> listScore = new ArrayList<Score>();
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.score_list);
ctx = this;
listScore = dbh.getAllScores();
listViewScore = (ListView) findViewById(R.id.score_list);
listViewScore.setAdapter(new ScoreListAdapter(ctx,
R.layout.score_row_item, listScore));
listViewScore.getAdapter().notifyDatasetChanged(); //this is where I get the error
}
}
Here's the adapter:
public class ScoreListAdapter extends ArrayAdapter<Score> {
private int resource;
private LayoutInflater inflater;
public ScoreListAdapter(Context ctx, int resourceId, List<Score> objects) {
super(ctx, resourceId, objects);
resource = resourceId;
inflater = LayoutInflater.from(ctx);
//context = ctx;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
convertView = (LinearLayout) inflater.inflate(resource, null);
Score score = getItem(position);
TextView txtName = (TextView) convertView.findViewById(R.id.name);
txtName.setText(score.getName());
TextView txtScoreChange = (TextView) convertView
.findViewById(R.id.scoreChange);
int scoreChange = Integer.parseInt(score.getScoreChange());
if (scoreChange > 0)
txtScoreChange.setText("+" + scoreChange);
else if (scoreChange < 0)
txtScoreChange.setText("" + scoreChange);
else
txtScoreChange.setText("");
TextView txtScoreTotal = (TextView) convertView
.findViewById(R.id.scoreTotal);
txtScoreTotal.setText(score.getScoreTotal());
final LinearLayout currentRow = (LinearLayout) convertView
.findViewById(R.id.scoreRowLayout);
notifyDataSetChanged();
return convertView;
}
}
Create an instance of your custom adapter, so you can use it anywhere you like...
public class ScoreList extends SherlockFragmentActivity {
private ListView listViewScore;
private ScoreListAdapter adapter;
static List<Score> listScore = new ArrayList<Score>();
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.score_list);
ctx = this;
listScore = dbh.getAllScores();
listViewScore = (ListView) findViewById(R.id.score_list);
adapter = new ScoreListAdapter(ctx, R.layout.score_row_item, listScore);
listViewScore.setAdapter(adapter);
adapter.notifyDatasetChanged();
}
}
By the way, if your listScore array is already loaded, then you do not need to use
adapter.notifyDatasetChanged();
Dont call the notifyDataSetChanged(); method while creation.
only call it when content of your listViewScore changes.. and to use it at that time-
replace
listView.getAdapter().notifyDatasetChanged();
with
((ScoreListAdapter)listView.getAdapter()).notifyDataSetChanged();
and see the magic...
thanks.

Adding a new row on button click to a list view

I want to add a new row on clicking add button in a list view in android.
Can someone help me out?
Here is the code I have so far:
public class prsnlhstry<EventArgs> extends Activity implements OnItemClickListener {
public static ArrayList<String> arr_sort_textview1= null;
public static ArrayList<String> arr_sort_textview2=null;
public static ArrayList<String> arr_sort_textview3=null;
public Resources ApptResources= null;
private ContextWrapper mycontext;
tododata todo;
int clickcounter = 0;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ListView lv= (ListView)findViewById(R.id.listview);
todo = new tododata(this);
List<listObject> List = new ArrayList<listObject>();
for(int i = 0; i <= 5; i++){
listObject lo = new listObject();
lo.grade = Integer.toString(i);
lo.reason = Integer.toString(i);
lo.school = Integer.toString(i);
List.add(lo);
}
CustomAdapter ca = new CustomAdapter(this);
lv = (ListView)findViewById(R.id.listview);
lv.setAdapter(ca);
lv.setOnItemClickListener(this);
ca.notifyDataSetChanged();
Button button = (Button) findViewById(R.id.btn);
button.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
Toast.makeText(prsnlhstry.this, "Row Added Successfully", Toast.LENGTH_SHORT).show();
}
});
}
private class listObject {
String grade;
String school;
String reason;
}
public class CustomAdapter extends BaseAdapter {
private LayoutInflater mInflater;
public CustomAdapter(Context context) {
mInflater = LayoutInflater.from(context);
}
#Override
public int getCount() {
return todo.getLength();
}
#Override
public Object getItem(int position) {
return position;
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
String temp = null;
// to reinflate it. We only inflate a new View when the convertView supplied
// by ListView is null.
if (convertView == null) {
convertView = mInflater.inflate(R.layout.grid_item, null);
// Creates a ViewHolder and store references to the views
// we want to bind data to.
holder = new ViewHolder();
holder.textview1 = (TextView) convertView.findViewById(R.id.item2);
holder.textview2 = (TextView) convertView.findViewById(R.id.item3);
holder.textview3 = (TextView) convertView.findViewById(R.id.item4);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.textview1.setText(todo.getgrade(position));
holder.textview2.setText(todo.getschool(position));
holder.textview3.setText(todo.getreason(position));
return convertView;
}
}
static class ViewHolder {
TextView textview1;
TextView textview2;
TextView textview3;
}
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
}
}
Just add the item to your list and refresh the adapter, nothing special.
You need to increment your "tododata" data structure on button click and then call to notifyDataSetChanged() method:
Button button = (Button) findViewById(R.id.btn);
button.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
todo.addNewRow();
ca.notifyDataSetChanged();
Toast.makeText(prsnlhstry.this, "Row Added Successfully", Toast.LENGTH_SHORT).show();
}
});
To compile the above code you will need to define "ca" variable as the final one, or make it class member.
I hope it helps.

Categories

Resources