4

I am currently trying to return a custom view of a textview, chrono, and checkbox. I have overriden the getView method but I am not sure if I did it correctly. I would appreciate some comments on the arrayadapter. It currently does not update 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 Answers 4

4

You should probably extend BaseAdapter rather than ArrayAdapter in your ListView adapter class. notifyDataSetChanged() is a little bit funky any time I do something other than extend BaseAdapter. That's been my experience at least.

Sign up to request clarification or add additional context in comments.

3 Comments

extending BaseAdapter sounds easy but I can't find a good resource yet. Do you know any?
Try this: www.androidguys.com/2008/07/14/fancy-listviews-part-one/
Thanks! Extending BaseAdapter helped! Look here for a sample: thinkandroid.wordpress.com/2010/01/13/custom-baseadapters
1
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;
    }
}

Comments

0

I had a problem when deleting elements from my ArrayAdapter from within the adapter itself, the list would have stayed the same and no amount of OnNotifyChanged didn't help. What did the trick was is actually running on the list of the elements within the arrayadapter itself and removing the element that you want to delete from the list and then calling OnNotifyChanged.

Comments

-1

Check this tutorial ( with eclipse source on the bottom ) for an example of a working ListAdapter (SimpleAdapter)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.