gpt4 book ai didi

android - 使用确认对话框删除 ListView 中的项目

转载 作者:行者123 更新时间:2023-11-30 00:38:50 25 4
gpt4 key购买 nike

我目前正在制作一个“联系人”应用程序作为我的 Android 主题的最终项目。当我单击列表中的项目时,它应该显示 this使用选项编辑和删除。单击删除按钮后,应显示 this confirmation单击确定后如何从 ListView 中删除项目?这是我的代码。

MainActivtity.java

public class MainActivity extends AppCompatActivity {
ImageButton floatButton;
private ContactsAdapter mContactsAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
floatButton = (ImageButton) findViewById(R.id.imagebutton);
floatButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(),
"Add New Contact", Toast.LENGTH_LONG).show();
addContact();

}
});
mContactsAdapter = new ContactsAdapter();
ListView listNote = (ListView)findViewById(R.id.listView);
listNote.setAdapter(mContactsAdapter);
listNote.setLongClickable(true);


listNote.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
mContactsAdapter.deleteContact(position);
return true;
}
});


listNote.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int whichItem, long id) {


Contacts tempContact = mContactsAdapter.getItem(whichItem);

DialogShowContact dialog = new DialogShowContact();
dialog.sendContactSelected(tempContact);
dialog.show(getFragmentManager(), "");
Toast.makeText(getApplicationContext(),
"Item clicked", Toast.LENGTH_LONG).show();

}
});

}
public void createNewContact(Contacts n) {
mContactsAdapter.addContact(n);
}
public void editNewContact(Contacts n) {
mContactsAdapter.addContact(n);
}

public void addContact() {
DialogNewContact dialog = new DialogNewContact();
dialog.show(getFragmentManager(), "");

}

public void editContact() {
DialogEditContact dialog = new DialogEditContact();
dialog.show(getFragmentManager(), "");

}


public void deleteContact() {
DialogDeleteContact dialog = new DialogDeleteContact();
dialog.show(getFragmentManager(), "");

}

ContactsAdapter.java

public class ContactsAdapter extends BaseAdapter{
private JSONSerializer mSerializer;

List<Contacts> contactsList = new ArrayList<Contacts>();
public ContactsAdapter(){
mSerializer = new JSONSerializer("NotetoSelf.json",MainActivity.this.getApplicationContext());

try{
contactsList= mSerializer.load();
}catch (Exception e) {
contactsList = new ArrayList<Contacts>();
Log.e("Error loading notes: ", "", e);
}}
public void addContact(Contacts c){
contactsList.add(c);
saveContacts();
notifyDataSetChanged();
}

public void deleteContact(int c){
contactsList.remove(c);
saveContacts();
notifyDataSetChanged();
}



public void saveContacts(){
try{
mSerializer.save(contactsList);
}catch (Exception e){
Log.e("Error Saving notes: ","",e);
}
}
public int getCount() {
return contactsList.size();
}

public Contacts getItem(int whichItem) {
return contactsList.get(whichItem);
}


public long getItemId(int whichItem) {
return whichItem;
}

public View getView(int whichItem, View view, ViewGroup viewGroup){
if(view == null){
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.list_item, viewGroup, false);
}

//Grab a referece to all our TextView and ImageView Widgets
TextView txtName = (TextView) view.findViewById(R.id.lName);
TextView txtNum = (TextView) view.findViewById(R.id.lNum);

Contacts tempContacts = contactsList.get(whichItem);

txtName.setText(tempContacts.getmName());
txtNum.setText(tempContacts.getmNum());
return view;

}




}


}

Contacts.java

public class Contacts {
private String mName;
private String mNum;

public static final String JSON_NAME = "name";
private static final String JSON_NUM = "num";

public Contacts(JSONObject jo) throws JSONException {
mName = jo.getString(JSON_NAME);
mNum = jo.getString(JSON_NUM);
}

public Contacts(){

}

public JSONObject convertToJSON() throws JSONException{
JSONObject jo = new JSONObject();

jo.put(JSON_NAME,mName);
jo.put(JSON_NUM,mNum);
return jo;
}

public String getmName() {
return mName;
}

public void setmName(String mName) {
this.mName = mName;
}

public String getmNum() {
return mNum;
}

public void setmNum(String mNum) {
this.mNum = mNum;


}

}

JSONserializer.java

public class JSONSerializer {

private String mFilename;
private Context mContext;


public JSONSerializer(String fn, Context con)
{
mFilename = fn;
mContext = con;
}

public void save (List<Contacts> contacts) throws IOException, JSONException
{
//Make an array in JSON format
JSONArray jArray = new JSONArray();

//And load it with the notes
for (Contacts c : contacts)
jArray.put(c.convertToJSON());

//Now write it to the private disk space of our app
Writer writer = null;
try
{
OutputStream out = mContext.openFileOutput(mFilename,mContext.MODE_PRIVATE);
writer = new OutputStreamWriter(out);
writer.write(jArray.toString());
}
finally
{
if(writer != null)
{
writer.close();
}
}
}



public ArrayList<Contacts> load() throws IOException, JSONException
{
ArrayList<Contacts> contactsList = new ArrayList<Contacts>();
BufferedReader reader = null;
try
{
InputStream in = mContext.openFileInput(mFilename);
reader = new BufferedReader(new InputStreamReader(in));
StringBuilder jsonString = new StringBuilder();
String line = null;

while((line = reader.readLine()) != null)
{
jsonString.append(line);
}
JSONArray jArray = (JSONArray) new JSONTokener(jsonString.toString()).nextValue();
for(int i = 0; i < jArray.length(); i++)
{
contactsList.add(new Contacts(jArray.getJSONObject(i)));
}
}
catch(FileNotFoundException e)
{

}
finally {
if(reader!=null)
reader.close();
}
return contactsList;
}


}//--------->end of JSONserializer

DialogShowContact.java

public class DialogShowContact extends DialogFragment {
private MainActivity.ContactsAdapter mContactsAdapter;
private Contacts mContact;
@Override
public Dialog onCreateDialog(Bundle savedInstanceState){
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

LayoutInflater inflater = getActivity().getLayoutInflater();
View dialogView = inflater.inflate(R.layout.view_contact, null);

TextView txtTitle = (TextView) dialogView.findViewById(R.id.vName);
TextView txtDescription = (TextView) dialogView.findViewById(R.id.vNum);

txtTitle.setText(mContact.getmName());
txtDescription.setText(mContact.getmNum());


Button btnEdit = (Button)dialogView.findViewById(R.id.vEdit);
Button btnDelete = (Button)dialogView.findViewById(R.id.vDelete);
builder.setView(dialogView).setMessage("Contact");

btnEdit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// dismiss();

DialogEditContact myDialog = new DialogEditContact();
myDialog.show(getFragmentManager(), "123");
}
});

btnDelete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

DeleteConfirmation myDialog = new DeleteConfirmation();
myDialog.show(getFragmentManager(), "123");
// MainActivity a = new MainActivity();
//mContactsAdapter.deleteContact();



}
});
return builder.create();
}

//Receive a note from the MainActivity
public void sendContactSelected(Contacts contactsSelected){
mContact=contactsSelected;
}
}

DeleteConfirmation.java

public class DeleteConfirmation extends DialogFragment {

private MainActivity.ContactsAdapter mContactsAdapter;
public Dialog onCreateDialog (Bundle savedInstanceState){
//Use Builder class because this dialog has a simple Ui
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

//Dialog will have Make a selection as the title

builder.setMessage("Delete Contact?")

//An ok button that does nothing

.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
//(HOW TO CALL THE DELETE OR DO DELETING AFTER CLICKING OK???
}
})

// A "Cancel" button that does nothing

.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {


}
});


//Create the object and return it
return builder.create();



}//End onCreateDialog
}

最佳答案

单击确定后如何从 ListView 中删除项目?

  • 听 OK Action
  • 当用户单击“确定”时,从列表中删除联系人,然后调用

listNote.getAdapter.notifyDataSetChanged;

您的问题是如何获取dialogFragment中ContactsAdapter的引用?您应该调用 getActivity() 并转换为 MainActivtity。然后你应该为你的mContactsAdapter设置一个getter,最后调用你的deleteContact方法,它会通知数据改变

关于android - 使用确认对话框删除 ListView 中的项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42895104/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com