私ListView
の にはTextView
、 とImageview
が一列に並んでいます。Listview
行データはローカル データベースから取得されます。ユーザーが選択のために任意の行に触れると、その行項目の画像を変更したいと考えています。ユーザーがこのアプリを閉じて再度開くと、imageview
前回選択した行の新しい画像を設定したいと思います。
また、ユーザーが のいずれかの行を選択すると、2 番目のアクティビティが呼び出されますlistview
。ユーザーがその新しいアクティビティから戻るボタンを押すと、選択された行imageview
は変更されず、元の画像のみが表示されます。以下のコードを使用しましたが、完全には機能していません。
アクティビティの onstart メソッドは次のとおりです。
@Override
public void onStart() {
super.onStart();
int pos = prefs.getInt(PrefernceData.PREF_CURR_SELECTED_ID, -1);
for(int i = 0; i < text.length; i++){
if(Integer.parseInt(text[i][0]) == pos){
dotPosition = i;
}
}
リストビュー itemclick メソッド:
public void onItemClick(AdapterView<?> arg0, View view, int position, long arg3) {
dotPosition = position;
clearSelection();
lastSelectedView = (ImageView)view.findViewById(R.id.doticon);
ImageView v = (ImageView)view.findViewById(R.id.doticon);
v.setImageResource(R.drawable.dothighlight);
prefs.edit().putInt(PrefernceData.PREF_CURR_SELECTED_ID, Integer.parseInt(text[position][0]).commit();
Intent i = new Intent(this,SecondActivity.class);
startActivity(i);
clearselection メソッドは次のとおりです。
public void clearSelection() {
if(lastSelectedView != null) {
Log.i("myLog","clear selection:lastselected icon::");
lastSelectedView.setImageResource(R.drawable.dot);
}
}
アダプターの getview メソッドは次のとおりです。
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
LayoutInflater mInflater = LayoutInflater.from(myActivity.this);
if(convertView == null) {
convertView = mInflater.inflate(R.layout.site_listview_items, null);
holder = new ViewHolder();
holder.siteText = (TextView)convertView.findViewById(R.id.homesitelisttext);
holder.dotIcon = (ImageView)convertView.findViewById(R.id.doticon);
convertView.setTag(holder);
} else {
holder = (ViewHolder)convertView.getTag();
}
holder.siteText.setText(text[position][1]);
Log.i("myLog","position::"+position+"dotPosition:"+dotPosition);
if(position == dotPosition) {
holder.dotIcon.setImageResource(R.drawable.dothighlight);
lastSelectedView = holder.dotIcon;
} else {
holder.dotIcon.setImageResource(R.drawable.dot);
}
return convertView;
}