2011-08-02 4 views
1

ListView가있는 Android 앱이 있습니다. 나는 두 TextViews과 버튼 다음에 체크 박스를 가지고 내 자신의 항목 레이아웃을 만든 : 사용자가 행을 탭하면Android ListView 및 이벤트 버블 링

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout android:id="@+id/linearLayout1" android:layout_width="fill_parent" android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android"> 
    <CheckBox android:id="@+id/checkBox1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginRight="5dip"></CheckBox> 
    <RelativeLayout android:layout_height="match_parent" android:id="@+id/relativeLayout1" android:layout_width="match_parent"> 
     <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/button1" android:layout_alignParentRight="true" android:text="Button"></Button> 
     <TextView android:id="@+id/textView1" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceLarge" android:text="TextView" android:layout_toLeftOf="@+id/button1" android:layout_alignTop="@+id/button1" android:layout_width="match_parent"></TextView> 
     <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceSmall" android:text="TextView" android:layout_below="@+id/textView1" android:layout_alignLeft="@+id/textView1" android:layout_alignRight="@+id/textView1"></TextView> 
    </RelativeLayout> 
</LinearLayout> 

, 나는 그들이 체크 박스를 누른 경우에 따라 다른 동작을 원하지는 버튼 또는 행 자체 (즉, CheckBox 또는 버튼 이외의 항목). 여기에 지금까지 (사용 setOnItemClickListener)를 구현하려고했습니다 무엇 : 나는 응용 프로그램을 실행하고 TextViews 중 하나를 클릭하면

package com.camelconsultants.shoplist; 

import java.util.ArrayList; 
import java.util.HashMap; 

import org.json.JSONArray; 
import org.json.JSONException; 
import org.json.JSONObject; 

import android.app.ListActivity; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.AdapterView; 
import android.widget.AdapterView.OnItemClickListener; 
import android.widget.ListView; 
import android.widget.SimpleAdapter; 
import android.widget.TextView; 
import android.widget.Toast; 

public class HelloAndroid extends ListActivity implements OnItemClickListener 
    { 
     ArrayList<HashMap<String, String>> Items = new ArrayList<HashMap<String, String>>(); 

     /** Called when the activity is first created. */ 
     @Override public void onCreate(Bundle savedInstanceState) 
      { 
       super.onCreate(savedInstanceState); 

       GetData(); 

       setListAdapter 
        ( 
         new SimpleAdapter 
          (
           this.getBaseContext(), 
           Items, 
           R.layout.multiitem, 
           new String[]{ "Code", "Description" }, 
           new int[]{ R.id.textView1, R.id.textView2 } 
          ) 
        ); 

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

       lv.setOnItemClickListener(this); 
      } 

     public void onItemClick(AdapterView<?> parent, View view, int position, long id) 
      { 
       Toast.makeText 
        (
         getApplicationContext(), 
         parent.getItemAtPosition(position).toString(), 
         Toast.LENGTH_SHORT 
        ).show(); 
      } 

     void GetData() 
      { 
       HashMap<String, String> Item; 

       try 
        { 
         JSONObject JsonObject = new JSONObject(this.getResources().getString(R.string.Json)); 
         JSONArray JsonArray = JsonObject.getJSONArray("Items"); 

         for (int i = 0; i < JsonArray.length(); i++) 
          { 
           Item = new HashMap<String, String>(); 
           Item.put("Code", JsonArray.getJSONObject(i).getString("Code")); 
           Item.put("Description", JsonArray.getJSONObject(i).getString("Description")); 

           Items.add(Item); 
          } 
        } 
       catch(JSONException Bummer) 
        { 
         Bummer.printStackTrace(); 
        } 
      } 
    } 

은, 아무 일도 발생하지 않습니다!

어떻게 접근하면 좋을까요?

답변

1

xml 정의에서 View에 대한 콜백을 설정할 수 있다는 것을 알게되었습니다. 다음은 업데이트 된 XML이다 :

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout android:id="@+id/linearLayout1" android:clickable="true" android:onClick="onClick" android:layout_width="fill_parent" android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android"> 
    <CheckBox android:id="@+id/checkBox1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginRight="5dip" android:onClick="onCheckBoxClick"></CheckBox> 
    <RelativeLayout android:layout_height="match_parent" android:id="@+id/relativeLayout1" android:layout_width="match_parent"> 
     <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/button1" android:layout_alignParentRight="true" android:text="Button" android:onClick="onButtonClick"></Button> 
     <TextView android:id="@+id/textView1" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceLarge" android:text="TextView" android:layout_toLeftOf="@+id/button1" android:layout_alignTop="@+id/button1" android:layout_width="match_parent"></TextView> 
     <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceSmall" android:text="TextView" android:layout_below="@+id/textView1" android:layout_alignLeft="@+id/textView1" android:layout_alignRight="@+id/textView1"></TextView> 
    </RelativeLayout> 
</LinearLayout> 

내가 적절한 콜백 후크 코드를 업데이트했습니다

package com.camelconsultants.shoplist; 

import java.util.ArrayList; 
import java.util.HashMap; 

import org.json.JSONArray; 
import org.json.JSONException; 
import org.json.JSONObject; 

import android.app.ListActivity; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.ListView; 
import android.widget.SimpleAdapter; 
import android.widget.Toast; 

public class HelloAndroid extends ListActivity 
    { 
     ArrayList<HashMap<String, String>> Items = new ArrayList<HashMap<String, String>>(); 

     /** Called when the activity is first created. */ 
     @Override public void onCreate(Bundle savedInstanceState) 
      { 
       super.onCreate(savedInstanceState); 

       GetData(); 

       setListAdapter 
        ( 
         new SimpleAdapter 
          (
           this.getBaseContext(), 
           Items, 
           R.layout.multiitem, 
           new String[]{ "Code", "Description" }, 
           new int[]{ R.id.textView1, R.id.textView2 } 
          ) 
        ); 

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

     @SuppressWarnings("unchecked") 
     private HashMap<String, String> getItem(View view) 
      { 
       HashMap<String, String> Item = null; 

       ListView listView = getListView(); 

       int Position = listView.getPositionForView(view); 

       if (listView.getItemAtPosition(Position) instanceof HashMap) 
        Item = (HashMap<String, String>)listView.getItemAtPosition(Position); 

       return Item; 
      } 

     public void onClick(View view) 
      { 
       HashMap<String, String> Item = getItem(view); 
       if (Item == null) 
        return; 

       Toast.makeText 
        (
         getApplicationContext(), 
         Item.get("Code") + ", " + Item.get("Description"), 
         Toast.LENGTH_SHORT 
        ).show(); 
      } 

     public void onCheckBoxClick(View view) 
      { 
       HashMap<String, String> Item = getItem(view); 
       if (Item == null) 
        return; 

       Toast.makeText 
        (
         getApplicationContext(), 
         "CheckBox! - " + Item.get("Code") + ", " + Item.get("Description"), 
         Toast.LENGTH_SHORT 
        ).show(); 
      } 

     public void onButtonClick(View view) 
      { 
       HashMap<String, String> Item = getItem(view); 
       if (Item == null) 
        return; 

       Toast.makeText 
        (
         getApplicationContext(), 
         "Button! - " + Item.get("Code") + ", " + Item.get("Description"), 
         Toast.LENGTH_SHORT 
        ).show(); 
      } 

     void GetData() 
      { 
       HashMap<String, String> Item; 

       try 
        { 
         JSONObject JsonObject = new JSONObject(this.getResources().getString(R.string.Json)); 
         JSONArray JsonArray = JsonObject.getJSONArray("Items"); 

         for (int i = 0; i < JsonArray.length(); i++) 
          { 
           Item = new HashMap<String, String>(); 
           Item.put("Code", JsonArray.getJSONObject(i).getString("Code")); 
           Item.put("Description", JsonArray.getJSONObject(i).getString("Description")); 

           Items.add(Item); 
          } 
        } 
       catch(JSONException Bummer) 
        { 
         Bummer.printStackTrace(); 
        } 
      } 
    } 

getItem 기능은 getItemAtPosition에보기를 통과하여 현재 항목에 대한의 HashMap를 반환합니다. 멋진 점은보기가 중첩되었는지 여부와 상관 없다는 것입니다.

그러나 내 자신의 어댑터를 만들고 setTag 메서드를 사용하는 것이 더 효율적이라고 가정합니다. 그러나 당분간은 위의 코드가 내 목적에 부합합니다!

+0

목록 위젯에 대한 레이아웃 xml 파일을 클릭하면 절대로 효과가 없습니다! – jsDebugger

1

Link에는 이와 관련된 정보가있는 것 같습니다.

관련 문제