简体   繁体   English

如何使每个 tablerow 上的 Textview 可点击

[英]How to make Textview clickable on each tablerow

I am doing admin page.我正在做管理页面。 As an admin, he must able to edit the data of each row of the table.作为管理员,他必须能够编辑表格每一行的数据。 But when I click on the Edit, it not able to go to a new activity.但是当我单击“编辑”时,它无法转到新活动。 I am not sure what is wrong.我不确定出了什么问题。 Below is my table image.下面是我的桌子图片。 在此处输入图片说明

Below is my java coding, I have cut short some not important coding:下面是我的 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));


        }
    }

Below is my xml coding:下面是我的 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" />

As you are dynamically generating the textview :当您动态生成textview

First create your separate OnClickListener method首先创建您单独的OnClickListener方法

 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;
        }
    }
};

Now define your custom id in your values/strings.xml:现在在您的 values/strings.xml 中定义您的自定义 id:

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

Now set your id and listener dynamically to the textview using:现在使用以下方法将您的 id 和侦听器动态设置为 textview:

edit.setId(R.id.my_custom_id);
edit.setOnClickListener(onclicklistener);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM