如何在TableLayout中获取TableRow所在的索引?

时间:2014-09-29 02:20:27

标签: java android layout-inflater

因此,我正在创建一个小提醒应用,您创建提醒,它将TableRow添加到TableLayout,这是ScrollView的子级。一切都很基础。

我想要做的是当我使用LayoutInflater将TableRow添加到TableLayout时,如何在TableRow上设置标记,以及如何在我的代码中获取存储在TableLayout中的信息?

当前代码:

public class MainActivity extends ActionBarActivity {

    private TableLayout reminderTableScrollView;

    Button deleteReminderButton;

    Toast remindersToast;


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

        reminderTableScrollView = (TableLayout) findViewById(R.id.reminderTableScrollView);

        deleteReminderButton = (Button) findViewById(R.id.deleteButton);
        deleteReminderButton.setOnClickListener(deleteClickListener);
    }

    private void goToEnterData() {
        Intent intent = new Intent(this, ReminderData.class);
        startActivityForResult(intent, 1);
    }

    protected void onActivityResult(int requestCode, int resultCode, Intent returnIntent) {

        if (requestCode == 1) {
            if(resultCode == RESULT_OK){
                String title = returnIntent.getStringExtra("title");
                String description = returnIntent.getStringExtra("description");
                String hour = returnIntent.getStringExtra("hour");
                String minute = returnIntent.getStringExtra("minute");
                String amPm = returnIntent.getStringExtra("amPm");

                Log.d("Debug", String.valueOf(list));
                Log.d("Debug", hour + ":" + minute);

                inflate(title, description, hour, minute, amPm);
            }
            if (resultCode == RESULT_CANCELED) {
                displayToast(new Toast(this), "Reminder canceled");
            }
        }
    }

    private void inflate(String text, String description, String hour, String minute, String amPm) {
        LayoutInflater inflator = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        View reminder = inflator.inflate(R.layout.reminder_layout, null);

        TextView reminderListTextView = (TextView) reminder.findViewById(R.id.reminderListTextView);
        reminderListTextView.setText(text);

        TextView reminderListTimeTextView = (TextView) reminder.findViewById(R.id.timeTextView);
        reminderListTimeTextView.setText(hour + ":" + minute + ((amPm == "0") ? " AM" : " PM"));

        reminderTableScrollView.addView(reminder);

        Log.d("Debug", String.valueOf(reminderTableScrollView.getTag(0)));
    }

ReminderData.java

public class ReminderData extends ActionBarActivity implements OnClickListener {

    EditText titleEditText;
    EditText descriptionEditText;
    EditText hourTimeEditText;
    EditText minuteTimeEditText;

    Spinner spinner;

    Button enterButton;

    Toast toast;

    SQLiteDatabase myDB;

    String title;
    String description;

    int spinnerPosition;

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

        titleEditText = (EditText) findViewById(R.id.titleEditText);
        descriptionEditText = (EditText) findViewById(R.id.descriptionEditText); 

        hourTimeEditText = (EditText) findViewById(R.id.hourTimeEditText);
        minuteTimeEditText = (EditText) findViewById(R.id.minuteTimeEditText);

        spinner = (Spinner) findViewById(R.id.ampm_spinner);
        spinner.setOnItemSelectedListener(spinnerItemSelectedListener);

        enterButton = (Button) findViewById(R.id.enterButton);
        enterButton.setOnClickListener(this);
    }

    private OnItemSelectedListener spinnerItemSelectedListener = new OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view,
                int position, long id) {
            spinnerPosition = position;
        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {

        }
    };

    private void returnData(String titleString, String descriptionString, String hour, String minute, String amPm) {
        Intent returnIntent = new Intent();
        returnIntent.putExtra("title", titleString);
        returnIntent.putExtra("description", descriptionString);
        returnIntent.putExtra("hour", hour);
        returnIntent.putExtra("minute", minute);
        returnIntent.putExtra("amPm", amPm);

        setResult(RESULT_OK, returnIntent);
        finish();
    }

对于大量混乱的代码感到抱歉,通常我会尽量保持简短,人们会要求更多代码

编辑:我删除了不必要的编码

1 个答案:

答案 0 :(得分:0)

  

我想要做的是当我将TableRow添加到TableLayout时   LayoutInflater,如何在TableRow上设置标签,我该怎么做   稍后在我的代码中获取存储在该TableLayout中的信息?

您可以将TableRow的索引设置为它的标记。在你的膨胀(String text,String description,String hour,String minute,String amPm)方法中这样做:

int index = reminderTableScrollView.getChildCount();
reminder.setTag(index);
reminderTableScrollView.addView(reminder);

然后像这样检索索引:

int index = (Integer)reminder.getTag();

<强>更新

根据您的评论,您可以通过将OnClickListener设置为每个TableRow来实现您的目标:

    int index = reminderTableScrollView.getChildCount();
    reminder.setTag(index);
    reminder.setOnClickListener(new OnClickListener(){
        public void onClick(View v){
            // The View you get here is actually the reminder
            int index = (Integer)reminder.getTag();
            // Now you have the index, you can do what you want.
        }
    });
    reminderTableScrollView.addView(reminder);