我有一个活动,我在其中动态创建了一个表。该表是从 SQLite 数据库填充的。Onclick 表格标题,表格将得到相应的排序。
一切正常,现在我的要求是在单击时更改表头文本视图的文本颜色。文本颜色没有改变。
这是填充表的方法:
//Time ascending method
public void populateTableTimeAscending()
{
getTimeNameTimeAsc();
getStudentNameTimeAsc();
follow_up_table.removeAllViews();
//---------------Table Header-----------------------------------------------
TableRow followup_tr_head = new TableRow(this);
followup_tr_head.setId(10);
followup_tr_head.setBackgroundResource(R.drawable.list_header);
followup_tr_head.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
TextView time_name_title = new TextView(this);
time_name_title.setId(20);
time_name_title.setText("Time");
time_name_title.setTextColor(Color.WHITE);
time_name_title.setPadding(5,5,5,5);
followup_tr_head.addView(time_name_title);// add the column to the table row here
time_name_title.setTextSize(12);
TextView student_name_title = new TextView(this);
student_name_title.setId(20);
student_name_title.setText("Student Name");
student_name_title.setTextColor(Color.WHITE);
student_name_title.setPadding(5,5,5,5);
followup_tr_head.addView(student_name_title);// add the column to the table row here
student_name_title.setTextSize(12);
//----------------------On click time_name_title---------------------------------------
time_name_title.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//makeAToast("Hello!");
populateTableTimeDescending();
}
});
//----------------------On click time_name_title---------------------------------------
//----------------------On click student_name_title---------------------------------------
student_name_title.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//makeAToast("Hello!");
populateTableNameAscending();
}
});
//----------------------On click student_name_title---------------------------------------
follow_up_table.addView(followup_tr_head, new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
//--------------- Table Header-----------------------------------------------
Iterator itr = time_name_list_time_asc.iterator();
Iterator itr2 = student_name_list_time_asc.iterator();
while(itr.hasNext())
{
while(itr2.hasNext())
{
//----------------table body------------------------------------------
followup_tr_data = new TableRow(this);
followup_tr_data.setId(10);
followup_tr_data.setBackgroundResource(R.drawable.grey_list_bg);
followup_tr_data.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
final TextView timeNameText = new TextView(this);
timeNameText.setId(20);
timeNameText.setText(Html.fromHtml(itr.next().toString()));
timeNameText.setTextColor(Color.BLACK);
timeNameText.setPadding(5,5,5,5);
timeNameText.setTextSize(10);
followup_tr_data.addView(timeNameText);// add the column to the table row here
final TextView StudentNameText = new TextView(this);
StudentNameText.setId(20);
StudentNameText.setText(Html.fromHtml(itr2.next().toString()));
StudentNameText.setTextColor(Color.BLACK);
StudentNameText.setPadding(5,5,5,5);
StudentNameText.setTextSize(10);
followup_tr_data.addView(StudentNameText);// add the column to the table row here
follow_up_table.addView(followup_tr_data, new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
//----------------table body------------------------------------------
}
}
}
在time_name_title.setOnClickListener(new OnClickListener()里面我试图改变没有发生的文本颜色。
----------------编辑完整代码------------------------------- ---
public class TableActivity extends Activity {
TableLayout follow_up_table;
TableRow followup_tr_data;
List<String> time_name_list_time_asc;
List<String> student_name_list_time_asc;
List<String> time_name_list_time_dsc;
List<String> student_name_list_time_dsc;
List<String> time_name_list_studname_asc;
List<String> student_name_studname_asc;
List<String> time_name_list_studname_dsc;
List<String> student_name_list_studname_dsc;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_table);
follow_up_table=(TableLayout) findViewById(R.id.follow_up_table);
//getTime();
// database handler
deleteRows();
insertData("Morning", "2013-04-23 10:00:00", "Suresh Kumar");
insertData("Morning", "2013-04-23 11:30:00", "Pravat Das");
insertData("Evening", "2013-04-23 16:16:00", "Namita Roy");
insertData("Evening", "2013-04-23 17:30:00", "Rakesh Mitra");
insertData("After noon", "2013-04-23 14:27:00", "Anil Sarma");
// getTimeNameTimeAsc();
// getStudentNameTimeAsc();
// getTimeNameTimeDsc();
// getStudentNameTimeDsc();
// getTimeNameSyudentAsc();
// getStudentNameStudentAsc();
// getTimeNameStudentDsc();
// getStudentNameStudentDsc();
populateTableTimeAscending();
}
//==================================================================================//
//=============Functions for Table View=====================================================//
//==================================================================================//
//Time ascending method
public void populateTableTimeAscending()
{
getTimeNameTimeAsc();
getStudentNameTimeAsc();
follow_up_table.removeAllViews();
//---------------Table Header-----------------------------------------------
TableRow followup_tr_head = new TableRow(this);
followup_tr_head.setId(10);
followup_tr_head.setBackgroundResource(R.drawable.list_header);
followup_tr_head.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
final TextView time_name_title = new TextView(this);
time_name_title.setId(20);
time_name_title.setText("Time");
time_name_title.setTextColor(Color.WHITE);
time_name_title.setPadding(5,5,5,5);
followup_tr_head.addView(time_name_title);// add the column to the table row here
time_name_title.setTextSize(12);
TextView student_name_title = new TextView(this);
student_name_title.setId(20);
student_name_title.setText("Student Name");
student_name_title.setTextColor(Color.WHITE);
student_name_title.setPadding(5,5,5,5);
followup_tr_head.addView(student_name_title);// add the column to the table row here
student_name_title.setTextSize(12);
//----------------------On click time_name_title---------------------------------------
time_name_title.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//makeAToast("Hello!");
populateTableTimeDescending();
}
});
//----------------------On click time_name_title---------------------------------------
//----------------------On click student_name_title---------------------------------------
student_name_title.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//makeAToast("Hello!");
populateTableNameAscending();
}
});
//----------------------On click student_name_title---------------------------------------
follow_up_table.addView(followup_tr_head, new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
//--------------- Table Header-----------------------------------------------
Iterator itr = time_name_list_time_asc.iterator();
Iterator itr2 = student_name_list_time_asc.iterator();
while(itr.hasNext())
{
while(itr2.hasNext())
{
//----------------table body------------------------------------------
followup_tr_data = new TableRow(this);
followup_tr_data.setId(10);
followup_tr_data.setBackgroundResource(R.drawable.grey_list_bg);
followup_tr_data.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
final TextView timeNameText = new TextView(this);
timeNameText.setId(20);
timeNameText.setText(Html.fromHtml(itr.next().toString()));
timeNameText.setTextColor(Color.BLACK);
timeNameText.setPadding(5,5,5,5);
timeNameText.setTextSize(10);
followup_tr_data.addView(timeNameText);// add the column to the table row here
final TextView StudentNameText = new TextView(this);
StudentNameText.setId(20);
StudentNameText.setText(Html.fromHtml(itr2.next().toString()));
StudentNameText.setTextColor(Color.BLACK);
StudentNameText.setPadding(5,5,5,5);
StudentNameText.setTextSize(10);
followup_tr_data.addView(StudentNameText);// add the column to the table row here
follow_up_table.addView(followup_tr_data, new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
//----------------table body------------------------------------------
}
}
}
//Time descending method
public void populateTableTimeDescending()
{
getTimeNameTimeDsc();
getStudentNameTimeDsc();
follow_up_table.removeAllViews();
//---------------Table Header-----------------------------------------------
TableRow followup_tr_head = new TableRow(this);
followup_tr_head.setId(10);
followup_tr_head.setBackgroundResource(R.drawable.list_header);
followup_tr_head.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
TextView time_name_title = new TextView(this);
time_name_title.setId(20);
time_name_title.setText("Time");
time_name_title.setTextColor(Color.WHITE);
time_name_title.setPadding(5,5,5,5);
followup_tr_head.addView(time_name_title);// add the column to the table row here
time_name_title.setTextSize(12);
TextView student_name_title = new TextView(this);
student_name_title.setId(20);
student_name_title.setText("Student Name");
student_name_title.setTextColor(Color.WHITE);
student_name_title.setPadding(5,5,5,5);
followup_tr_head.addView(student_name_title);// add the column to the table row here
student_name_title.setTextSize(12);
//----------------------On click setOnClickListener---------------------------------------
time_name_title.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
populateTableTimeAscending();
}
});
//----------------------On click setOnClickListener---------------------------------------
//----------------------On click student_name_title---------------------------------------
student_name_title.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//makeAToast("Hello!");
populateTableNameAscending();
}
});
//----------------------On click student_name_title---------------------------------------
follow_up_table.addView(followup_tr_head, new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
//--------------- Table Header-----------------------------------------------
Iterator itr = time_name_list_time_dsc.iterator();
Iterator itr2 = student_name_list_time_dsc.iterator();
while(itr.hasNext())
{
while(itr2.hasNext())
{
//----------------table body------------------------------------------
followup_tr_data = new TableRow(this);
followup_tr_data.setId(10);
followup_tr_data.setBackgroundResource(R.drawable.grey_list_bg);
followup_tr_data.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
final TextView timeNameText = new TextView(this);
timeNameText.setId(20);
timeNameText.setText(Html.fromHtml(itr.next().toString()));
timeNameText.setTextColor(Color.BLACK);
timeNameText.setPadding(5,5,5,5);
timeNameText.setTextSize(10);
followup_tr_data.addView(timeNameText);// add the column to the table row here
final TextView StudentNameText = new TextView(this);
StudentNameText.setId(20);
StudentNameText.setText(Html.fromHtml(itr2.next().toString()));
StudentNameText.setTextColor(Color.BLACK);
StudentNameText.setPadding(5,5,5,5);
StudentNameText.setTextSize(10);
followup_tr_data.addView(StudentNameText);// add the column to the table row here
follow_up_table.addView(followup_tr_data, new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
//----------------table body------------------------------------------
}
}
}
//Name ascending method
public void populateTableNameAscending()
{
getTimeNameSyudentAsc();
getStudentNameStudentAsc();
follow_up_table.removeAllViews();
//---------------Table Header-----------------------------------------------
TableRow followup_tr_head = new TableRow(this);
followup_tr_head.setId(10);
followup_tr_head.setBackgroundResource(R.drawable.list_header);
followup_tr_head.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
TextView time_name_title = new TextView(this);
time_name_title.setId(20);
time_name_title.setText("Time");
time_name_title.setTextColor(Color.WHITE);
time_name_title.setPadding(5,5,5,5);
followup_tr_head.addView(time_name_title);// add the column to the table row here
time_name_title.setTextSize(12);
TextView student_name_title = new TextView(this);
student_name_title.setId(20);
student_name_title.setText("Student Name");
student_name_title.setTextColor(Color.WHITE);
student_name_title.setPadding(5,5,5,5);
followup_tr_head.addView(student_name_title);// add the column to the table row here
student_name_title.setTextSize(12);
//----------------------On click time_name_title---------------------------------------
time_name_title.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//makeAToast("Hello!");
populateTableTimeDescending();
}
});
//----------------------On click time_name_title---------------------------------------
//----------------------On click student_name_title---------------------------------------
student_name_title.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//makeAToast("Hello!");
populateTableNameDescending();
}
});
//----------------------On click student_name_title---------------------------------------
follow_up_table.addView(followup_tr_head, new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
//--------------- Table Header-----------------------------------------------
Iterator itr = time_name_list_studname_asc.iterator();
Iterator itr2 = student_name_studname_asc.iterator();
while(itr.hasNext())
{
while(itr2.hasNext())
{
//----------------table body------------------------------------------
followup_tr_data = new TableRow(this);
followup_tr_data.setId(10);
followup_tr_data.setBackgroundResource(R.drawable.grey_list_bg);
followup_tr_data.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
final TextView timeNameText = new TextView(this);
timeNameText.setId(20);
timeNameText.setText(Html.fromHtml(itr.next().toString()));
timeNameText.setTextColor(Color.BLACK);
timeNameText.setPadding(5,5,5,5);
timeNameText.setTextSize(10);
followup_tr_data.addView(timeNameText);// add the column to the table row here
final TextView StudentNameText = new TextView(this);
StudentNameText.setId(20);
StudentNameText.setText(Html.fromHtml(itr2.next().toString()));
StudentNameText.setTextColor(Color.BLACK);
StudentNameText.setPadding(5,5,5,5);
StudentNameText.setTextSize(10);
followup_tr_data.addView(StudentNameText);// add the column to the table row here
follow_up_table.addView(followup_tr_data, new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
//----------------table body------------------------------------------
}
}
}
//Name descending method
public void populateTableNameDescending()
{
getTimeNameStudentDsc();
getStudentNameStudentDsc();
follow_up_table.removeAllViews();
//---------------Table Header-----------------------------------------------
TableRow followup_tr_head = new TableRow(this);
followup_tr_head.setId(10);
followup_tr_head.setBackgroundResource(R.drawable.list_header);
followup_tr_head.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
TextView time_name_title = new TextView(this);
time_name_title.setId(20);
time_name_title.setText("Time");
time_name_title.setTextColor(Color.WHITE);
time_name_title.setPadding(5,5,5,5);
followup_tr_head.addView(time_name_title);// add the column to the table row here
time_name_title.setTextSize(12);
TextView student_name_title = new TextView(this);
student_name_title.setId(20);
student_name_title.setText("Student Name");
student_name_title.setTextColor(Color.WHITE);
student_name_title.setPadding(5,5,5,5);
followup_tr_head.addView(student_name_title);// add the column to the table row here
student_name_title.setTextSize(12);
//----------------------On click time_name_title---------------------------------------
time_name_title.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//makeAToast("Hello!");
populateTableTimeDescending();
}
});
//----------------------On click time_name_title---------------------------------------
//----------------------On click student_name_title---------------------------------------
student_name_title.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//makeAToast("Hello!");
populateTableNameAscending();
}
});
//----------------------On click student_name_title---------------------------------------
follow_up_table.addView(followup_tr_head, new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
//--------------- Table Header-----------------------------------------------
Iterator itr = time_name_list_studname_dsc.iterator();
Iterator itr2 = student_name_list_studname_dsc.iterator();
while(itr.hasNext())
{
while(itr2.hasNext())
{
//----------------table body------------------------------------------
followup_tr_data = new TableRow(this);
followup_tr_data.setId(10);
followup_tr_data.setBackgroundResource(R.drawable.grey_list_bg);
followup_tr_data.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
final TextView timeNameText = new TextView(this);
timeNameText.setId(20);
timeNameText.setText(Html.fromHtml(itr.next().toString()));
timeNameText.setTextColor(Color.BLACK);
timeNameText.setPadding(5,5,5,5);
timeNameText.setTextSize(10);
followup_tr_data.addView(timeNameText);// add the column to the table row here
final TextView StudentNameText = new TextView(this);
StudentNameText.setId(20);
StudentNameText.setText(Html.fromHtml(itr2.next().toString()));
StudentNameText.setTextColor(Color.BLACK);
StudentNameText.setPadding(5,5,5,5);
StudentNameText.setTextSize(10);
followup_tr_data.addView(StudentNameText);// add the column to the table row here
follow_up_table.addView(followup_tr_data, new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
//----------------table body------------------------------------------
}
}
}
//==================================================================================//
//=============Functions for Table View=====================================================//
//==================================================================================//
//==================================================================================//
//=============Functions for DB=====================================================//
//==================================================================================//
//=================Time name ascending=================================
public void getTimeNameTimeAsc()
{
DatabaseHandler db = new DatabaseHandler(getApplicationContext());
time_name_list_time_asc = db.getTimeNameTimeAsc();
Iterator itr = time_name_list_time_asc.iterator();
while(itr.hasNext())
{
System.out.println(itr.next());
}
}
public void getStudentNameTimeAsc()
{
DatabaseHandler db = new DatabaseHandler(getApplicationContext());
student_name_list_time_asc = db.getStudentNameTimeAsc();
Iterator itr = student_name_list_time_asc.iterator();
while(itr.hasNext())
{
System.out.println(itr.next());
}
}
//=================Time name ascending=================================
//=================Time name descending=================================
public void getTimeNameTimeDsc()
{
DatabaseHandler db = new DatabaseHandler(getApplicationContext());
time_name_list_time_dsc = db.getTimeNameTimeDsc();
Iterator itr = time_name_list_time_dsc.iterator();
while(itr.hasNext())
{
System.out.println(itr.next());
}
}
public void getStudentNameTimeDsc()
{
DatabaseHandler db = new DatabaseHandler(getApplicationContext());
student_name_list_time_dsc = db.getStudentNameTimeDsc();
Iterator itr = student_name_list_time_dsc.iterator();
while(itr.hasNext())
{
System.out.println(itr.next());
}
}
//=================Time name descending=================================
//=================Student name ascending=================================
public void getTimeNameSyudentAsc()
{
DatabaseHandler db = new DatabaseHandler(getApplicationContext());
time_name_list_studname_asc = db.getTimeNameSyudentAsc();
Iterator itr = time_name_list_studname_asc.iterator();
while(itr.hasNext())
{
System.out.println(itr.next());
}
}
public void getStudentNameStudentAsc()
{
DatabaseHandler db = new DatabaseHandler(getApplicationContext());
student_name_studname_asc = db.getStudentNameStudentAsc();
Iterator itr = student_name_studname_asc.iterator();
while(itr.hasNext())
{
System.out.println(itr.next());
}
}
//=================Student name ascending=================================
//=================Student name descending=================================
public void getTimeNameStudentDsc()
{
DatabaseHandler db = new DatabaseHandler(getApplicationContext());
time_name_list_studname_dsc = db.getTimeNameStudentDsc();
Iterator itr = time_name_list_studname_dsc.iterator();
while(itr.hasNext())
{
System.out.println(itr.next());
}
}
public void getStudentNameStudentDsc()
{
DatabaseHandler db = new DatabaseHandler(getApplicationContext());
student_name_list_studname_dsc = db.getStudentNameStudentDsc();
Iterator itr = student_name_list_studname_dsc.iterator();
while(itr.hasNext())
{
System.out.println(itr.next());
}
}
//=================Student name descending=================================
//method to insert data into local database
public void insertData(String timeName, String time, String studentName)
{
DatabaseHandler db = new DatabaseHandler(TableActivity.this);
ContentValues values = new ContentValues();
//db.createDataBase();
values.put("timeName",timeName);
values.put("time",time);
values.put("studentName",studentName);
db.insertValues(timeName, time, studentName);
db.close();
}
//method to delete all rows
public void deleteRows()
{
DatabaseHandler db = new DatabaseHandler(getApplicationContext());
db.deleteAllData();
db.close();
}
//==================================================================================//
//=============Functions for DB=====================================================//
//==================================================================================//
//method to show toast message
public void makeAToast(String str) {
//yet to implement
Toast toast = Toast.makeText(this,str, Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
}
}