我想区分视图的背景颜色,但是我不知道该怎么做。谁能帮助我完成任务。
下面是我的DatabaseHelper.java类
public List<HouseModal> get_Working_Area_Structures_House(int limit, int offset,String searched_Str) {
if(database == null){
database = getWritableDatabase();
}
List<HouseModal> list_krd= new ArrayList<>();
String selectQuery = "SELECT DISTINCT\n" +
"\tWAS.Para_Structure_ID,\n" +
"\tWAS.Para_Code || ' ' || CAST (WAS.Structure_No AS int) AS Structure_No,\n" +
"\tST.Family_Count AS Families,\n" +
"\tFM.Family_Members_Count AS Family_Members, \n" +
"\t FM.Family_Migration AS Family_Migration\n" +
"FROM\n" +
"\t (\n" +
"\t\t SELECT\n" +
"\t\t\tP.Para_Code,\n" +
"\t\t\tPS.Para_Structure_ID,\n" +
"\t\t\tPS.Structure_No \n" +
"\t\tFROM\n" +
"\t\t\tPara_Structure AS PS\n" +
"\t\tINNER JOIN Working_Area_Structures AS WAS ON PS.Para_Structure_ID = WAS.Para_Structure_ID\n" +
"\t\tINNER JOIN Field_Station_Paras AS P ON PS.Para_ID = P.Para_ID \n" +
"\t) AS WAS,\n" +
"\t (\n" +
"\tSELECT\n" +
"\t\t\tKH.Para_Structure_ID,\n" +
"\t\t\tCOUNT(DISTINCT KH.House_Hold_ID) AS Family_Count \n" +
"\t\tFROM\n" +
"\t\t\tKhandan_Register_Header AS KH\n" +
"\t\tLEFT JOIN Family_Visit_History AS FVH ON KH.House_Hold_ID = FVH.House_Hold_ID \n" +
"\t\tWHERE\n" +
"\t\t(FVH.Visit_Status = 0 OR FVH.Visit_Status IS NULL)\n" +
"\t\t--AND ((date(FVH.Visit_Date) >= (SELECT date(Starting_Date) FROM Working_Cycle WHERE DATE('NOW') BETWEEN date(Starting_Date)AND date(Ending_Date)) AND date(FVH.Visit_Date) <= (SELECT date(Ending_Date) FROM Working_Cycle WHERE DATE('NOW') BETWEEN date(Starting_Date)AND date(Ending_Date))) OR date(FVH.Visit_Date) IS NULL) \n" +
"\t\t--AND (KH.House_Hold_ID NOT IN (SELECT DISTINCT House_Hold_ID FROM Family_Visit_History WHERE Visit_Status = 1)) \n" +
"\t\tGROUP BY KH.Para_Structure_ID\n" +
"\t) AS ST , (\n" +
"\t\t SELECT\n" +
"\t\t\tKH.Para_Structure_ID,\n" +
"\t\t\tCOUNT(\n" +
"\t\t\t\tDISTINCT KD.Family_Member_ID\n" +
"\t\t\t) AS Family_Members_Count, \n" +
"\t\t\tfmr.Family_Migration_ID AS Family_Migration \n" +
"\t\tFROM\n" +
"\t\t\tKhandan_Register_Detail AS KD\n" +
"\t\tINNER JOIN Khandan_Register_Header AS KH ON KD.House_Hold_ID = KH.House_Hold_ID\n" +
"\t\tLEFT JOIN Family_Visit_History AS FVH ON KH.House_Hold_ID = FVH.House_Hold_ID \n" +
"\t\tleft join Family_Migration_Register as fmr on KH.House_Hold_ID = fmr.House_Hold_ID \n" +
"\t\tWHERE\n" +
"\t\t\t(\n" +
"\t\t\t\tFVH.Visit_Status = 0\n" +
"\t\t\t\tOR FVH.Visit_Status IS NULL\n" +
"\t\t\t) \n" +
"\t\t--AND ((date(FVH.Visit_Date) >= (SELECT date(Starting_Date)FROM Working_Cycle WHERE DATE('NOW') BETWEEN date(Starting_Date)AND date(Ending_Date)) \n" +
"\t\t--AND date(FVH.Visit_Date) <= (SELECT date(Ending_Date) FROM Working_Cycle WHERE DATE('NOW') BETWEEN date(Starting_Date)AND date(Ending_Date))) OR date(FVH.Visit_Date) IS NULL) \n" +
"\t\tAND (KH.House_Hold_ID NOT IN (SELECT DISTINCT House_Hold_ID FROM Family_Visit_History WHERE Visit_Status = 1 )) \n" +
"\t\tGROUP BY\n" +
"\t\t\tKH.Para_Structure_ID \n" +
"\t) AS FM \n" +
"WHERE\n" +
"\tWAS.Para_Structure_ID = ST.Para_Structure_ID \n" +
"\n" +
"AND WAS.Para_Structure_ID = FM.Para_Structure_ID\n" +
"\n" +
"AND ST.Para_Structure_ID = FM.Para_Structure_ID\n" +
"\n";
if (!searched_Str.trim().equals("null") && !searched_Str.trim().equals(""))
{
selectQuery += " And WAS.Structure_No like '%"+searched_Str+"%'";
}
selectQuery += " ORDER BY WAS.Para_Code, Cast(WAS.Structure_No As int) limit "+limit+" offset "+offset;
try{
Cursor cursor = database.rawQuery(selectQuery, null);
if (cursor.getCount() > 0) {
if (cursor != null && cursor.moveToFirst()) {
do {
HouseModal dkrd = new HouseModal();
dkrd.setStructureID(cursor.getInt(0));
dkrd.setStructureNo(cursor.getString(1));
dkrd.setFamilies(cursor.getInt(2));
dkrd.setMembers(cursor.getInt(3));
dkrd.setFamilyMigrationID(cursor.getString(4));
list_krd.add(dkrd);
} while (cursor.moveToNext());
}
}
cursor.close();
}
catch (Exception e){
e.printStackTrace();
}
return list_krd;
}
这是我的模态课
package pk.softech.sukh.modal;
public class HouseModal {
private int structureID, families,members;
private String migrationID;
private String houseHoldID;
private String structureNo ;
public String getFamilyMigrationID() {
return migrationID;
}
public void setFamilyMigrationID(String newMigrationID) {
this.migrationID = newMigrationID;
}
public String getStructureNo() {
return structureNo;
}
public void setStructureNo(String structureNo) {
this.structureNo = structureNo;
}
public int getStructureID() {
return structureID;
}
public void setStructureID(int structureID) {
this.structureID = structureID;
}
public int getFamilies() {
return families;
}
public void setFamilies(int families) {
this.families = families;
}
public int getMembers() {
return members;
}
public void setMembers(int members) {
this.members = members;
}
public String getHouseHoldID() {
return houseHoldID;
}
public void setHouseHoldID(String houseHoldID) {
this.houseHoldID = houseHoldID;
}
}
下面是我的适配器代码,我正在使用getter“ getFamilyMigrationID”获取数据。因此,场景是,如果此吸气剂的值为“ null”,它将给出浅灰色的背景;否则,如果此吸气剂的值为,则它将给出浅黄色的背景。我正在做一个错误,但不知道错误是什么?
package pk.softech.sukh.adapter;
import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import java.util.List;
import pk.softech.sukh.R;
import pk.softech.sukh.modal.HouseModal;
public class HouseAdapter extends ArrayAdapter<HouseModal> {
public HouseAdapter(Context context,List<HouseModal> items) {
super(context,R.layout.view_house_item,items);
}
class ViewHolder{
TextView totalAfrad,totalKhandan,structureID,migratedFamilies;
}
@Override
public View getView(int position, View view, ViewGroup parent) {
ViewHolder holder;
HouseModal item = getItem(position);
if(view == null){
view = LayoutInflater.from(getContext()).inflate(R.layout.view_house_item,null);
holder = new ViewHolder();
holder.structureID = (TextView) view.findViewById(R.id.structureID);
holder.totalAfrad = (TextView) view.findViewById(R.id.totalAfrad);
holder.totalKhandan = (TextView) view.findViewById(R.id.totalKhandan);
holder.migratedFamilies = (TextView) view.findViewById(R.id.families);
if(item.getFamilyMigrationID() == null){
view.findViewById(R.id.structureID).setBackgroundColor(Color.parseColor("#CFD8DC")); //light gray
} else {
view.findViewById(R.id.structureID).setBackgroundColor(Color.parseColor("#FDFDE9")); //light yellow
}
view.setTag(holder);
}
else{
holder = (ViewHolder) view.getTag();
}
holder.structureID.setText(String.valueOf(item.getStructureNo()));
holder.totalKhandan.setText(String.valueOf(item.getFamilies()));
holder.totalAfrad.setText(String.valueOf(item.getMembers()));
holder.migratedFamilies.setText(String.valueOf(item.getFamilyMigrationID()));
return view;
}
}
答案 0 :(得分:0)
如果您的商品为空,如何更改其颜色或如何检查?
null为空。
您应检查Null,以确保崩溃不会改变颜色的安全性。 如果要更改颜色,则应该从数据库中获取一个特定字段,该字段显示此列中没有值或为空。 然后可以检查其颜色
答案 1 :(得分:0)
放入
if(holder.structureID.isEmpty(){
view.findViewById(R.id.structureID).setBackgroundColor(Color.parseColor("#CFD8DC")); //light gray
} else {
view.findViewById(R.id.structureID).setBackgroundColor(Color.parseColor("#FDFDE9")); //light yellow
}
在if(view == null)之外并进入else块,以便它在重绘时得到更改,因为recyclerview将重用单元格,并且需要更改的任何内容(文本,值,颜色)还需要重新应用
编辑:只是为了澄清-如果您更改getView()方法以便仅设置,您仍然需要在if块中使用以前的“ if语句更改颜色if语句”您的if / else语句中的viewHolder对象的值,然后在下面更改您需要在视图本身上更改的所有值,这样最终将变得更整洁和易于阅读