2012-04-21 5 views
2

나는 android를 처음 사용했습니다. 나는 편집 및 삭제 버튼이있는 학생들의 목록보기가있는 응용 프로그램을 개발 중입니다.여러 컨트롤이있는 안드로이드 목록보기


STUDENT LIST 

[ABC]                             [편집]                추천3210          

[DEF]                             [편집]                        을 [삭제]  난에 함께 레코드의 ID를 저장하는 데 필요 <Textview>

public class DataBaseDemoActivity extends ListActivity { 
/** Called when the activity is first created. */ 
    SQLiteDatabase db; 
    Button btnInsert; 
    ArrayAdapter<String> students; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    try{ 
    db=openOrCreateDatabase("StudentDB",SQLiteDatabase.CREATE_IF_NECESSARY,null); 

    Cursor c=db.rawQuery("SELECT * FROM temp",null); 
    String[] students = new String[c.getCount()]; 

    if (c.moveToFirst()) 
    {      
     for (int i = 0; i < c.getCount(); i++) 
     { 
      students[i] = c.getString(0).toString()+" "+c.getString(1).toString(); 
      c.moveToNext(); 
     }   
    } 
    c.close(); 

    setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, students)); 

    ListView lv = getListView(); 
    lv.setTextFilterEnabled(true); 

    lv.setOnItemClickListener(new OnItemClickListener() { 
    public void onItemClick(AdapterView<?> parent, View view, 
     int position, long id) { 
     // When clicked, show a toast with the TextView text 
     Toast.makeText(getApplicationContext(), ((TextView) view).getText(), 
      Toast.LENGTH_SHORT).show(); 
    } 
    }); 


    }catch(SQLException e) 
    { 
    } 
} 

}

에서 학생 세부 정보를 표시 할 수이 코드 메신저로


[삭제]를 목록보기를 사용하면 편집 또는 삭제 버튼을 클릭 할 때 ID를 찾아서 DB에서 변경해야합니다. 값을 두 필드로 설정하는 방법은 <TextView> [세부 정보 표시] 및 <EditText> [visibility : insisible - 레코드의 id 저장]입니다. 위의 코드를 사용하여 세부 정보를 <TextView> (으)로 가져올 수 있습니다. 모든 제안을 주시면 감사하겠습니다.

UPDATE :

레이아웃 난 당신이 정수 "위치"..을 얻고 그쪽으로 값을 얻을 것이다 온 클릭 방법이 선 후

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="horizontal"  
    > 
    <EditText 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:visibility="invisible" 
     android:inputType="text" 
     android:onClick="showInfo" 
     /> 
    <TextView xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="230dip" 
     android:layout_height="wrap_content" 
     android:textSize="16sp" 
     android:id="@+id/studentInfo" > 
    </TextView> 

    <Button 
     android:id="@+id/button1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@string/action" 
     android:onClick="showInfo" 
     /> 

</LinearLayout> 
+0

에서

봐 "는 레코드의 ID는"텍스트 데이터베이스 레코드 ID를 의미? M : 맞지? – user370305

+0

@ user370305 : 데이터베이스 레코드의 ID를 의미합니다. –

답변

1

커서 데이터 가져 오기 루프 내에서 ID를 가져올 수 있습니다. HashMap<id,String> 콜렉션을 사용하고 데이터 (텍스트)와 함께 ID를 저장하십시오.

요구 사항에 따라 ArrayAdapter 대신 SimpleCursorAdapter을 사용하고 ListView을 사용하는 것이 좋습니다.

목록을 사용하여 데이터베이스 레코드를 쉽게 관리하고 목록 항목을 클릭하면 특정 레코드에 대한 데이터베이스와 관련된 모든 정보를 얻을 수 있습니다. SimpleCursorAdapters and ListViews

Creating a custom CursorAdapter for Android

1

..

students[i] = c.getString(0).toString()+" "+c.getString(1).toString(); 
    student_ids[i]=Integer.parseInt(//get the id...) 
    //and you dont need to put it in invisible edittext... 

을 사용하고 있습니다 해당 위치의 students_ids []

int id=students_ids[position]; 
+0

편집 버튼을 클릭하면 정수 위치를 어떻게 얻을 수 있습니까? .. 제발 .. 제발 .. –

+0

버튼은 listview의 행의 일부가 아니십니까? – 5hssba

+0

예 버튼이 행 내에 있습니다. 업데이트 된 질문이 있습니다 .. 내 레이아웃을 추가했습니다. 확인해주십시오. –