我目前正在制作一个"联系人"应用程序作为我的Android主题的最终项目。当我单击列表中的某个项目时,它应显示this,其中包含编辑和删除选项。 点击删除按钮后,它应显示this confirmation 单击确定后如何从列表视图中删除项目? 这是我的代码。
MainActivtity.java
()
ContactsAdapter.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(), "");
}
Contacts.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;
}
}
}
JSONserializer.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;
}
}
DialogShowContact.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
DeleteConfirmation.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;
}
}
答案 0 :(得分:1)
如何在单击确定后从列表视图中删除项目?
listNote.getAdapter.notifyDataSetChanged;
您的问题是如何在dialogFragment中获取ContactsAdapter的引用?
您应该致电getActivity()
并转发给MainActivtity
。然后你应该为mContactsAdapter
设置一个getter,最后调用你的deleteContact
方法来通知数据更改