简体   繁体   中英

Simple Todo android app with mlab dont add or edit

I created an application with android studio and connected it to mLab, the application is a simple task list (toDo App). I can see perfectly the records of the mLab database. The problem is that I can not edit or create new records. Deleting works perfects, If I delete them, they are removed from the app and mLab. I do not see any visible error. Anyone can help me to spot the error?

i leave here the repo from my project TodoApp

MainActivity.java

public class MainActivity extends AppCompatActivity {


ListView lstViewItems;
Button btnAdd, btnEdit, btnDelete;
EditText edtItem;
item itemSelected=null;
List<item> items = new ArrayList<item>();


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    lstViewItems = (ListView)findViewById(R.id.lstView);
    btnAdd = (Button)findViewById(R.id.btnAddItem);
    btnEdit = (Button)findViewById(R.id.btnEdit);
    btnDelete = (Button)findViewById(R.id.btnDelete);
    edtItem = (EditText) findViewById(R.id.edtItem);

    //LOAD DATA WHEN APP OPENED
    new GetData().execute(Common.getAddressAPI());

    lstViewItems.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
           itemSelected = items.get(position);
           //SET TEXT TO EDIT TEXT
            edtItem.setText(itemSelected.getItem());
        }
    });



    //ADD EVENT TO BUTTON

    btnAdd.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View v){
            new PostData(edtItem.getText().toString()).execute(Common.getAddressAPI());
        }
    });
//BECAUSE THIS FUNCTION WE NEED PARAMETER ITEMSELECTED, SO WE NEED SET ITEMSELECTED
    //WHEN USER CLICK ON ITEM IN LISTVIEW
    btnEdit.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View v){

        }
    });

    btnDelete.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View v){
            new DeleteData(itemSelected).execute(Common.getAddressSingle(itemSelected));

        }

    });



}

//FUNCTION PROCESS DATA
class GetData extends AsyncTask<String, Void, String>{
    ProgressDialog pd = new ProgressDialog(MainActivity.this);

    @Override
    protected void onPreExecute(){
        super.onPreExecute();
        //Pre process
        pd.setTitle("Please wait...");
        pd.show();
    }

    @Override
    protected String doInBackground(String... params){
        // RUNNING PROCCESS..
        String stream = null;
        String urlString = params[0];

        HTTPDataHandler http = new HTTPDataHandler();
        stream = http.GetHTTPData(urlString);
        return stream;
    }

    @Override
    protected void onPostExecute(String s){
        super.onPostExecute(s);
        //Done process
        //WE WILL USE GSON TO PARSE JSON TO CLASS
        Gson gson = new Gson();
        Type listType = new TypeToken<List<item>>(){}.getType();
        items=gson.fromJson(s,listType); // PARSE TO LIST
        CustomAdapter adapter = new CustomAdapter(getApplicationContext(),items); // CREATE ADAPTER
        lstViewItems.setAdapter(adapter); // SET ADAPTER TO LIST VIEW
        pd.dismiss();
    }
}



// FUNCTION TO ADD NEW ITEM
class PostData extends AsyncTask<String,String,String> {
    ProgressDialog pd = new ProgressDialog(MainActivity.this);
    String item;

    public PostData(String item){
        this.item = item;
    }



    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pd.setTitle("Please wait...");
        pd.show();
    }

    @Override
    protected String doInBackground(String... params) {
        String urlString = params[0];

        HTTPDataHandler hh = new HTTPDataHandler();
        String json="(\"item\":\""+item+"\")";
        hh.PostHTTPData(urlString,json);
        return "";
    }

    @Override
    protected void onPostExecute(String s) {
        super.onPostExecute(s);


        //REFRESH DATA
        new GetData().execute(Common.getAddressAPI());
        pd.dismiss();
    }
}


// FUNCTION TO EDIT ITEM
class PutData extends AsyncTask<String,String,String> {
    ProgressDialog pd = new ProgressDialog(MainActivity.this);
    String item;

    public PutData(String item){
        this.item = item;
    }



    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pd.setTitle("Please wait...");
        pd.show();
    }

    @Override
    protected String doInBackground(String... params) {
        String urlString = params[0];

        HTTPDataHandler hh = new HTTPDataHandler();
        String json="(\"item\":\""+item+"\")";
        hh.PutHTTPData(urlString,json);
        return "";
    }

    @Override
    protected void onPostExecute(String s) {
        super.onPostExecute(s);


        //REFRESH DATA
        new GetData().execute(Common.getAddressAPI());
        pd.dismiss();
    }
}

// FUNCTION TO DELETE ITEM
class DeleteData extends AsyncTask<String,String,String> {
    ProgressDialog pd = new ProgressDialog(MainActivity.this);
    item item;

    public DeleteData(item item){
        this.item = item;
    }



    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pd.setTitle("Please wait...");
        pd.show();
    }

    @Override
    protected String doInBackground(String... params) {
        String urlString = params[0];

        HTTPDataHandler hh = new HTTPDataHandler();
        String json="(\"item\":\""+item.getItem()+"\")";
        hh.DeleteHTTPData(urlString,json);
        return "";
    }

    @Override
    protected void onPostExecute(String s) {
        super.onPostExecute(s);


        //REFRESH DATA
        new GetData().execute(Common.getAddressAPI());
        pd.dismiss();
    }
}

}

}

The first error is that you are sending a wrong json format so you need to change it to the right format, second you are indexing the id variable but you are noting sending it. change your json variable in the post method to :

String json = "{'item' :'" + item + "', 'id' : '"+ UUID.randomUUID().toString() +"'}";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM