2012-06-19 2 views
1

안녕하세요 내 응용 프로그램에서 나는 커서에서 데이터를 가져와 ListActivity에 넣을 것입니다.목록보기를 오래 클릭하면 대화 상자가 표시됩니다. android

길게 누르시면 대화 상자를 표시하고 싶습니다. 여기

final ListView selectRoom = (ListView) findViewById(android.R.id.list); 
     selectRoom.setOnItemLongClickListener(new OnItemLongClickListener() { 

      public boolean onItemLongClick(AdapterView<?> adapter, View view, 
        int position, long id) { 

       Cursor unitCursor = (Cursor) getListView().getItemAtPosition(
         position); 
       int rid = unitCursor.getInt(0); 
       DBAccessor dba = new DBAccessor(getApplicationContext()); 
       dba.alterUnitName(rid); 
       callAdapter(); 
       final Dialog d = new Dialog(getApplicationContext()); 
       d.setContentView(R.layout.settings); 
       d.setTitle("My Dialog"); 
       d.show(); 

       return true; 

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="horizontal" > 

    <TextView 
     android:id="@+id/unitId" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:visibility="invisible"/> 

    <TextView 
     android:id="@+id/unitName" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" /> 

    <TextView 
     android:id="@+id/unitRoomId" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:visibility="invisible"/> 

</LinearLayout> 

미리 감사드립니다 ... 커서의 열

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

    <Spinner 
     android:id="@+id/spinnerRooms" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" > 
    </Spinner> 

    <ListView 
     android:id="@android:id/android:list" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_below="@id/spinnerRooms"> 
    </ListView> 

</RelativeLayout> 

XML 파일입니다 ... 목록과 XML 파일입니다 ..

+2

그리고 질문에 희망을 뭐야? –

+0

대화 상자가 나타나지 않습니다. – yama

답변

1

다음과 같이 setOnItemLongClickListener를 사용하십시오.

selectRoom.setOnItemLongClickListener(new OnItemLongClickListener(){ 
     @Override 
     public boolean onItemLongClick(AdapterView<?> arg0, View arg1, 
       int position, long arg3) { 
      // TODO Auto-generated method stub 
      Dialog(); 
    or 
final MyDialog rDialog=new MyDialog(this,this); 
      return false; 
     } 

    }); 

public void Dialog() { 

     AlertDialog.Builder dialogAlert = new AlertDialog.Builder(getApplicationContext()); 
     dialogAlert.setTitle("Demo ?"); 

     dialogAlert.show(); 
} 

//Another way 

public class MyDialog extends Dialog { 

Activity act; 
public ReminderSettingDialog(Context context,Activity act) { 
    super(context); 
    this.act=act; 
    // TODO Auto-generated constructor stub 
} 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 
    requestWindowFeature(Window.FEATURE_NO_TITLE); 
    setContentView(R.layout.settings); 

} 

}

+2

그는 이미'setOnItemLongClickListener'를 지정했습니다. 이 답변에 새로운 내용이 있습니까? –

+0

@munish 이미 ListActivity를 확장했습니다 – yama

+0

MyDialog 클래스를 별도로 만들고 ListActivity –

0

이 코드를보십시오 :

selectRoom.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() { 
     @Override 
     public boolean onItemLongClick(AdapterView<?> av, View v, int pos, long id) { 
      return onLongListItemClick(v,pos,id); 
     } 
     protected boolean onLongListItemClick(View v, final int pos, long id) { 
      /////Display Dialog Here....................... 

        return true; 

    }); 
+0

에서 MyDialog 객체를 사용할 수 있습니다. onLongListItemClick() 메서드를 사용할 수 없습니다. – yama

0

대화 그 같은 DIALG 빌더 사용하여 작성해야합니다

AlertDialog alertDialog = new AlertDialog.Builder(view.getContext()).create(); 
alertDialog.setTitle("Reset..."); 
alertDialog.setMessage("Are you sure?"); 
alertDialog.setButton("OK", new DialogInterface.OnClickListener() { 
    public void onClick(DialogInterface dialog, int which) { 
     // here you can add functions 
    } 
}); 
alertDialog.setIcon(R.drawable.icon); 
alertDialog.show(); 

이 도움이 :)

관련 문제