2013-02-16 1 views
1

ListView Row에서 작동하는 버튼을 얻으려고합니다. 사용자 지정 어댑터에 대해 읽었는데 제대로 작동하지 않습니다. 기본적으로 내가 4 TextView 및 다른 클래스 ListView fieds 1 단추가있는 List_item 클래스가 있습니다. textView 중 하나에 id/Name이 있습니다. 그래서 내가 원하는 것은 그 행의 버튼을 누를 때입니다. TextView "name"값을 가져와 ArrayList에 추가하고 다른 행의 다른 버튼을 누르면 동일한 arraylist에 "name"의 값을 추가합니다. 그래서 기본적으로 버튼에 눌린 모든 것들을 ArrayList에 추가 할 것입니다. 그게 가능한가 ??ListView의 각 행에 대한 사용자 정의 레이아웃

그냥 추가하십시오. textView는 내 데이터베이스에 의해 피드됩니다.

List_item.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/RelativeLayout1" 
android:layout_width="fill_parent" 
android:layout_height="match_parent" 
android:orientation="vertical" 
android:paddingBottom="10dp" > 

<TableRow 
    android:id="@+id/tableRow1" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:baselineAligned="false" > 

    <TextView 
     android:id="@+id/id" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:textColor="#FFFFFF" 
     android:visibility="gone" /> 

    <TextView 
     android:id="@+id/name" 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:layout_gravity="left" 
     android:layout_weight="3" 
     android:textAllCaps="true" 
     android:textColor="#FFFFFF" 
     android:textSize="30sp" 
     android:textStyle="bold" 
     android:typeface="serif" /> 

    <TextView 
     android:id="@+id/price" 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:layout_gravity="center" 
     android:layout_weight="1" 
     android:gravity="right" 
     android:paddingRight="15dp" 
     android:textAllCaps="true" 
     android:textColor="#FFFFFF" 
     android:textSize="30sp" 
     android:textStyle="bold" 
     android:typeface="serif" /> 

    <Button 
     android:id="@+id/order" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="right" 
     android:layout_weight="0" 
     android:focusable="false" 
     android:minHeight="0dp" 
     android:minWidth="50dp" 
     android:text="+" 
     android:textStyle="bold" /> 

</TableRow> 

<TableRow 
    android:id="@+id/tableRow2" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:layout_below="@+id/tableRow1" 
    android:gravity="center_horizontal" > 

    <TextView 
     android:id="@+id/description" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:textAllCaps="true" 
     android:textColor="#FFFFFF" 
     android:textSize="20sp" 
     android:textStyle="bold" 
     android:typeface="serif" 
     android:visibility="gone" /> 

</TableRow> 

</RelativeLayout> 

내 menu.xml는

<ListView 
    android:id="@android:id/list" 
    android:layout_width="fill_parent" 
    android:layout_height="697dp" > 
</ListView> 

지금 내가 그 행의 버튼을 누를 때 텍스트 뷰 "이름"의 값을 얻으려면있다.

은 내가 버튼을 추가하기 전에 내 목록보기를 채우는 데 사용하는 방법이며하지만 지금은 내가 reaserched 그렇게 모든 값을하지 않는 버튼을 추가하고 내가 발견 한 후 미세

ListAdapter adapter = new SimpleAdapter (getCategoryItems.this,itemsList,R.layout.list_item, new String[]{ TAG_ID, TAG_NAME,TAG_PRICE, TAG_DESCRIPTION},new int[] { R.id.id, R.id.name, R.id.price, R.id.description}); 
setListAdapter(adapter); 

을 일하고 있었다 내 sampleAdapter를 사용자 지정 어댑터로 변경해야합니다. 그리고 getView() 내부 버튼을 추가하지만 그 걸릴 수 없습니다.

Button order = (Button) view.findViewById(R.id.order); 
order.setOnClickListener(new Button.OnClickListener() { 
    @Override 
    public void onClick(View v) { 
     String name = ((TextView)view.findViewById(R.id.name)).getText().toString(); 
     Log.d("Selected : ", name); 
    } 
}); 
+2

자신이하는 일을 설명하기 위해 최소한의 코드를 게시하십시오. –

+0

postexceute()에서 listAdapter를 수행하기 전에 백그라운드 메소드를 추가했습니다. –

+1

귀하의 질문은 JSON과 관련이 없기 때문에 하드 코딩 된 데이터로 간단한 예제를 작성하는 것이 좋습니다. 도움이되는 –

답변

0

당신은 당신의 ListView에 대한 사용자 정의 어댑터 클래스를 작성해야합니다 OnClickListener를

내 버튼을 누릅니다. 데이터베이스에서 데이터를 가져 오는 중이므로 CursorAdapter을 확장하고 newView()bindView()을 무시하는 것이 좋습니다. 내 경험에 비추어 볼 때이 작업을 올바르게 수행하는 방법은 문서화가 잘되어 있지 않으므로 여기에서 몇 가지 지침을 알려 드리겠습니다.

newView()에는 ListActivity에서 getLayoutInflator()으로 전화하여 제공되는 LayoutInflator을 사용해야합니다. 이 LayoutInflatorinflate()을 호출하면 특정 행에 대한 Button을 가져 와서 OnClickListener을 설정할 수 있습니다.

bindView()에서 Android가 기존 View 객체를 다시 사용하도록 요청하기 때문에 새로운보기를 부 풀 필요가 없다는 점을 제외하고는 거의 동일한 작업을 수행합니다. 반복되는 코드의 양을 줄이기 위해 몇 가지 추가 메서드를 만들 수 있습니다.

코드 예제가 아닌 영어로 설명되어있어도 도움이되기를 바랍니다.

+0

고마워요. 나는 당신이 저에게 제공 한 정보로 그것을 통해 내 방식대로 일할 것입니다. –

+0

@MoAdel 그냥'CursorAdapter'보다는'SimpleCursorAdapter'를 확장하는 방법을 생각해 볼 수 있습니다. 'TextView'를 채우기 위해 그것을 활용할 수 있습니다. 따라서 수동으로 할 필요는 없습니다. –