我的应用程序的TableLayout
包含60个texviews。
用户可以触摸它们并DialogActivity
开始,以获取所点击的TextView
的文字和背景颜色。
但该应用程序有一个错误,有时所有TextView
采取第一个输入颜色的颜色和代码内部我没有分配颜色的循环。
我认为,问题来自于屏幕的方向(可能吗?)。
因为包含TableLayout
的活动为landscape
而Dialog
为portrait
。
事实上,当对话框开始后面有一个Activity,它会改变他对话的方向,所有的文本视图都会改变它们的颜色。
我该如何避免这个错误?
为什么会这样?
活动景观:
public class ActivitySetOrario extends ActionBarActivity {
//Static perch� cosi non perdo i dati inseriti in precedenza!
static int clickedTextViewId; // Declare TextView as class level member field
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_set_orario);
MySQLiteHelper db = new MySQLiteHelper(this);
//Get all materie inside database
List<Materia> materia = db.getAllMaterie();
//change all TextView inputed from user
if(materia.isEmpty()){
//do nothing
}else {
for (Materia mat : materia) {
//Change all the TextView with values stored inside the database
TextView changedtextview = (TextView) findViewById(mat.getID());
changedtextview.setText(mat.getMateria());
changedtextview.setBackgroundColor(mat.getColor());
}
}
}//Fine oncreate
//Prende indietro la materia aggiunta dall'ActivityAddMateria
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == 1) {
if (resultCode == RESULT_OK) {
MySQLiteHelper db = new MySQLiteHelper(this);
String result = data.getStringExtra("result"); //Take the materia from Dialog
int color = data.getIntExtra("color", 1); //Take the color from Dialog
//Here i need to recognize row and column
db.addMateria(new Materia(clickedTextViewId, result, color));
TextView clickedtextView = (TextView) findViewById(clickedTextViewId); //(TextView) view;
clickedtextView.setText(result);
clickedtextView.setBackgroundColor(color);
}
if (resultCode == RESULT_CANCELED) {
//Nessuna materia inserita
}
}
}//onActivityResult
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_set_orario, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
switch (item.getItemId()) {
case R.id.draw_orario:
//addMateria();
MySQLiteHelper db = new MySQLiteHelper(this);
db.deleteMateria();
onStart();
return true;
case R.id.save_data_orario:
//SERIALIZZO I DATI CHE DOVRA PRENDERE ActivityOrario
backToOrario();
finish();
return true;
case R.id.exit_orario:
//Torno alla schermata orario annullo ogni modifica NON SERIALIZZO
backToOrario();
finish();
return true;
case R.id.action_settings:
return true;
default:
return super.onOptionsItemSelected(item);
}
}
//Torna alla ActivityOrario
public void backToOrario(){
Intent myIntent = new Intent(ActivitySetOrario.this, ActivityOrario.class);
startActivity(myIntent);
}
public void addMateria(View v){
//To get ID of your TextView do this
clickedTextViewId = v.getId();
//StartActivityForResult perche mi aspetto la materia inserita dall'altra activity
Intent myIntent = new Intent(ActivitySetOrario.this, ActivityAddMateria.class);
ActivitySetOrario.this.startActivityForResult(myIntent, 1);
}
}
对话框肖像活动:
public class ActivityAddMateria extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_materia);
final Button exit_button = (Button) findViewById(R.id.exit_dialog_materia);
exit_button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//No input
Intent returnIntent = new Intent();
setResult(RESULT_CANCELED, returnIntent);
//Exit from Dialog
finish();
}
});
final Button accept_button = (Button) findViewById(R.id.add_materia);
accept_button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//Find EditText for take data
EditText nome_materia = (EditText)findViewById(R.id.nome_materia);
//Put result into variable result that is send back
String result = nome_materia.getText().toString();
RadioGroup radioGroup = (RadioGroup) findViewById(R.id.group1);
int radioButtonID = radioGroup.getCheckedRadioButtonId();
View radioButton = radioGroup.findViewById(radioButtonID);
Drawable background = radioButton.getBackground();
if (background instanceof ColorDrawable) {
int color = ((ColorDrawable) background).getColor();
Intent returnIntent = new Intent();
returnIntent.putExtra("result",result).putExtra("color",color);
setResult(RESULT_OK,returnIntent);
}
// Exit to Dialog
finish();
}
});
}
}
第一项活动的xml:
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/blue_orario"
android:id="@+id/table">
<TableRow
android:id="@+id/dayrow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="35dp" >
<TextView
android:id="@+id/d1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Lun."
android:textColor="@color/text_orario"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/d2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Mar."
android:textColor="@color/text_orario"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/d3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Mer."
android:textColor="@color/text_orario"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/d4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Gio."
android:textColor="@color/text_orario"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/d5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Ven."
android:textColor="@color/text_orario"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/d6"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Sab."
android:textColor="@color/text_orario"
android:textAppearance="?android:attr/textAppearanceLarge" />
</TableRow>
<ScrollView
android:id="@+id/scrollorario"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TableRow
android:id="@+id/prima_riga"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/h1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1"
android:textColor="@color/text_orario"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/mat11"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:gravity="center"
android:clickable="true"
android:onClick="addMateria"
android:background="@color/grigio_chiaro"
android:text=""/>
<TextView
android:id="@+id/mat12"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:layout_marginLeft="33dp"
android:gravity="center"
android:clickable="true"
android:onClick="addMateria"
android:background="@color/grigio_chiaro"
android:text="" />
<TextView
android:id="@+id/mat13"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:layout_marginLeft="33dp"
android:gravity="center"
android:clickable="true"
android:onClick="addMateria"
android:background="@color/grigio_chiaro"
android:text="" />
<TextView
android:id="@+id/mat14"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:layout_marginLeft="33dp"
android:gravity="center"
android:clickable="true"
android:onClick="addMateria"
android:background="@color/grigio_chiaro"
android:text="" />
<TextView
android:id="@+id/mat15"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:layout_marginLeft="33dp"
android:gravity="center"
android:clickable="true"
android:onClick="addMateria"
android:background="@color/grigio_chiaro"
android:text="" />
<TextView
android:id="@+id/mat16"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:layout_marginLeft="33dp"
android:gravity="center"
android:clickable="true"
android:onClick="addMateria"
android:background="@color/grigio_chiaro"
android:text="" />
</TableRow>
<!--TOO LONG THE XML I CUT IT THE OTHER ROW ARE THE SAME-->
</LinearLayout>
</ScrollView>
</TableLayout>
一些屏幕截图:
答案 0 :(得分:0)
您不应该在数据库中使用textviews的ID:它们可以在您应用的不同编辑之间进行更改。这可能是罪魁祸首。但是,它们全部设置的唯一方法是在for循环中:您应该验证数据库是否正确并验证每次都没有运行循环。