سؤال

I have a custom class "Something" which holds some ints and strings. I'm making a page to allow the user to see each current Something's description, and a button to edit or delete next to each one.

I have overridden toString() in the Something class, so that it can return a description to be put into the TextView field.

list_task_edit.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin" 
android:orientation="vertical"
tools:context=".EditTaskList">
    ...
    <ListView 
android:id="@+id/edit_task_listview"
android:layout_width="match_parent"
android:layout_height="match_parent">
</ListView>

    ...
</LinearLayout>

edit_task_list_item.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >

    <TextView
    android:id="@+id/edit_task_name"
    android:layout_weight="1"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    />

    <Button
    android:id="@+id/edit_task_edit"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/button_edittask"
    android:onClick=""
    />

    <Button
    android:id="@+id/edit_task_delete"
    android:layout_width="wrap_content"
    android:text="@string/button_delete_task"
    android:layout_height="wrap_content"
    android:onClick="deleteTask"
    />

</LinearLayout>

(I haven't put a method for editTask yet, as I'm not up to there)

public class EditTaskList extends Activity {

private static ArrayList<Something> list;

ArrayAdapter<Something> arrayAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.list_task_edit);
    list = MainActivity.getMasterList();

    arrayAdapter = new ArrayAdapter<Something>
    (this,
            R.layout.edit_task_list_item,
            R.id.edit_task_name,
            list);
    ListView listView = (ListView)findViewById(R.id.edit_task_listview);
    listView.setAdapter(arrayAdapter);
}

public void deleteTask(View view){
    Something thisItem = (Something)view.getTag();
    if(thisItem == null){
        System.out.println("Bad things");
    }
    arrayAdapter.remove(thisItem);
    arrayAdapter.notifyDataSetChanged();
}
    ...
}

My diagnostic line to check if thisItem == null is going off every time. What am I missing? Am I declaring the ArrayAdapter wrong somehow?

هل كانت مفيدة؟

المحلول 2

Your deleteTask event will not return your list item, instead it will return a button view.

To get the functionality which you want, you should build a custom adapter to keep track of your list items. To do this, you will need to extend the BaseAdapter.

See this response for more detail:

How to add/remove item from listview in android when click button in item listview

The key point is that in the GetView method, you are able to set a tag on your list button which denotes the position of the container item. This then will let you delete the item specified by that position in the OnClickListener for the button.

نصائح أخرى

Updated, working code. In case it's useful to others:

public class EditTaskList extends Activity {

    private static ArrayList<Something> list;
private CustomAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.list_task_edit);
        ListView listView = (ListView)findViewById(R.id.edit_task_listview);
        list = MainActivity.getMasterList();
        adapter = new CustomAdapter(this, list);
        listView.setAdapter(adapter);
    }

    public void resetAdapter(){
        adapter.notifyDataSetChanged();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.edit_task_list, menu);
        return true;
    }

    public class CustomAdapter extends ArrayAdapter<Something>{
        private ArrayList<Something> things;
        private LayoutInflater inflater;

        class ViewHolder {
            public TextView text;
            public Button editButton;
            public Button deleteButton;
        }

        public CustomAdapter(Context context, ArrayList<Something> things) {
            super(context, R.layout.edit_task_list_item, things);
            inflater = LayoutInflater.from(context);
            this.things = things;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            // reuse views
            ViewHolder viewHolder;
            if (convertView == null) {
                viewHolder = new ViewHolder();
                convertView = inflater.inflate(R.layout.edit_task_list_item, null);
                // configure view holder
                viewHolder.text = (TextView) convertView.findViewById(R.id.edit_task_name);
                viewHolder.editButton = (Button) convertView.findViewById(R.id.edit_task_edit);
                viewHolder.deleteButton = (Button) convertView.findViewById(R.id.edit_task_delete);
                convertView.setTag(viewHolder);
            }

            else {
                viewHolder = (ViewHolder) convertView.getTag();
            }

            viewHolder.text.setTag(position);
            viewHolder.text.setText(things.get(position).toString());
            viewHolder.deleteButton.setTag(position);
            viewHolder.editButton.setTag(position); //currently does nothing

            viewHolder.deleteButton.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View view) {
                int tag = (Integer)view.getTag();
                things.remove(tag);
                resetAdapter();
                }
            });

            return convertView;
        }
    }
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top