Я разрабатываю пример приложения, чтобы понять, как работать с данными цикла в таблице.Программно получить выбранные данные строк
Следующие задачи, с которыми я столкнулся, - это как получить данные при нажатии на определенные строки.
XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_profile"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.singatour.touristattraction.ProfileActivity">
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TableLayout
android:id="@+id/tlGuide"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left">
</TableLayout>
</ScrollView>
</LinearLayout>
Простая программа
package com.singatour.touristattraction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
public class GuideActivity extends AppCompatActivity
{
TextView tvGuideName = null;
TableLayout tlGuide = null;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_guide);
tlGuide = (TableLayout) findViewById(R.id.tlGuide);
for(int i = 0; i < 100; i++)
{
TableRow rows = new TableRow(this);
TableRow.LayoutParams lp = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT);
rows.setLayoutParams(lp);
tvGuideName = new TextView(this);
tvGuideName.setId(i);
tvGuideName.setText("I am in row " + i);
rows.addView(tvGuideName);
tlGuide.addView(rows, i);
rows.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
TableRow t = (TableRow) v;
TextView firstTextView = (TextView) t.getChildAt(0);
String firstText = firstTextView.getText().toString();
Log.d("GuideActivity", firstText);
}
});
}
}
}
Я могу петлю в 100 строк данных, начиная от
Я нахожусь в строке 0
Я в строке 1
Я в строке 2
..
..
Я в строке 99
Однако, есть способ, чтобы получить получить данные, которые я установил на коснувшись на ряд?
РЕЗЬБА ЗАКРЫТА. ОТВЕТ ОБ ОБНОВЛЕНО В МОЙ ВОПРОС.
возможно регистрировать onClickListener в текстовом режиме? что вы думаете? – Lino