我的android应用程序中有一个列表视图,里面有一个ImageButton。我需要为我的ImageButton onClick编写一个动作。我能够使用监听器为我的ImageButton编写onClick,但是我无法获得用户点击的当前ListItem的位置。
我尝试了不同的解决方案来获得这个位置,但没有一个能够奏效。
这是我的自定义适配器文件
package com.fyshadows.collegemate;
import java.util.ArrayList;
import java.util.List;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class ForumAdapter extends ArrayAdapter<DiscussionList> {
private static List<DiscussionList> items = null;
public ForumAdapter(Context context, List<DiscussionList> items) {
super(context, R.layout.custom_list, items);
this.items = items;
}
@Override
public int getCount() {
return items.size();
}
public static DiscussionList getModelPosition(int position) {
return items.get(position);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater li = LayoutInflater.from(getContext());
v = li.inflate(R.layout.custom_list, null);
}
DiscussionList app = items.get(position);
if (app != null) {
TextView titleText = (TextView) v.findViewById(R.id.dscTitle);
TextView categoryText = (TextView) v.findViewById(R.id.dscCategory);
TextView descriptionText = (TextView) v
.findViewById(R.id.dscDescription);
TextView timeText = (TextView) v.findViewById(R.id.dscTime);
TextView idText = (TextView) v.findViewById(R.id.dscDiscId);
titleText.setText(app.getTitle());
categoryText.setText(app.getCategory());
descriptionText.setText(app.getDescription());
timeText.setText(app.getTime());
idText.setText(app.getDiscId());
String dId = app.getDiscId();
//onClick for image button inside list view
ImageButton imgButton = (ImageButton) v
.findViewById(R.id.imageButton1);
imgButton.setFocusable(false);
imgButton.setTag(position);
imgButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
final int position = getView(position, view, parent).get(position);
Toast.makeText(getContext(), position,
Toast.LENGTH_SHORT).show();
}
});
}
return v;
}
}
我可以用一些虚拟字符串显示祝酒词,但由于某种原因无法获得该位置。
提前致谢
现在错误日志显示
06-20 01:46:43.498: E/AndroidRuntime(12672): at android.content.res.Resources.getText(Resources.java:246)
06-20 01:46:43.498: E/AndroidRuntime(12672): at android.widget.Toast.makeText(Toast.java:265)
06-20 01:46:43.498: E/AndroidRuntime(12672): at com.fyshadows.collegemate.ForumAdapter$1.onClick(ForumAdapter.java:72)
06-20 01:46:43.498: E/AndroidRuntime(12672): at android.view.View.performClick(View.java:4421)
06-20 01:46:43.498: E/AndroidRuntime(12672): at android.view.View$PerformClick.run(View.java:17903)
06-20 01:46:43.498: E/AndroidRuntime(12672): at android.os.Handler.handleCallback(Handler.java:730)
06-20 01:46:43.498: E/AndroidRuntime(12672): at android.os.Handler.dispatchMessage(Handler.java:92)
06-20 01:46:43.498: E/AndroidRuntime(12672): at android.os.Looper.loop(Looper.java:213)
06-20 01:46:43.498: E/AndroidRuntime(12672): at android.app.ActivityThread.main(ActivityThread.java:5225)
06-20 01:46:43.498: E/AndroidRuntime(12672): at java.lang.reflect.Method.invokeNative(Native Method)
06-20 01:46:43.498: E/AndroidRuntime(12672): at java.lang.reflect.Method.invoke(Method.java:525)
06-20 01:46:43.498: E/AndroidRuntime(12672): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:741)
06-20 01:46:43.498: E/AndroidRuntime(12672): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
06-20 01:46:43.498: E/AndroidRuntime(12672): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:1)
您正在使用setTag()
但从未对其进行任何操作。变化
final int position = getView(position, view, parent).get(position);
到
final int position = (Integer)view.getTag();
现在它可能会提供通过position
迭代的最后一项的getView()
。
修改强>
您的Toast
需要String
,因此您需要将其更改为
Toast.makeText(getContext(), "" + position, Toast.LENGTH_SHORT).show();
或之前将其转换为String
。