如何在每个tablerow上单击Textview

时间:2017-11-26 18:03:22

标签: android

我在做管理页面。作为管理员,他必须能够编辑表格每行的数据。但是当我点击编辑时,它无法进入新活动。我不确定是什么问题。以下是我的表格图片。enter image description here

下面是我的java编码,我缩短了一些不重要的编码:

public class assessment_table_edit extends AppCompatActivity implements View.OnClickListener{

    String data = "";
    TableLayout tlAssessment;
    TableRow tr;
    TextView stuID,totalmarks,marks,edit;

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_assessment_table_edit);
        tlAssessment=(TableLayout)findViewById(R.id.tlAssessment_Edit);
        edit=(TextView)findViewById(R.id.assessment_edit);
        final Assessment_Information_GetData getdb=new Assessment_Information_GetData();
        new Thread(new Runnable() {
            @Override
            public void run() {
                data =getdb.getDataFromDB();
                System.out.println(data);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        ArrayList<Assessment_Information> users=parseJSON(data);
                        addData(users);
                    }
                });
            }
        }).start();


        if(getSupportActionBar()!=null){
            getSupportActionBar().setDisplayShowHomeEnabled(true);
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.assessment_edit:
                Intent iChange=new Intent(assessment_table_edit.this,edit_details.class);
                startActivity(iChangePassword);
                break;

        }

    }

    @SuppressWarnings("deprecation")
    void addHeader(){

        edit=new TextView(this);
        edit.setText("Modify");
        edit.setGravity(Gravity.CENTER);
        edit.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT));
        edit.setPadding(2,2,2,2);
        edit.setTextColor(Color.parseColor("#FFFFFF"));
        edit.setBackgroundColor(Color.parseColor("#607D8B"));
        Ll=new LinearLayout(this);
        params = new LinearLayout.LayoutParams(TableRow.LayoutParams.FILL_PARENT,
                TableRow.LayoutParams.WRAP_CONTENT);
        params.setMargins(2,2,2,2);
        Ll.addView(edit,params);
        tr.addView((View)Ll);

        tlAssessment.addView(tr,
                new TableLayout.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
    }

    @SuppressWarnings({ "rawtypes", "deprecation" })
    public void addData(ArrayList<Assessment_Information>users){
        addHeader();
        for (Iterator i = users.iterator(); i.hasNext();) {
            Assessment_Information p=(Assessment_Information)i.next();
            tr=new TableRow(this);

           //Edit here
            edit=new TextView(this);
            edit.setText("Edit");
            edit.setOnClickListener(this);
            edit.setGravity(Gravity.CENTER);
            edit.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT));
            edit.setPadding(2, 2, 2, 2);
            edit.setTextColor(Color.parseColor("#3F51B5"));
            edit.setBackgroundColor(Color.parseColor("#90CAF9"));
            Ll = new LinearLayout(this);
            params = new LinearLayout.LayoutParams(TableRow.LayoutParams.FILL_PARENT,
                    TableRow.LayoutParams.WRAP_CONTENT);
            params.setMargins(2, 2, 2, 2);
            Ll.addView(edit,params);
            tr.addView((View) Ll);

            tlAssessment.addView(tr,
                    new TableLayout.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT));


        }
    }

下面是我的xml编码:

 <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true" >

    <TableLayout
        android:id="@+id/tlAssessment_Edit"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:stretchColumns="*" >

    </TableLayout>
    </ScrollView>
    <TextView
        android:id="@+id/assessment_edit"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:visibility="gone" />

感谢有人可以启发我。

1 个答案:

答案 0 :(得分:0)

当您动态生成android { testOptions { unitTests { all { jvmArgs '-noverify', '-ea' jacoco { includeNoLocationClasses = true } } includeAndroidResources = true } } }

首先创建单独的textview方法

OnClickListener

现在在values / strings.xml中定义自定义ID:

 OnClickListener onclicklistener = new OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch (v.getId()){
            case R.id.my_custom_id:
                Intent iChange=new Intent(assessment_table_edit.this,edit_details.class);
                startActivity(iChange);
                break;
        }
    }
};

现在使用以下命令将您的id和侦听器动态设置为textview:

<item name="my_custom_id" type="id" />