1
在我的android應用程序中,我使用圖像和textview自定義listview。在以下的使用setOnClickListener的onClick方法相關聯的TextView和圖像的getView方法點擊事件擴展BaseAdapter如圖代碼下面從自定義BaseAdapter完成活動
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) {
}
}
當點擊圖像它實際上可以刪除從列表條目這工作正常,但點擊textview我想返回單擊文本到主要活動的文本,但我無法實現這一點,雖然我可以從setOnItemClickListener這個自定義列表視圖活動做到這一點。
不錯!它的工作原理,我不知道它會那麼簡單 – Waqas 2010-11-22 06:38:42