안드로이드 개발 질문/답변
(글 수 45,052)
ListActivity 을 상속받아 리스트를 만들었습니다 .
리스트 아이템의 클릭은 onListItemClick 으로 해결했는데요
이건 한번누르면 반응하는데 ..
길게 눌렀을때는 삭제를하고
짧게 한번눌렀을때는 웹뷰를 보여주려고 합니다 .
길게 눌렀을때는 어떤걸 사용해야 하나요 ..
리스트 아이템의 클릭은 onListItemClick 으로 해결했는데요
이건 한번누르면 반응하는데 ..
길게 눌렀을때는 삭제를하고
짧게 한번눌렀을때는 웹뷰를 보여주려고 합니다 .
길게 눌렀을때는 어떤걸 사용해야 하나요 ..
2010.04.04 20:21:50
롱클릭 시에는 Context Menu 뜨게 되어 있습니다.
http://developer.android.com/reference/android/app/Activity.html#registerForContextMenu(android.view.View) 참고해 보세요.
2010.04.05 22:04:05
package cont.eye;
import java.util.ArrayList;
import android.R.integer; import android.app.ListActivity; import android.content.Context; import android.content.Intent;
import android.database.Cursor; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.os.Bundle; import android.util.Log; import android.view.ContextMenu; import android.view.KeyEvent; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; import android.view.ContextMenu.ContextMenuInfo;
import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.ImageView; import android.widget.ListView; import android.widget.TextView;
/**
* 1. DB 삭제 기능
* 2. 선택한 프로덕트 아이디로 웹뷰 보여주기 <완료>
* 3. 롱클릭시 삭제 기능
* 3. 메뉴 만들어서 전체 삭제 기능넣기
* @author Administra
*
*/
public class history extends ListActivity {
private ArrayList<his_class> his_list = new ArrayList<his_class>();
private history_adapter his_ada;
static final private int DB_CLEAR = Menu.FIRST;
static final private int RECODE_DELETE = Menu.FIRST+1;
private db_adapter DB_HIS = new db_adapter(this);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//속성클래스 선언
his_class h1 = new his_class();
// 접속해서 xml 받아 온다음 db에삽입
HttpConn http_xml_recv = new HttpConn(this, "1");
//배열 아답터 선언
his_ada = new history_adapter(this, R.layout.history,his_list);
// db 내용을 his_class에 삽입
insert_arrylist(this);
setListAdapter(his_ada);
}
/* @Override
public boolean onKeyLongPress(int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
super.onKeyLongPress(keyCode, event);
// 삭제
db_id_remove(his_list.get(this.getSelectedItemPosition()).KEY_ID);
Log.w("-->","DELETEOK");
return true;
}*/
/* @Override
public void registerForContextMenu(View view) {
Log.w("-->","registerForContextMenu "+view.toString());
super.registerForContextMenu(view);
}*/
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
// 웹뷰
Log.w("-->", "position"+Integer.toString(position)+" ID="+Long.toString(id)+" = "+his_list.get(position).productidID);
Intent ID_WEB_VIEW = new Intent(this, eye_view_main.class);
ID_WEB_VIEW.putExtra("productidID", his_list.get(position).productidID);
startActivity(ID_WEB_VIEW);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
MenuItem item_db_clear = menu.add(0,DB_CLEAR,Menu.NONE,R.string.db_clear);
MenuItem item_db_recode_clear = menu.add(0,RECODE_DELETE, Menu.NONE , R.string.recode_delete);
item_db_clear.setIcon(R.drawable.icon);
item_db_recode_clear.setIcon(R.drawable.icon);
return true;
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
// TODO Auto-generated method stub
super.onCreateContextMenu(menu, v, menuInfo);
menu.setHeaderTitle("DELETE");
menu.add(0, RECODE_DELETE, Menu.NONE, R.string.recode_delete);
}
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
super.onPrepareOptionsMenu(menu);
MenuItem removeitem = menu.findItem(RECODE_DELETE);
removeitem.setTitle(R.string.recode_delete);
removeitem.setVisible(true);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
super.onOptionsItemSelected(item);
switch (item.getItemId()) {
case DB_CLEAR:
Log.w("-->","------------------->DB_CLEAR");
//db_all_clear();
break;
case RECODE_DELETE:
Log.w("-->","------------------->onOptionsItemSelected------>RECODE_DELETE");
break;
default:
break;
}
return true;
}
@Override
public boolean onContextItemSelected(MenuItem item) {
// TODO Auto-generated method stub
super.onContextItemSelected(item);
switch (item.getItemId()) {
case (RECODE_DELETE): {
Log.w("-->","------------------->onOptionsItemSelected------>RECODE_DELETE");
/* AdapterView.AdapterContextMenuInfo menuInfo = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
int index = menuInfo.position;
db_id_remove(index);*/
return true;
}
}
return true;
}
private void db_all_clear(){
his_ada.notifyDataSetChanged();
}
private void db_id_remove(Long id){
DB_HIS.open();
if(DB_HIS.deleterecode(id)){
Log.w("-->","TRUE");
}else{
Log.w("-->","FALSE");
}
// db 내용을 his_class에 삽입
insert_arrylist(this);
his_ada.notifyDataSetChanged();
}
/**
* ArrayAdapter 에 DB 의 내용을 전부 끌어와서 삽입한다
* @param context 컨택스트를 받아온다
* @return
*/
private void insert_arrylist(Context context){
Log.w("-->", "insert_arrylist start");
/*his_class his_insert = new his_class();*/
Cursor db_cursor;
//DB 열기
DB_HIS.open();
// DB 의 모든내용을 가져온다
db_cursor = DB_HIS.fetchallrecode();
startManagingCursor(db_cursor);
// 쿼리시작
db_cursor.requery();
// 배열 아답터의 내용지우기
his_list.clear();
// 배열아답터에 내용 채워넣기
if(db_cursor.moveToFirst()){
do{
his_class his_insert = new his_class();
his_insert.his_set(
db_cursor.getLong(DB_HIS.COLUMN_KEY),
db_cursor.getString(DB_HIS.COLUMN_PRODUCTID),
"",
db_cursor.getString(DB_HIS.COLUMN_NAME)+ db_cursor.getLong(DB_HIS.COLUMN_KEY),
db_cursor.getString(DB_HIS.CLOUMN_VOTE),
db_cursor.getString(DB_HIS.CLOUMN_PRICE));
his_list.add(0,his_insert);
}while(db_cursor.moveToNext());
}
Log.w("-->","INSERTOK");
his_ada.notifyDataSetChanged();
DB_HIS.close();
Log.w("-->","insert_arrylist _ END");
}
static class line_layout{
ImageView IV;
TextView TV_TITLE;
TextView TV_VOTE;
TextView TV_PEICE;
}
private class history_adapter extends ArrayAdapter<his_class>{
private Bitmap defaultbitmap;
private ArrayList<his_class> his_items;
public history_adapter(Context context, int textViewResourceId, ArrayList<his_class> objects) {
super(context, textViewResourceId, objects);
this.his_items = objects;
defaultbitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.icon);
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if(v == null){
LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.history, null);
}
/*his_class hc = his_items.get(position);*/
his_class hc = getItem(position);
line_layout line_layout_set = new line_layout();
if(hc != null){
line_layout_set.IV =(ImageView) v.findViewById(R.id.image01);
line_layout_set.TV_TITLE = (TextView) v.findViewById(R.id.hi_title);
line_layout_set.TV_VOTE = (TextView) v.findViewById(R.id.hi_text1);
line_layout_set.TV_PEICE = (TextView) v.findViewById(R.id.hi_text2);
}
line_layout_set.IV.setImageBitmap(defaultbitmap);
line_layout_set.TV_TITLE.setText(hc.title);
line_layout_set.TV_VOTE.setText(hc.vote);
line_layout_set.TV_PEICE.setText(hc.price);
return v;
}
} // baseadapter
public class his_class{
private Long KEY_ID = new Long(0);
private String bitmapsrc = "bitmapsrc";
private String title = "title";
private String vote = "vote";
private String price = "price";
private String productidID = "productidID";
public his_class(String bitmapsrc, String title, String vote, String price) {
this.bitmapsrc = bitmapsrc;
this.title = title;
this.vote = vote;
this.price = price;
}
public his_class(){
}
public void his_set(Long key_id,String productidID ,String bitmapsrc, String title, String vote, String price){
/*Log.w("-->","KeyId="+key_id+" bitmapsrc="+ bitmapsrc+" title="+title+" vote="+vote+ " price="+price);
*/
this.KEY_ID = key_id;
this.bitmapsrc = bitmapsrc;
this.productidID = productidID;
this.title = title;
this.vote = vote;
this.price = price;
}
public String getbitmapsrc(){
return bitmapsrc;
}
public String gettitle(){
return title;
}
public String getvote(){
return vote;
}
public String getprice(){
return price;
}
}
}
이렇게 구성했지만 원하는 클릭 웹뷰 롱클릭 삭제는 안되는건가요 ㅠ_ㅠ
2010.04.05 22:14:49
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//속성클래스 선언
his_class h1 = new his_class();
// 접속해서 xml 받아 온다음 db에삽입
HttpConn http_xml_recv = new HttpConn(this, "1");
//배열 아답터 선언
his_ada = new history_adapter(this, R.layout.history,his_list);
// db 내용을 his_class에 삽입
insert_arrylist(this);
setListAdapter(his_ada);
this.registerForContextMenu(getListView());
}이렇게 하는거군요 ㅠ_ㅠ 감사합니다 ㅎㅎ



