3

ListViewのカスタムアレイアダプタを使用してを作成しましたMyObject。の1つのインスタンスには、とがMyObject含まれます。視覚化すると、すべてのアイテムが太字で表示され、それらのアイテムの1つをタップすると、新しいアイテムが開始されます。私がやりたいのは、押して戻ってくるたびに、クリックしたアイテムを太字ではなく視覚化することです。これは私の習慣です:StringbooleanListViewActivityListViewArrayAdapter

public class MyAdapter extends ArrayAdapter<MyObject> {
private final Context context;
private final ArrayList<MyObject> values;

public MyAdapter(Context context, ArrayList<MyObject> values) {
    super(context, R.layout.my_item, values);
    this.context = context;
    this.values = values;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    if(convertView == null){
        convertView = inflater.inflate(R.layout.my_item, parent, false);
    }
    TextView textView = (TextView) convertView.findViewById(R.id.itemName);
    textView.setText(values.get(position).getMyObjectName());
    if(values.get(position).isSetted()){
        textView.setTypeface(null, 0);
    }
    return convertView;
}

}

私のActivity中に:

    @Override
protected void onResume() {
    super.onResume();
    adapter.notifyDataSetChanged();
}

コードは実際に機能しますが、問題があります。いくつかのアイテムをタップした後、戻ってきたときに他のタップされていないアイテムが太字になりません。これはランダムな方法で発生します!なぜこうなった?

    @Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);

    String item = objList.get(position).getMyObjectName();
    objList.get(position).setSetted(true);


    to_ans.putExtra("ItemName", item);
    startActivity(to_ans);

}

アイテムのboolean値は、最初はすべて。に設定されていfalseます。

4

1 に答える 1

1

次の場合は、デフォルトの太字スタイルに戻す必要があります (デフォルトのスタイルに戻すには、太字なしで設定されている可能性のある ( is でない場合)リサイクルされた可能性があります):isSetted()falseViewconvertViewnull

if(values.get(position).isSetted()){
    textView.setTypeface(null);
} else {
    textView.setTypeface(Typeface.BOLD);
}
于 2012-05-10T16:58:32.753 に答える