Android 更改listview行的颜色

Android 更改listview行的颜色,android,listview,Android,Listview,让我简单解释一下 我有两个片段: 1) 用户在其中输入一些文本的片段。在这里,我还定义了5个不同颜色的按钮。从这里,输入的文本被添加到数据库中 2) 片段B有一个listview,当用户单击片段a中的“保存”按钮时,它使用customadapter从该数据库填充数据 一切正常。正在保存数据,并将其加载到Listview和所有。现在还记得那5个有5种不同颜色的按钮吗 我想要的是假设在片段A中添加数据时,用户选择了颜色为“橙色”的按钮,那么适配器的getView()方法中要膨胀的行的背景也应该是橙色

让我简单解释一下

我有两个片段:

1) 用户在其中输入一些文本的片段。在这里,我还定义了5个不同颜色的按钮。从这里,输入的文本被添加到数据库中

2) 片段B有一个listview,当用户单击片段a中的“保存”按钮时,它使用customadapter从该数据库填充数据

一切正常。正在保存数据,并将其加载到Listview和所有。现在还记得那5个有5种不同颜色的按钮吗

我想要的是假设在片段A中添加数据时,用户选择了颜色为“橙色”的按钮,那么适配器的getView()方法中要膨胀的行的背景也应该是橙色。 (像谷歌保留的东西)

可能吗

我的适配器类:

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();
}
公共类notesAdapter扩展BaseAdapter{
ArrayList注释;
语境;
视图转换视图;
私有静态最终字符串TAG=“samduceapter”;
私人最终布局平面图mLayoutInflater;
私有最终随机域;
专用SparseBooleanArray MSelectEditEMSID;
专用静态最终SparseArray sPositionHeightRatios=新SparseArray();
公共notesdapter(上下文、数组列表注释){
this.notes=注释;
this.context=上下文;
this.mLayoutInflater=LayoutInflater.from(上下文);
this.mRandom=新随机数();
}
@凌驾
public int getCount(){
returnnotes.size();
}
@凌驾
公共对象getItem(int位置){
返回注释。获取(位置);
}
@凌驾
公共长getItemId(int位置){
返回0;
}
@凌驾
公共视图getView(最终整型位置,视图转换视图,
最终视图组(父视图){
this.convertView=convertView;
视窗座;
if(convertView==null){
convertView=MLAYOUTINGER.充气(R.LAYOUTING.notes\单行,父项,false);
vh=新的视窗支架();
convertView.setTag(vh);
}否则{
vh=(ViewHolder)convertView.getTag();
}
vh.txtView=detail(convertView,R.id.notes\u grid\u text,notes.get(position.getNotes());
vh.notes_title=详细信息(添加了convertView、R.id.note_title、notes.get(position.getNotesTitle());
int len=vh.txtView.length();
如果(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);
}
双位置高度=getPositionRatio(位置);
vh.txtView.setHeightRatio(位置高度);
vh.注释\标题设置高度比(位置高度);
vh.notes_title.setPaintFlags(vh.notes_title.getPaintFlags()| Paint.UNDERLINE_TEXT_FLAG);
/*如果((位置==0 | |位置==5 | |位置==10 | |位置==15)){
setBackgroundColor(Color.rgb(255、112、67));
}else if(位置==1 | |位置==6 | |位置==11 | |位置==16){
convertView.setBackgroundColor(Color.rgb(29233182));
}else if(位置==2 | |位置==7 | |位置==12 | |位置==17){
setBackgroundColor(Color.rgb(121134203));
}else if(位置==3 | |位置==8 | |位置==13 | |位置==18){
convertView.setBackgroundColor(Color.rgb(205,220,57));
}否则如果(位置==4 | |位置==9 | |位置==14 | |位置==19){
setBackgroundColor(Color.rgb(22464251));
}*/
返回视图;
}
public void changeColortoRange(){
setBackgroundColor(Color.rgb(255、112、67));
}
静态类视窗夹{
DynamicLightTextView txtView,注释和标题;
}
专用双getPositionRatio(最终内部位置){
双比值=sPositionHeightRatios.get(位置,0.0);
如果(比率==0){
比率=getRandomHeightRatio();
sPositionHeightRatios.append(位置、比率);
Log.d(标签“getPositionRatio:”+position+“ratio:”+ratio);
}
回报率;
}
私有双getRandomHeightRatio(){
return(mRandom.nextDouble()/2.4)+0.8;
}
私有DynamicLightTextView详细信息(视图v、整型剩余、字符串文本){
DynamicHeightTextView电视=(DynamicHeightTextView)v.findViewById(resId);
tv.setText(文本);
返回电视;
}
公共无效切换选择(内部位置){
选择查看(位置,!mSelectedItemsIds.get(位置));
}
public void selectView(int位置,布尔值){
如果(值)
mSelectedItemsIds.put(位置、值);
其他的
mSelectedItemsIds.delete(位置);
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)));
}
public类add\u note\u frag扩展片段实现View.OnClickListener{
编辑文本注释,注释标题;
字符串user\u note,user\u note\u title;
按钮svenote;
IMAGEVIEWORA、vio、yel、pin;
相对性;
ActionBar ab;
注:适配器;
private ArrayList notes=new ArrayList();
@可空
@凌驾
CreateView上的公共视图(布局、充气机、视图组容器、捆绑包保存状态){
视图=充气机。充气(R.layout.add\u notes\u片段,容器,false);
返回视图;
}
@凌驾
恢复时公开作废(){
super.onResume();
ab=getActivity().getActionBar();
ab.setDisplayHomeAsUpEnabled(真);
rel=(RelativeLayout)getActivity().findViewById(R.id.notes\u rel\u lay);
note=(EditText)getActivity().findViewById(R.id.note);
noteTitle=(EditText)getActivity().findViewById(R.id.note\u title);
svenote=(按钮)getActivity().findViewById(R.id.savenote);
适配器=新的notesAdapter(getActivity(),notes);
ora=(ImageView)getActivity().findViewById(R.id.orange);
vio=(ImageView)getActivity().findViewById(R.id.violet);
yel=(ImageView)getActivity().findViewById(R.id.yellow);
引脚=(图像视图)getAct