在我的Android应用程序中,我有自定义列表视图,带有图像和textview。在getView方法下的扩展BaseAdapter中,使用setOnClickListener将textview和image的单击事件与onClick方法相关联,如下面的代码所示
public View getView(int position, View convertView, ViewGroup viewGroup) {
String entry = listWords.get(position);
if(convertView == null) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.history_row, null);
}
TextView txtWordView = (TextView) convertView.findViewById(R.id.txtWordView);
txtWordView.setText(entry);
txtWordView.setOnClickListener(this);
txtWordView.setTag(entry);
ImageView imgRemove = (ImageView) convertView.findViewById(R.id.del_x);
imgRemove.setOnClickListener(this);
imgRemove.setTag(entry);
return convertView;
}
和
public void onClick(View view) {
try {
if(view instanceof TextView){
//Here i would like this to finish this activity with result being sent to main
//activity - something like this
//Intent result = getIntent();
//result.putExtra("word", _strList.get(location));
//setResult(RESULT_OK, result);
//finish();
} else {
String entry = (String) view.getTag();
listWords.remove(entry);
History objHistory = new History(this.context);
objHistory.clearHistory(entry);
}
notifyDataSetChanged();
} catch (Exception e) {
}
}
当点击图像时,它会从List中删除该条目,但是在点击textview时我想将点击的textview文本返回到主要活动,但是我无法实现这一点,尽管我可以从setOnItemClickListener执行此操作这个自定义列表视图活动。
答案 0 :(得分:2)
您可以将活动对象传递给baseActivity的构造函数,然后就可以随意执行任何操作。