2012-04-09 28 views
11

Tôi có một ứng dụng Android có ListView trong đó, ListView sẽ thiết lập tốt nhưng bây giờ tôi muốn một hình ảnh trong ListView có thể nhấp được. Tôi làm điều này bằng cách sử dụng 2 lớp, lớp Activity (parent) và ArrayAdapter để điền vào danh sách. Trong ArrayAdapter tôi thực hiện một OnClickListener cho hình ảnh trong danh sách mà tôi muốn có thể nhấp được.Cách bật OnClick trong Bộ điều hợp của ListView Chức năng của hoạt động

Cho đến giờ tất cả đều hoạt động.

Nhưng bây giờ tôi muốn chạy một hàm từ lớp hoạt động khi onClick, cho hình ảnh trong danh sách, được chạy nhưng tôi không biết làm thế nào. Dưới đây là 2 lớp mà tôi sử dụng.

Đầu tiên lớp Hoạt động:

public class parent_class extends Activity implements OnClickListener, OnItemClickListener 
{ 
    child_class_list myList; 
    ListView myListView; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     // setup the Homelist data 
     myList  = new child_class_list (this, Group_Names, Group_Dates); 
     myListView = (ListView) findViewById(R.id.list); 

     // set the HomeList 
     myListView.setAdapter(myList); 
     myListView.setOnItemClickListener(this); 
    } 

    void function_to_run() 
    { 
     // I want to run this function from the LiscView Onclick 
    } 

    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) 
    { 
     // do something 
    } 
} 

Và ArrayAdapter từ nơi tôi muốn gọi một hàm từ lớp Hoạt động:

public class child_class_list extends ArrayAdapter<String> 
{ 
    // private 
    private final Context context; 
    private String[]  mName; 
    private String[]  mDate; 

    public child_class_list (Context context, String[] Name, String[] Date) 
    { 
     super(context, R.layout.l_home, GroupName); 
     this.context  = context; 
     this.mName  = Name; 
     this.mDate = Date; 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) 
    { 
     LayoutInflater inflater = (LayoutInflater) context 
       .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     View rowView = inflater.inflate(R.layout.l_home, parent, false); 

     ImageView selectable_image = (ImageView) rowView.findViewById(R.id.l_selectable_image); 
     selectable_image.setOnClickListener(new OnClickListener() 
     { 
      public void onClick(View v) 
      { 
       // I want to run the function_to_run() function from the parant class here 
      } 
     } 
     ); 

     // get the textID's 
     TextView tvName = (TextView) rowView.findViewById(R.id.l_name); 
     TextView tvDate = (TextView) rowView.findViewById(R.id.l_date); 

     // set the text 
     tvName.setText  (mName[position]); 
     tvDate.setText (mDate[position]); 

     return rowView; 
    } 
} 

Nếu có ai biết làm thế nào để chạy các chức năng trong hoạt động lớp từ arrayadapter hoặc làm thế nào để thiết lập hình ảnh onClickListener trong lớp hoạt động, tôi sẽ giúp đỡ rất nhiều.

Trả lời

44

Bên onClick() Làm như thế này:

((ParentClass) context).functionToRun(); 
+0

Tuyệt vời. Đơn giản và hiệu quả. –

8

Chỉ cần cho rõ ràng để mở rộng câu trả lời cung cấp Trong một BaseAdapter bạn có thể nhận được các lớp cha mẹ bằng cách gọi this.getActivity(); Nếu bạn sau đó định kiểu này đến lớp hoạt động thực tế bạn sau đó có thể gọi một hàm theo @AdilSoomro trả lời dưới đây để bạn thực sự kết thúc với một cái gì đó như thế này

public class MyAdapter extends BaseAdapter<Long> { 
    public MyAdapter(Activity activity, 
      TreeStateManager<Long> treeStateManager, int numberOfLevels) { 
     super(activity, treeStateManager, numberOfLevels); 
    } 

    @Override 
    public void handleItemClick(final View view, final Object id) { 
     ((MyActivity) this.activity).someFunction(); 
    } 
} 

sau đó chỉ cần khai báo someFunction trong MyActivity để làm những gì bạn w ant

protected void someFunction(){ 
// Do something here 
    } 
Các vấn đề liên quan