2012-02-22 4 views
1

나는 체크 한 목록 항목을 삭제하는 방법에 대해 혼란 스럽다. 여기에 전체 코드를 포함 시켜서 도와주세요.데이터베이스를 사용하여 안드로이드의 특정 체크 된 목록 항목을 삭제하는 방법

먼저 북마크에 jok를 나열하십시오. JokList.Java

둘째, 어디에서 내 질문을하고 있습니까? DatabaseHelper.java

마지막으로 체크 된 항목을 삭제할 본인의 의도입니다. bookjok.java

제게 제안 해 주시기 바랍니다. 나는 1 주일 이래로이 문제를 겪고있다. 진심으로 감사드립니다.

+0

첫 번째 두 개의 링크가 동일, 그것은 사용자 정의 어댑터 및 목록 작업 방법에 대해 알아보십시오 – Raman

+0

해결하고 당신이 드리겠습니다 당신의 문제를 해결하십시오. 그런 다음 사용할 수있는 기본 데이터베이스 상호 작용을 학습합니다. – JoxTraex

+0

첫 번째 두 링크가 같으므로 수정하십시오 – Raman

답변

0

내가있는 내가 체크 목록에서 데이터를 삭제하고 붙여 넣기 샘플 코드입니다

public class ListViewwithCheckBoxesActivity extends Activity { 
/** Called when the activity is first created. */ 
private String savedNames = null,savedPhoneno = ""; 
private ListView listView; 
final String SETTING_TODOLIST = "todolist"; 
private ArrayList<String> selectedItems = new ArrayList<String>(); 
static List<PhoneList> phonelist = new ArrayList<PhoneList>(); 
static String[] names ,phoneno; 
PhonebookAdapter adapter; 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.phonebooklist); 
    listView = (ListView) findViewById(R.id.phlist);  
    phoneno = new String[]{"9441781204","95503999393","7799455550"}; 
    names = new String[]{"name1","name2","name3","name4"}; 
    for(int i =0; i<phoneno.length; i++){ 
     phonelist.add(new PhoneList(names[i], phoneno[i], false)); 

    } 
    adapter = new PhonebookAdapter(this, phonelist); 
    listView.setAdapter(adapter); 
    Button btnDelete = (Button) findViewById(R.id.btnDelete); 
    btnDelete.setOnClickListener(new OnClickListener() { 

     public void onClick(View v) { 
      String deletedItems = getDeletedItems(); 

      //Here open the database, call the query, delete from database and refresh the list and call the adapter once again 

     } 
    }); 
    Button btnClear = (Button) findViewById(R.id.btnClear); 
    btnClear.setOnClickListener(new OnClickListener() { 

     public void onClick(View v) { 
      phonelist.clear(); 
      for(int i =0; i<phoneno.length; i++){ 
       phonelist.add(new PhoneList(names[i], phoneno[i], false)); 

      } 
      adapter = new PhonebookAdapter(getBaseContext(), phonelist); 
      listView.setAdapter(adapter); 

     } 
    }); 
    Button btnSelectAll = (Button) findViewById(R.id.btnSelectAll); 
    btnSelectAll.setOnClickListener(new OnClickListener() { 

     public void onClick(View v) { 
      phonelist.clear(); 
      for(int i =0; i<phoneno.length; i++){ 
       phonelist.add(new PhoneList(names[i], phoneno[i], true)); 

      } 
      adapter = new PhonebookAdapter(getBaseContext(), phonelist); 
      listView.setAdapter(adapter); 

     } 
    }); 

} 
public class PhonebookAdapter extends BaseAdapter { 


    private LayoutInflater inflater; 
    private TextView nameTV, phonenumberTV; 

    public PhonebookAdapter(Context context, List<PhoneList> phoneList) { 
     inflater = LayoutInflater.from(context); 
     ListViewwithCheckBoxesActivity.phonelist = phoneList; 
    } 

    public int getCount() { 
     return ListViewwithCheckBoxesActivity.phonelist.size(); 
    } 

    // @Override 
    public Object getItem(int position) { 
     return ListViewwithCheckBoxesActivity.phonelist.get(position); 
    } 

    // @Override 
    public long getItemId(int position) { 
     return position; 
    } 

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

     convertView = inflater.inflate(R.layout.phonebook, null); 
     nameTV = (TextView) convertView.findViewById(R.id.nameTV); 
     nameTV.setText(ListViewwithCheckBoxesActivity.phonelist.get(position).getname()); 
     phonenumberTV = (TextView) convertView.findViewById(R.id.phonenoTV); 
     phonenumberTV.setText(ListViewwithCheckBoxesActivity.phonelist.get(position).getphoneNo()); 

     final CheckBox checkBox = (CheckBox) convertView 
       .findViewById(R.id.checkBox1); 

     checkBox.setChecked(ListViewwithCheckBoxesActivity.phonelist.get(position).getChecked()); 
     checkBox.setOnClickListener(new OnClickListener() { 

      public void onClick(View arg0) { 
       System.out.println("checkBox.isChecked()"+ checkBox.isChecked()); 
       if (checkBox.isChecked() == true) { 
        ListViewwithCheckBoxesActivity.phonelist.get(position).setChecked(true); 
       } else { 
        ListViewwithCheckBoxesActivity.phonelist.get(position).setChecked(false); 
       } 
      } 
     }); 

     return convertView; 
    } 

} 
@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    MenuInflater inflater = getMenuInflater(); 
    inflater.inflate(R.menu.menu, menu); 
    return true; 
} 
public boolean onPrepareOptionsMenu (Menu menu) { 
    /*if(optionsMenuSelected){ 
     optionsMenuSelected = false; 
     return true; 
    } 
    else{ 
     return false; 
    }*/ 
    return true; 
} 

public boolean onOptionsItemSelected(MenuItem item) { 
    switch (item.getItemId()) { 

    case R.id.showCheckboxes: 
     CheckBox checkBox1 = (CheckBox)findViewById(R.id.checkBox1); 
     checkBox1.setVisibility(1); 
     break; 

    } 
    return true; 
} 
private String getDeletedItems() { 
    String savedNames= ""; 
    String[] tempNames = null, tempPhonenos = null; 

    int count = this.listView.getAdapter().getCount(); 

    for (int i = 0; i < count; i++) { 

     if (ListViewwithCheckBoxesActivity.phonelist.get(i).getChecked() == true) { 
      savedNames += ListViewwithCheckBoxesActivity.phonelist.get(i).getname(); 
      System.out.println("savedNames is "+savedNames); 
      for(int i1 =0;i1 < names.length;i1++) 
      { 
         if(savedNames.equals(names[i])) 
         { 

          tempNames = new String[names.length - 1]; 
          tempPhonenos = new String[names.length-1]; 
          System.arraycopy(names, 0, tempNames, 0, i); 
          System.arraycopy(names, i+1, tempNames, i, names.length-i-1); 
          System.arraycopy(phoneno, 0, tempPhonenos, 0, i); 
          System.arraycopy(phoneno, i+1, tempPhonenos, i, phoneno.length-i-1); 


         } 

      } 
     } 
    } 
      phonelist.clear(); 
      for(int i1 =0; i1<tempNames.length; i1++){ 
       phonelist.add(new PhoneList(tempNames[i1], tempPhonenos[i1], false)); 

      } 
      adapter = new PhonebookAdapter(getBaseContext(), phonelist); 
      listView.setAdapter(adapter); 




    return savedNames; 
} 


} 
+0

나는 이것이 내 문제에 따라 완전히 다르다고 나는 생각하지 않는다. –

+0

내가 삭제할 수있는 완전한 소스 코드를 추가했습니다. Button btnDelete = (Button) findViewById (R.id.btnDelete); btnDelete.setOnClickListener (새 OnClickListener를() { 공공 무효 온 클릭 (보기 V) { 문자열 DeletedItems에의 =의 getDeletedItems(); // 여기에 데이터베이스 쿼리를 호출 데이터베이스에서 삭제하고 목록과 전화를 새로 고침을 엽니 다 어댑터가 다시 한번 } }); – user1203673

관련 문제