从ListView中的特定行获取特定TextView的值。 Android的

时间:2018-06-08 05:05:55

标签: android listview

我有一个ListView,ListView中的每一行都有三个TextViews。单击一行时,将突出显示该行。我希望从该行的特定TextView中获取文本,特别是' listview_row_employee_id'。

我怎么能以编程方式做到这一点? 请参阅XML ListView行布局和Activity Java逻辑。

XML

<TextView
    android:layout_width="0dp"
    android:layout_weight="1"
    android:layout_height="wrap_content"
    android:textSize="@dimen/medium"
    android:id="@+id/listview_row_employee_id"

    />

<LinearLayout
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="3"
    >
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="@dimen/medium"
        android:id="@+id/listview_row_employee_first_name"
        />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text=" "
        android:textSize="@dimen/medium"
        />

    <TextView
        android:layout_width="wrap_content"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:textSize="@dimen/medium"
        android:id="@+id/listview_row_employee_last_name"
        />


</LinearLayout>

爪哇

private void instantiateListViewClickListener() {

    listview_employee.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
            viewEmployeeDetailsButton.setEnabled(true);
            viewEmployeeDetailsButton.setTextColor(Color.parseColor("#41ea12"));


            rowView = view;
            listViewPosition = position;
            //If there is a previously selected view in the list, it will be un-highlighted when
            //a new view within the list is selected.
            if (previouslySelectedView != null) {
                previouslySelectedView.setBackgroundColor(0);
                previouslySelectedView = view;
            } else {
                previouslySelectedView = view;
            }
            //Clicked view in list will be highlighted. Light blue
            view.setBackgroundColor(Color.parseColor("#66ccff"));
        }
    });
}


private void updateListView() {
    employeeList = dbDataAccess.getEmployeeList();

    Cursor cursor = dbDataAccess.getEmployeeListCursor();

    String[] columns = {
            DBOpenHelperEmployeeCenter.EMPLOYEES_COLUMN_ID,
            DBOpenHelperEmployeeCenter.EMPLOYEES_COLUMN_FIRST_NAME,
            DBOpenHelperEmployeeCenter.EMPLOYEES_COLUMN_LAST_NAME
    };


    int[] resourceIds = {
            R.id.listview_row_employee_id,
            R.id.listview_row_employee_first_name,
            R.id.listview_row_employee_last_name
    };


    listAdapter = new SimpleCursorAdapter(this, R.layout.employee_listview_row,
            cursor, columns, resourceIds, 0);



    listview_employee = (ListView) findViewById(R.id.listview_employees);
    listview_employee.setAdapter(listAdapter);
}

1 个答案:

答案 0 :(得分:0)

使用onItemClick方法的第二个参数获取TextView

这应该有效,

listview_employee.setOnItemClickListener(new AdapterView.OnItemClickListener() {

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
        viewEmployeeDetailsButton.setEnabled(true);
        viewEmployeeDetailsButton.setTextColor(Color.parseColor("#41ea12"));

        String empId = ((TextView) view.findViewById(R.id.listview_row_employee_id)).getText().toString();

        //other logic
    }
});