2014-08-28 63 views
1

大家好我有一個問題,就是在列表視圖中刪除或隱藏列表,像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; 

     } 

} 
+0

要刪除從隨機行/記錄列表?我對嗎? – Umair 2014-08-28 09:12:48

+0

@Darkie我想刪除我點擊過的那一行。 – Hope 2014-08-28 09:14:35

+0

所以我假設你在數據庫的listview中顯示記錄? – Umair 2014-08-28 09:15:59

回答

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

把這些線在你onlistitemclick

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

和belooo加入這一行

adapter.notifyDataSetChanged(); 
+0

謝謝。但它不起作用。將出現錯誤,說「方法remove(int)未定義類型ListAdapter」。我已經嘗試過,但沒有運氣。 – Hope 2014-08-28 09:39:16

+0

檢查我編輯的答案,然後發生錯誤將您的logcat文件 – 2014-08-28 09:52:30

+0

錯誤說「位置無法解析或不是字段」 – Hope 2014-08-28 10:04:55