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.
Tuyệt vời. Đơn giản và hiệu quả. –