如何在onCreate期间更改ListView项目背景LinearLayout背景? [英] How to change ListView Item Background LinearLayout Background during onCreate?

查看:64
本文介绍了如何在onCreate期间更改ListView项目背景LinearLayout背景?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前有一个ListView,由SimpleAdapter填充,人口正常。我遇到的问题是我似乎无法弄清楚如何在onCreate期间更改ListItem LinearLayout的背景颜色。如果我单击一个按钮或精心做一个动作,但是如果我在onCreate中执行它,或者如果我在基于我的SQLITE数据库中的所有表填充ListView后执行此操作,它工作得很好。 />
此代码正在改变颜色。

I currently have a ListView Which is being populated by a SimpleAdapter and the population is working great. Problem I am having is I cant seem to figure out how to change the background color of the ListItem LinearLayout during the onCreate. It is working perfectly fine if I click a button or do an action psychically but does not want to work if i do it in the onCreate or if I do it after populating the ListView based off of all the tables in my SQLITE database.
This code is what is changing the color.

if(c.moveToFirst()){
               do{
                   if(checkCat.moveToFirst()){
                       do{
                           if(c.getString(0).equals(checkCat.getString(0))){
                               for (int i = 0; i < adapter.getCount(); i++) {
                                   final FrameLayout listItemLayout = (FrameLayout) cardList.getChildAt(i);
                                   final LinearLayout lL = (LinearLayout) listItemLayout.findViewById(R.id.cardmainlayout);
                                   final TextView tv = (TextView) listItemLayout.findViewById(R.id.text1);                                    if(tv.getText().toString().equals(checkCat.getString(1).trim())) {
                                       switch (c.getString(1).trim()) {
                                           case "White":                                                lL.setBackgroundResource(R.drawable.bg_color_white);
                                               break;
                                           case "Orange":                                                lL.setBackgroundResource(R.drawable.bg_color_orange);
                                               break;
                                           case "Red":                                                lL.setBackgroundResource(R.drawable.bg_color_red);
                                               break;
                                           case "Green":                                                lL.setBackgroundResource(R.drawable.bg_color_green);
                                               break;
                                           case "Purple":                                                lL.setBackgroundResource(R.drawable.bg_color_purple);
                                               break;
                                           case "Blue":                                                lL.setBackgroundResource(R.drawable.bg_color_blue);
                                               break;
                                       }
                                       switch (c.getString(2).trim()) {
                                           case "White":                                                tv.setTextColor(getResources().getColor(android.R.color.white));
                                               break;
                                           case "Black":                                                tv.setTextColor(getResources().getColor(android.R.color.black));
                                               break;
                                           case "Green":                                                tv.setTextColor(getResources().getColor(android.R.color.holo_green_dark));
                                               break;
                                           case "Orange":                                               tv.setTextColor(getResources().getColor(android.R.color.holo_orange_dark));
                                               break;
                                           case "Red":                                                tv.setTextColor(getResources().getColor(android.R.color.holo_red_dark));
                                               break;
                                           case "Blue":                                                tv.setTextColor(getResources().getColor(android.R.color.holo_blue_dark));
                                               break;
                                       }
                                   }
                               }
                           }
                       }while (checkCat.moveToNext());
                   }
               }while(c.moveToNext());
           }



此代码是填充ListView的原因


This code is what is what is populating the ListView

    if (c.moveToFirst()) {
    do {
        if (!c.getString(0).contains(&quot;a&quot;)) {
            if (!c.getString(0).contains(&quot;name&quot;)) {
                if (!c.getString(0).contains(&quot;name2&quot;)) {
                    String str = c.getString(0);
                    map = new HashMap&lt;String, Object&gt;();
                    map.put(&quot;title&quot;, str.toString();
                    fillMaps.add(map);
                }
            }
        }
    } while (c.moveToNext());
}







还有这个代码重新更新ListView基于代码在这里工作的类别。问题是第一部分与上面相同所以它崩溃,第二部分在adpater刷新后没有完全更新。必须再次单击它类别按钮才能检测到适配器已更改。(是的我正在调用adapter.notifyDataSetChanged()。)




Also have this code which reupdates the ListView Based on categories the code is sort of working here. Problem is the first part is the same as above so it crashes and the second part does not exactly update after the adpater is refreshed. you have to click it the category button again for it to detect the adapter changed.(yes I am calling the adapter.notifyDataSetChanged().)

if(strName.toLowerCase().equals("all")) {
       fillMaps.clear();
       Cursor c = appDB.rawQuery("SELECT name FROM sqlite_master WHERE type='table';", null);
       if (c.moveToFirst()) {
           do {
               if (!c.getString(0).contains("android_metadata")) {
                   if (!c.getString(0).contains("Name1")) {
                       if (!c.getString(0).contains("Name")) {
                           map = new HashMap<String, Object>();
                           map.put("title", c.getString(0));
                           fillMaps.add(map);
                           chkBox = 0;
                       }
                   }
               }

           } while (c.moveToNext());
       }
   }
   else {
       Cursor checkCat = appDB.rawQuery("SELECT * FROM Name", null);
       if (checkCat.moveToFirst()) {
           fillMaps.clear();
           do {
               if (checkCat.getString(0).contains(strName)) {
                   map = new HashMap<String, Object>();
                   map.put("title", checkCat.getString(1));
                   fillMaps.add(map);
                   chkBox = 0;
               }
           } while (checkCat.moveToNext());
       }

   }
   adapter.notifyDataSetChanged();



,最后但并非最不重要的是这是我的SimpleAdapter


and last but not least this is my SimpleAdapter

adapter = new SimpleAdapter(this, fillMaps, R.layout.main_c_layout, from, to) {
          @Override
          public View getView(final int position, View convertView, ViewGroup parent) {
              View rRow = super.getView(position, convertView, parent);
              final Button vsButton = (Button) rRow.findViewById(R.id.quizListViewButton);
              final CheckBox cb = (CheckBox) rRow.findViewById(R.id.deleteCheckBox);
              TextView v = (TextView) rRow.findViewById(R.id.text1);
              final String str = v.getText().toString().replaceFirst("\\s+$", "");
              vsButton.setOnClickListener(new View.OnClickListener() {
                  @Override
                  public void onClick(View v) {
                      Toast.makeText(MainActivity.this, str + "", Toast.LENGTH_SHORT).show();
                  }
              });
              cb.setOnClickListener(new View.OnClickListener() {
                  @Override
                  public void onClick(View v) {
                      if (cb.isChecked()) {
                          chkBox++;
                      } else {
                          chkBox--;
                      }
                      if (chkBox > 1) {
                          editNoteSet.setVisibility(View.GONE);
                      } else {
                          editNoteSet.setVisibility(View.VISIBLE);
                      }
                  }
              });
              return rRow;
          }
      };
      List.setAdapter(adapter);



所以只是为了清除我的问题你将如何更改Custom ListView行中的LinearLayout的颜色。(对于记录,一切都是正确填充我只是无法改变颜色。)



此外,我一直得到的错误是尝试调用虚拟方法'android.view.View android.widget.FrameLayout .findViewById(int)'在空对象引用上

这没有意义,因为它在我单击按钮或物理操作时有效。感谢任何和所有的帮助。提前谢谢。

推荐答案

,);
vsButton.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
Toast.makeText(MainActivity.this, str +,Toast.LENGTH_SHORT)。show();
}
});
cb.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(查看对象){
if(cb.isChecked()){
chkBox ++;
} else {
chkBox--;
}
if(chkBox> 1){
editNoteSet.setVisibility(View.GONE);
} else {
editNoteSet.setVi sibility(View.VISIBLE);
}
}
});
返回rRow;
}
};
List.setAdapter(adapter);
", ""); vsButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(MainActivity.this, str + "", Toast.LENGTH_SHORT).show(); } }); cb.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (cb.isChecked()) { chkBox++; } else { chkBox--; } if (chkBox > 1) { editNoteSet.setVisibility(View.GONE); } else { editNoteSet.setVisibility(View.VISIBLE); } } }); return rRow; } }; List.setAdapter(adapter);



所以只是为了清除我的问题你将如何更改Custom ListView行中的LinearLayout的颜色。(对于记录,一切都是正确填充我只是无法改变颜色。)



此外,我一直得到的错误是尝试调用虚拟方法'android.view.View android.widget.FrameLayout .findViewById(int)'在空对象引用上

这没有意义,因为它在我单击按钮或物理操作时有效。感谢任何和所有帮助。提前感谢您。


< b> 解决方案



解决方案是从SimpleAdapter调用更改。我没有意识到每次填充ListView时都会调用SimpleAdapter。因此我将代码更改为此并且它完美无缺。



Solution

The solution was to call the change from the SimpleAdapter. I did not realize that the SimpleAdapter was called everytime the ListView was populated.Thus I changed the code to this and it works flawlessly.

adapter = new SimpleAdapter(this, fillMaps, R.layout.main_c_layout, from, to) {
    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        View rRow = super.getView(position, convertView, parent);

        final Button vsButton = (Button) rRow.findViewById(R.id.quizListViewButton);
        final CheckBox cb = (CheckBox) rRow.findViewById(R.id.deleteCheckBox);
        LinearLayout lL = (LinearLayout)rRow.findViewById(R.id.myLayout);
        TextView v = (TextView) rRow.findViewById(R.id.text1);
        final String str = v.getText().toString().replaceFirst("\\s+


,);

If(str.equals(c.getString(0)){
lL.setBackgroundColor(Color.Blue);
} else {
lL.setBackgroundColor( Color.Green);
}


vsButton.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {


Toast.makeText(MainActivity.this,str +,Toast.LENGTH_SHORT)。show();
}
});


cb.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
if(cb.isChecked()) {
chkBox ++;
} else {
chkBox--;
}
if(chkBox> 1){
editNoteSet.setVisibility(View.GONE) );
} else {
editNoteSet.setVisibility(View.VISIBLE);
}
}
});

返回rRow;
}
};
List.setAdapter(适配器);
", ""); If(str.equals(c.getString(0)){ lL.setBackgroundColor(Color.Blue); }else{ lL.setBackgroundColor(Color.Green); } vsButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(MainActivity.this, str + "", Toast.LENGTH_SHORT).show(); } }); cb.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (cb.isChecked()) { chkBox++; } else { chkBox--; } if (chkBox > 1) { editNoteSet.setVisibility(View.GONE); } else { editNoteSet.setVisibility(View.VISIBLE); } } }); return rRow; } }; List.setAdapter(adapter);


这篇关于如何在onCreate期间更改ListView项目背景LinearLayout背景?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆