use*_*683 13 android onclick android-listview custom-adapter
我有一个带有ListView的Android应用程序,ListView将设置正常,但现在我希望ListView中的图像可以点击.我通过使用2个类,Activity类(父)和ArrayAdapter来填充列表.在ArrayAdapter中,我为列表中我想要点击的图像实现了一个OnClickListener.
到目前为止一切都有效.
但是现在我想在活动类中运行一个函数,当onClick(对于列表中的图像)运行但我不知道如何.以下是我使用的2个类.
首先是Activity类:
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
}
}
Run Code Online (Sandbox Code Playgroud)
我想从Activity类中调用函数的ArrayAdapter:
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;
}
}
Run Code Online (Sandbox Code Playgroud)
如果有人知道如何在arrayadapter中运行活动类中的函数,或者如何在Activity类中设置图像onClickListener,我会非常熟悉帮助.
Adi*_*mro 44
里面onClick()
做这样的事情:
((ParentClass) context).functionToRun();
Run Code Online (Sandbox Code Playgroud)
只是为了清楚地扩展提供的答案在BaseAdapter中,您可以通过调用获取父类this.getActivity();
如果您然后将其类型化为实际的活动类,则可以根据下面的@AdilSoomro答案调用函数,这样您实际上最终会得到类似的东西
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();
}
}
Run Code Online (Sandbox Code Playgroud)
然后在MyActivity中声明someFunction以执行您想要的操作
protected void someFunction(){
// Do something here
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
22819 次 |
最近记录: |