견고한 다리

다운로드
 

MainActivity.java

package kr.razberry.test; 
 
import android.content.Context; 
import android.database.Cursor; 
import android.database.sqlite.SQLiteDatabase; 
import android.database.sqlite.SQLiteException; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.widget.AdapterView; 
import android.widget.EditText; 
import android.widget.LinearLayout; 
import android.widget.ListView; 
import android.widget.Toast; 
import java.util.Vector; 
 
 
public class MainActivity extends AppCompatActivity { 
    //Vector cardVec = new Vector(); 
    DBHelper helper; 
    SQLiteDatabase db; 
    EditText nameTxt; 
    EditText telTxt; 
    ListView cardlistView = null; 
    Friend clickedFriend = null; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.activity_main); 
        helper = new DBHelper(this); 
        try { 
            db = helper.getWritableDatabase(); 
        } 
        catch (SQLiteException ex) { 
            db = helper.getReadableDatabase(); 
        } 
        nameTxt = (EditText) findViewById(R.id.nameTxt); 
        telTxt = (EditText) findViewById(R.id.telTxt); 
        cardlistView = (ListView)findViewById(R.id.listview); 
 
        cardlistView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
            @Override 
            public void onItemClick(AdapterView <?> parent, View v, int position, long id) { 
                nameTxt.setText(((EditText)(v.findViewById(R.id.nameTxt))).getText().toString()); 
                telTxt.setText(((EditText)(v.findViewById(R.id.telTxt))).getText().toString()); 
                clickedFriend = (Friend)v.getTag(); 
                Toast.makeText(getBaseContext(),clickedFriend.getName(),Toast.LENGTH_LONG).show(); 
            } 
        }); 
    } 
 
    public void makeToast(View V) { 
        Toast.makeText(getBaseContext(),"btnClicked" ,Toast.LENGTH_SHORT).show(); 
    } 
    public void insertCard(View v) { 
        String name = nameTxt.getText().toString(); 
        String tel = telTxt.getText().toString(); 
        db.execSQL("INSERT INTO contacts VALUES (null, '" + name + "', '" + tel + "');"); 
        Toast.makeText(getApplicationContext(),"성공적으로 추가되었음",Toast.LENGTH_SHORT).show(); 
        nameTxt.setText(""); 
        telTxt.setText(""); 
        showList("SELECT * FROM contacts"); 
    } 
    public void updateCard(View v) { 
        if(clickedFriend == null) { 
            return; 
        } 
 
        String name = nameTxt.getText().toString(); 
        String tel = telTxt.getText().toString(); 
       db.execSQL("update contacts set name='" + name + "', tel='" + tel + "' where name='" + clickedFriend.getName() + "';"); 
        showList("SELECT * FROM contacts"); 
    } 
 
    public void deleteCard(View v) { 
        if(clickedFriend == null) { 
            return; 
        } 
        db.execSQL("delete from contacts where name = '" + clickedFriend.getName() + "'; "); 
        showList("SELECT * FROM contacts"); 
    } 
 
    public void searchCard(View v) { 
        String name = nameTxt.getText().toString(); 
        String tel = telTxt.getText().toString(); 
 
        showList("SELECT * FROM contacts where name like '%" + name + "%' and tel like '%" + tel +"%'"); 
    } 
    private void showList(String s) { 
        Cursor cursor = db.rawQuery(s, null); 
        startManagingCursor(cursor); 
        String[] from = { "name" , "tel" }; 
        int[] to = { R.id.nameTxt, R.id.telTxt }; 
        MyDBCursorAdapter adapter = new MyDBCursorAdapter(this, R.layout.cardlayout, cursor, from, to); 
 
        cardlistView.setAdapter(adapter); 
 
        clickedFriend = null; 
        nameTxt.setText(""); 
        telTxt.setText(""); 
    } 
 
 
}