讓我簡單解釋一下。更改列表視圖行的顏色
我有2個片段:
1)片段A,其中用戶輸入的一些文本。在這裏我還定義了5個不同顏色的按鈕。從這裏,輸入的文本被添加到數據庫。
2)片段B,其具有從該數據庫使用customadapter當用戶在片段A.點擊「保存」按鈕
一切正常填充數據列表視圖。數據正在保存,加載到Listview和全部。現在記得5種不同顏色的按鈕嗎?
我理想是,雖然在片段A添加數據假設,用戶選擇與顏色按鈕「橙色」假設,那麼將在getView被充氣的行()適配器的方法也應該有它的背景橙色。 (Something ike Google Keep)
這可能嗎?
我的適配器類:
public class notesAdapter extends BaseAdapter {
ArrayList<notesSingleRow> notes;
Context context;
View convertView;
private static final String TAG = "SampleAdapter";
private final LayoutInflater mLayoutInflater;
private final Random mRandom;
private SparseBooleanArray mSelectedItemsIds;
private static final SparseArray<Double> sPositionHeightRatios = new SparseArray<Double>();
public notesAdapter(Context context, ArrayList<notesSingleRow> notes) {
this.notes = notes;
this.context = context;
this.mLayoutInflater = LayoutInflater.from(context);
this.mRandom = new Random();
}
@Override
public int getCount() {
return notes.size();
}
@Override
public Object getItem(int position) {
return notes.get(position);
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(final int position, View convertView,
final ViewGroup parent) {
this.convertView = convertView;
ViewHolder vh;
if (convertView == null) {
convertView = mLayoutInflater.inflate(R.layout.notes_single_row, parent, false);
vh = new ViewHolder();
convertView.setTag(vh);
} else {
vh = (ViewHolder) convertView.getTag();
}
vh.txtView = detail(convertView, R.id.notes_grid_text, notes.get(position).getNotes());
vh.notes_title = detail(convertView, R.id.note_title_added, notes.get(position).getNotesTitle());
int len = vh.txtView.length();
if (len == 1 || len ==2){
vh.txtView.setTextSize(100);
}
else if (len == 3){
vh.txtView.setTextSize(80);
}
else if (len == 4){
vh.txtView.setTextSize(60);
}
else if (len ==5){
vh.txtView.setTextSize(50);
}
else if (len == 8){
vh.txtView.setTextSize(60);
}
double positionHeight = getPositionRatio(position);
vh.txtView.setHeightRatio(positionHeight);
vh.notes_title.setHeightRatio(positionHeight);
vh.notes_title.setPaintFlags(vh.notes_title.getPaintFlags()| Paint.UNDERLINE_TEXT_FLAG);
/* if ((position == 0 || position == 5 || position == 10 || position ==15)) {
convertView.setBackgroundColor(Color.rgb(255, 112, 67));
} else if (position == 1 || position == 6 || position == 11 || position ==16) {
convertView.setBackgroundColor(Color.rgb(29, 233, 182));
} else if (position == 2 || position == 7 || position == 12 || position ==17) {
convertView.setBackgroundColor(Color.rgb(121, 134, 203));
} else if (position == 3 || position == 8 || position == 13 || position ==18) {
convertView.setBackgroundColor(Color.rgb(205, 220, 57));
} else if (position == 4 || position == 9 || position == 14 || position ==19) {
convertView.setBackgroundColor(Color.rgb(224, 64, 251));
}*/
return convertView;
}
public void changeColorToOrange() {
convertView.setBackgroundColor(Color.rgb(255, 112, 67));
}
static class ViewHolder {
DynamicHeightTextView txtView, notes_title;
}
private double getPositionRatio(final int position) {
double ratio = sPositionHeightRatios.get(position, 0.0);
if (ratio == 0) {
ratio = getRandomHeightRatio();
sPositionHeightRatios.append(position, ratio);
Log.d(TAG, "getPositionRatio:" + position + " ratio:" + ratio);
}
return ratio;
}
private double getRandomHeightRatio() {
return (mRandom.nextDouble()/2.4) + 0.8;
}
private DynamicHeightTextView detail(View v, int resId, String text) {
DynamicHeightTextView tv = (DynamicHeightTextView) v.findViewById(resId);
tv.setText(text);
return tv;
}
public void toggleSelection(int position) {
selectView(position, !mSelectedItemsIds.get(position));
}
public void selectView(int position, boolean value) {
if (value)
mSelectedItemsIds.put(position, value);
else
mSelectedItemsIds.delete(position);
notifyDataSetChanged();
}
用戶在該片段添加文本用不同顏色的按鈕:
public class add_note_frag extends Fragment implements View.OnClickListener {
EditText note, noteTitle;
String user_note, user_note_title;
Button svenote;
ImageView ora, vio, yel, pin;
RelativeLayout rel;
ActionBar ab;
notesAdapter adapter;
private ArrayList<notesSingleRow> notes = new ArrayList<notesSingleRow>();
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.add_notes_fragment, container, false);
return view;
}
@Override
public void onResume() {
super.onResume();
ab = getActivity().getActionBar();
ab.setDisplayHomeAsUpEnabled(true);
rel = (RelativeLayout) getActivity().findViewById(R.id.notes_rel_lay);
note = (EditText) getActivity().findViewById(R.id.note);
noteTitle = (EditText) getActivity().findViewById(R.id.note_title);
svenote = (Button) getActivity().findViewById(R.id.savenote);
adapter = new notesAdapter(getActivity(), notes);
ora = (ImageView) getActivity().findViewById(R.id.orange);
vio = (ImageView) getActivity().findViewById(R.id.violet);
yel = (ImageView) getActivity().findViewById(R.id.yellow);
pin = (ImageView) getActivity().findViewById(R.id.pink);
ora.setOnClickListener(this);
vio.setOnClickListener(this);
yel.setOnClickListener(this);
pin.setOnClickListener(this);
svenote.setOnClickListener(this);
}
public void saveNote() {
tasks_Database_Operations tasksDatabaseOperations = new tasks_Database_Operations(getActivity());
user_note = note.getText().toString();
user_note_title = noteTitle.getText().toString();
long id1 = tasksDatabaseOperations.insertNote(user_note, user_note_title);
if (id1 < 0) {
Log.e("HirakDebug", "add_task_frag failed insertData operation");
} else {
Log.d("HirakDebug", "Data sent to be inserted");
}
}
@Override
public void onClick(View v) {
if (v == svenote) {
saveNote();
goBackToNoteFrag();
ab.setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.actionbar)));
}
if (v == ora) {
rel.setBackgroundColor(getResources().getColor(R.color.orange));
ab.setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.orange)));
adapter.changeColorToOrange();
}
if (v == vio) {
rel.setBackgroundColor(getResources().getColor(R.color.violet));
ab.setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.violet)));
}
if (v == yel) {
rel.setBackgroundColor(getResources().getColor(R.color.yellow));
ab.setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.yellow)));
}
if (v == pin) {
rel.setBackgroundColor(getResources().getColor(R.color.pinkk));
ab.setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.pinkk)));
}
}
public void goBackToNoteFrag() {
notesListFrag nLF = new notesListFrag();
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.setCustomAnimations(R.anim.slide_up, R.anim.slide_down);
ft.remove(this);
ft.replace(R.id.dynamic_content, nLF, "nLF");
ft.commit();
}
@Override
public void onDetach() {
super.onDetach();
ab.setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.actionbar)));
}
是的,請張貼您的適配器。我們必須「玩」按鈕的onClick和getView – 2014-11-01 12:16:10
我更新了問題。 nignore適配器類中的註釋行。我用它們根據該行的位置改變背景顏色。但現在我想根據用戶選擇 – 2014-11-01 12:21:10