我在我的应用程序中使用Edittexts。但无法在运行时设置背景颜色。如果我做错了,请建议。
我已经采取了三个edittexts,它们应该根据do while循环中使用的编码来改变颜色。请检查并做出响应ASAp。提前谢谢。
这是我的代码。
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
List<Map<String,String>> list = new ArrayList<Map<String,String >>();
EditText ed1 = (EditText) findViewById(R.id.name);
EditText ed2 = (EditText) findViewById(R.id.age);
EditText ed3 = (EditText) findViewById(R.id.date);
Map<String,String> map = new HashMap<String, String>();
int i = 2;
EmpDatabase empClick = new EmpDatabase(getApplicationContext());
Cursor cursor = empClick.getDetails();
if(cursor.moveToFirst()){
do{
i= i+1;
if(i % 2 == 0)
{
// here is something wrong
ed1.setBackgroundColor(Color.BLUE);
ed2.setBackgroundColor(Color.BLUE);
ed3.setBackgroundColor(Color.BLUE);
}
else
{
// here is something wrong
ed1.setBackgroundColor(Color.CYAN);
ed2.setBackgroundColor(Color.CYAN);
ed3.setBackgroundColor(Color.CYAN);
}
map = new HashMap<String, String>();
String name = cursor.getString(cursor.getColumnIndex("name"));
String age = cursor.getString(cursor.getColumnIndex("age"));
String time = cursor.getString(cursor.getColumnIndex("time"));
map.put("name",name);
map.put("age",age);
map.put("time", time);
list.add(map);
}while(cursor.moveToNext());
cursor.close();
}
SimpleAdapter adapter = new SimpleAdapter(this, list, R.layout.text_view, new String [] {"name", "age", "time"}, new int[] {R.id.name,R.id.age, R.id.date});
setListAdapter(adapter);
}
答案 0 :(得分:1)
你对setContentView(YOUR_LAYOUT)的调用在哪里,基本上没有这个你的布局甚至没有生成,所以findViewById(R.id.name)将不会产生任何结果。
答案 1 :(得分:0)
我得到了这个问题的答案。 我做了一个适配器类,它扩展了简单的适配器..并编写以下代码..
public class EmpCustomAdapter extends SimpleAdapter{
private int[] colors = new int[] { 0x30FF0000, 0x300000FF };
public EmpCustomAdapter(Context context, List<Map<String, String>> list, int resource, String[] from, int[] to) {
super(context, list, resource, from, to);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);
int colorPos = position % colors.length;
view.setBackgroundColor(colors[colorPos]);
return view;
}
}
并创建其对象并设置适配器..
EmpCustomAdapter adapter = new EmpCustomAdapter(this, list, R.layout.text_view, new String [] {"name", "age", "time"}, new int[] {R.id.name,R.id.age, R.id.date});
setListAdapter(adapter);
它工作.. :) 感谢所有帮助我摆脱这个问题的人.. :)因信誉问题无法上传结果图片。:(