user8750453
user8750453

Reputation:

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.enter image description here

Below is my java coding, I have cut short some not important coding:

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:

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

Upvotes: 1

Views: 55

Answers (1)

rafsanahmad007
rafsanahmad007

Reputation: 23881

As you are dynamically generating the textview:

First create your separate OnClickListener method

 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:

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

Now set your id and listener dynamically to the textview using:

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

Upvotes: 1

Related Questions