Android如何在TableView中设置TextView内容

时间:2015-02-24 19:06:28

标签: android textview

我有一个带有一些TextView的表。 我点击其中一个startActivityForResult并输入数据并将其放入按下的TextView。 问题是我在此布局中有60 TextView。 如何设置点击的TextView以及我使用onActivityResult的结果?

XML:

<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" />

ActivitySetOrario.java

public class ActivitySetOrario extends ActionBarActivity {

TextView textView;
//Static perche cosi non perdo i dati inseriti in precedenza!
static Data DB = new DataBase();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_set_orario);

}//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){

            String result = data.getStringExtra("result"); //Take the materia from Dialog
            int color = data.getIntExtra("color",1); //Take the color from Dialog

            Materia materia;
            materia = new Materia();
            materia.setMateria(result); //put the name materia into class materia
            materia.setColor(color); //put the color materia into class materia
            DB.getMaterie().add(materia);
   //******HERE I NEED TO TAKE THE ID OF THE CLICKER AND SET TEXT WITH RESULT AND BACKGROUND FROM 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();
        return true;
    case R.id.save_data_orario:

        backToOrario();
        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){
    //StartActivityForResult perche mi aspetto la materia inserita dall'altra activity
    Intent myIntent = new Intent(ActivitySetOrario.this, ActivityAddMateria.class);
    ActivitySetOrario.this.startActivityForResult(myIntent, 1);
}

}

2 个答案:

答案 0 :(得分:0)

问题是您的addMateria方法未正确声明,因此Android无法链接onClick操作。

尝试更改

public void addMateria(){
    //StartActivityForResult perche mi aspetto la materia inserita dall'altra activity
    Intent myIntent = new Intent(ActivitySetOrario.this,ActivityAddMateria.class);
    ActivitySetOrario.this.startActivityForResult(myIntent, 1);
}

public void addMateria(View v){
    //To get your TextView do this
    TextView myTextView = (TextView) v;
    //StartActivityForResult perche mi aspetto la materia inserita dall'altra activity
    Intent myIntent = new Intent(ActivitySetOrario.this,ActivityAddMateria.class);
    ActivitySetOrario.this.startActivityForResult(myIntent, 1);
}

答案 1 :(得分:-1)

您的代码无法在点击视图中解析addmateria类。 http://chrisrisner.com/31-Days-of-Android--Day-5%E2%80%93Adding-Multiple-Activities-and-using-Intents 这会帮助你。 找到一个方法addMateria()(View)或者如果它没有在你调用的intent中声明它。