ListView with Custom ArrayAdapter not updating

I am currently trying to return a custom textview, chrono and checkbox view. I overridden the getView method, but not sure if I did it correctly. I would appreciate some comments on the array. It is not currently being updated in my application. Thanks!

main java

public class TaskTracker extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Button addButton; addButton=(Button)findViewById(R.id.button1); ListView myListView= (ListView)findViewById(R.id.listView1); final EditText myEditText= (EditText)findViewById(R.id.editText1) ; //final ArrayList<String> taskitems = new ArrayList<String>(); final TTAdapterView aa = new TTAdapterView(this); // aa = new ArrayAdapter<String>(this, 0); myListView.setAdapter(aa); addButton.setOnClickListener(new OnClickListener(){ public void onClick(View v){ aa.add(myEditText.getText().toString()); //taskitems.add(count, myEditText.getText().toString()); aa.notifyDataSetChanged(); myEditText.setText(""); myEditText.requestFocus(); } }); } 

}

Arrayadapter

 public class TTAdapterView extends ArrayAdapter<String> { public View v; public TTAdapterView(Context context){ super(context,R.layout.row); } @Override public View getView(int position, View convertView, ViewGroup parent){ this.v = convertView; if(v==null){ LayoutInflater vi = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); v = vi.inflate(R.layout.row, null); } return v; } 
+4
source share
4 answers

You should BaseAdapter extend the BaseAdapter , not the ArrayAdapter in your ListView adapter class. notifyDataSetChanged() bit funky anytime I do something other than the BaseAdapter extension. At least that was my experience.

+4
source
 public class DisplaydetailsListAdapter extends ArrayAdapter<EmployeeData> { ArrayList<EmployeeData> emplist = null; public Activity activity; public DisplaydetailsListAdapter(Activity activity, int layoutResourceId, ArrayList<EmployeeData> emplist) { super(activity, layoutResourceId, emplist); this.activity = activity; this.emplist = emplist; } public static class ViewHolder { public TextView firstnamelastname; public TextView designation; public TextView dateofbirth; } public View getView(int position, View convertView, ViewGroup parent) { View v = convertView; ViewHolder holder; if (v == null) { LayoutInflater vi = (LayoutInflater) activity .getSystemService(Context.LAYOUT_INFLATER_SERVICE); v = vi.inflate(R.layout.adapterlayout, null); holder = new ViewHolder(); holder.firstnamelastname = (TextView) v.findViewById(R.id.name); holder.designation = (TextView) v.findViewById(R.id.desig); holder.dateofbirth = (TextView) v.findViewById(R.id.dateofbirth); v.setTag(holder); } else holder = (ViewHolder) v.getTag(); final EmployeeData objempdat = emplist.get(position); if (objempdat != null) { holder.firstnamelastname.setText(objempdat.getFirstName() + " " + objempdat.getLastName()); holder.designation.setText(objempdat.getDesignation()); holder.dateofbirth.setText(objempdat.getDob()); } else { holder.firstnamelastname.setText(" no name is entered"); holder.designation.setText("no disignation entered"); holder.dateofbirth.setText("no date of birth enterd"); } return v; } } 
+1
source

I had a problem deleting elements from my ArrayAdapter array from the adapter itself, the list would remain the same, and no amount of OnNotifyChanged helped. What the trick did, actually works in the list of elements inside the array itself and removes the element you want to remove from the list, and then calls OnNotifyChanged.

0
source

Check the tutorial (with eclipse source at the bottom) for an example working ListAdapter ( SimpleAdapter )

-1
source

Source: https://habr.com/ru/post/1347571/


All Articles