dimanche 13 mars 2016

how can i make the user be able to edit item in list view in android

i have a listView and every item is a textView , and a dataBase contains 3 columns ( "ID"- "NAME" - "TAG" ) , what is showable in the list view is the name , the user enter it in the editBox and the tag associated with it and stored in the dataBase now i want when the user do a long click on each item opens a new activity contain 2 editbox one for the name and one for the tag and enter them so will update that one already clicked on it .. here is my code

database functions class :

package com.chaos.twittertask;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class DataBase {

    private SQLiteDatabase mDatabase;
    private DB_Helper helper;

    public DataBase(Context context) {
        helper = new DB_Helper(context);
    }

    protected void open() {
        mDatabase = helper.getWritableDatabase();
    }

    public void close() {
        if (mDatabase != null) {
            mDatabase.close();
        }
    }
    public Cursor getAllNames()
    {
        String[] col = { "NAME" };
        Cursor cursor = mDatabase.query(
                "task", // table name
                col, // column names
                null, // where clause
                null, // where params
                null, // groupby
                null, // having
                null // orderby
        );

        return cursor;
    }

    public Cursor getTagByID(int id) {
        String[] columns = { "TAG"};

        return mDatabase.query(
                "task", // table name
                columns, // column names
                "ID" + " = " + id, // where clause // id param. could be here or appended as it is ^
                null, // where params
                null, // groupby
                null, // having
                null // orderby
        );
    }

    public void insertTagAndName(String name,String tag) {

            ContentValues newTask = new ContentValues();
            newTask.put("NAME", name);
            newTask.put("TAG",tag);
            mDatabase.insert("task", null, newTask);

    }


    public void updateNameAndTag(int id,String name,String tag) {
        ContentValues editTask = new ContentValues();
        editTask.put("NAME", name);
        editTask.put("TAG", tag);
        mDatabase.update(
                "task", // table name
                editTask, // values
                "ID" + " = " + id, // where clause
                null // where params
        );
    }
    public void getNameId(String name) {
        mDatabase.execSQL("SELECT ID from task WHERE NAME="+name);
    }
    public void deleteTagById(int id) {
        mDatabase.delete(
                "task", // table name
                "ID" + "=" + id, // where clause
                null // where params
        );
    }

}

MatinActivity :

package com.chaos.twittertask;

import android.database.Cursor;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    private Button save;
    private EditText name;
    private EditText tag;
    private TextView headline;
    private ListView list;
    private DataBase dataBase;
    private String[] names;
    private Cursor  cursor;
    private ArrayAdapter<String> adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        save= (Button) findViewById(R.id.save);
        name=(EditText) findViewById(R.id.name);
        tag=(EditText) findViewById(R.id.tag);
        headline=(TextView) findViewById(R.id.tagged_searches);
        list=(ListView) findViewById(R.id.listView);
        dataBase=new DataBase(MainActivity.this);
        save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dataBase.insertTagAndName(name.getText().toString(), tag.getText().toString());
                name.setText("");
                tag.setText("");
                Toast.makeText(MainActivity.this, "Tag has been added successfully ",
                        Toast.LENGTH_LONG).show();
                loadData();
            }
    });
        list.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {

                return false;
            }
        });
    }

    protected void onResume() {
        super.onResume();
        loadData();
    }


    private void loadData()
    {
        int i=0;
        dataBase.open();
        cursor = dataBase.getAllNames();
        names = new String[cursor.getCount()];
        try {
            cursor.moveToFirst();
            int index_name = cursor.getColumnIndex("NAME");
            do {
                names[i] = cursor.getString(index_name);
                i++;
            }while (cursor.moveToNext());
                    adapter = new ArrayAdapter<String>(
                    this,
                    R.layout.list_item, R.id.name_item,
                    names);
            list.setAdapter(adapter);
        }catch (Exception e) {
            Toast.makeText(getApplicationContext(), e.getMessage(),
                    Toast.LENGTH_LONG).show();
        }
    }

}

Aucun commentaire:

Enregistrer un commentaire