1

Hello everyone I have a problem in regards with the deletion or hiding of list in a list view something like .hide() in javascript . I know there are lots of answers here about it but it seems It didn't answer my problem. To explain clearly, below is some part of my code.

    package sample.wew.wew;

    import info.androidhive.sqlite.model.Message;

    import java.util.ArrayList;
    import java.util.List;

    import android.content.Intent;
    import android.database.Cursor;
    import android.os.Bundle;
    import android.support.v4.app.Fragment;
    import android.util.Log;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.AdapterView;
    import android.widget.ArrayAdapter;
    import android.widget.ListAdapter;
    import android.widget.ListView;
    import android.widget.SimpleCursorAdapter;
    import android.widget.TextView;
    import android.widget.Toast;
    import android.widget.AdapterView.OnItemClickListener;

    public class QuestionsSent extends Fragment {

        private static final String TAG = "Question";

        private static List questions;
        protected ListAdapter adapter;
        ListView theList;   

        @SuppressWarnings("deprecation")
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

            View view;

            view = inflater.inflate(R.layout.questions_main_layout, container, false);
            AskdDatabaseHelper msg_db = new AskdDatabaseHelper(getActivity());

            Cursor cursor = msg_db.FetchQuestion("S");

            String[] fromFieldNames = new String[] { "msg_from_user", "msg_message" };
            int[] toViewIDs = new int[] { R.id.tvMessage, R.id.tvMessageSender };

            adapter = new QuestionsCursorAdapter(getActivity(), // Context
                            R.layout.question_detail, // Row layout template
                            cursor, // cursor (set of DB records to map)
                            fromFieldNames, // DB Column names
                            toViewIDs // View IDs to put information in
            );

            theList = (ListView) view.findViewById(R.id.list);
            theList.setAdapter(adapter);
            theList.setOnItemClickListener(new AdapterView.OnItemClickListener() {

                @Override
                public void onItemClick(AdapterView<?> parent, View view,
                                int position, long id) {
                    /*
                    Log.v(TAG, ": NAAY GI CLICK");
                    Intent intent = new Intent(getActivity(), Sent_details.class);
                    Cursor cursor = (Cursor) adapter.getItem(position);
                    Log.d("TAG", "" + adapter.getItem(position));
                    intent.putExtra("MESSAGE_ID", cursor.getString(cursor.getColumnIndex("msg_message")));
                    startActivity(intent);
                    */

                    // HERE IS WHERE I WANT TO PUT MY CODE TO DELETE OR HIDE A LIST
                }
            });      

            if(adapter.isEmpty()){
                view = inflater.inflate(R.layout.fragments_question_new, container, false);
                ((TextView)view.findViewById(R.id.textView)).setText("No Sent Questions");
            }

            return view;

        }

}
Hope
  • 644
  • 2
  • 6
  • 21
  • you want to delete a random row/record from the list ? am i right? – Umair Aug 28 '14 at 09:12
  • @Darkie I want to delete the row that i've clicked. – Hope Aug 28 '14 at 09:14
  • so I assume you are displaying records in listview from a database ? – Umair Aug 28 '14 at 09:15
  • If you can use custom adapter, you can set any flag for the object in the clicked position, and call notifyDatasetchanged(); in getView(), you need to show by checking the condition, same for getCount() – Jithu Aug 28 '14 at 09:16
  • @Jithu can you give me a sample code that. Im a beginner in android – Hope Aug 28 '14 at 09:21

2 Answers2

0

Here you can do something like this it will ask you in the form of dialogue box to weather delete the record or not and after clicking yes it will delete the record. Hope this helps you ...

userList.setOnItemLongClickListener(new OnItemLongClickListener() 
    {
        public boolean onItemLongClick(AdapterView<?> arg0, View arg1,final int arg2, long arg3)
        {

        build = new AlertDialog.Builder(HomePage.this);
        build.setTitle("Delete " + FirstName.get(arg2));
        build.setMessage("Do you want to delete ?");
        build.setPositiveButton("Yes", new DialogInterface.OnClickListener() 
        {
            public void onClick(DialogInterface dialog, int which)
            {
            Toast.makeText(getApplicationContext(), FirstName.get(arg2) + " " + LastName.get(arg2) + " is deleted.", 3000).show();
                dataBase = mHelper.getWritableDatabase();
                dataBase.delete(DatabaseActivity.TABLE_NAME, DatabaseActivity.KEY_ID + "=" + userId.get(arg2), null);

                dataBase.close();
                displayData();
                dialog.cancel();
            }
                    });

            build.setNegativeButton("No", new DialogInterface.OnClickListener() 
            {
                public void onClick(DialogInterface dialog, int which) 
                {
                    dialog.cancel();
                    }
                });
            AlertDialog alert = build.create();
            alert.show();
            return true;
        }
    });
}
Umair
  • 6,366
  • 15
  • 42
  • 50
0

put these lines in your onlistitemclick

int count = youradapter.getCount();
for (int i = count-1; i >= 0; i--) 
{ 
adapter.remove(adapter.getItem(i).postion);
 }

and add this line in belooo

adapter.notifyDataSetChanged();