删除或隐藏列表视图中的行

时间:2014-08-28 09:08:40

标签: android simplecursoradapter onitemclick

大家好我在列表视图中删除或隐藏列表时遇到问题,例如javascript中的.hide()。我知道这里有很多答案,但它似乎没有回答我的问题。为了清楚地解释,下面是我的代码的一部分。

    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;

        }

}

2 个答案:

答案 0 :(得分:0)

在这里你可以做这样的事情,它会以对话框的形式询问你是否天气删除记录,点击是后会删除记录。希望这有助于你...

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;
        }
    });
}

答案 1 :(得分:0)

将这些行放入你的onlistitemclick

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

并在belooo中添加此行

adapter.notifyDataSetChanged();