简体   繁体   English

获取自定义适配器android的textview

[英]Get textview of custom adapter android

I have a custom view adapter with four buttons and a title that populate a ListView. 我有一个带有四个按钮的自定义视图适配器和一个填充ListView的标题。

When a user clicks one of those buttons, I want to retrieve the title associated with that specific adapter. 当用户单击这些按钮之一时,我想检索与该特定适配器关联的标题。 So far I have tried retrieving the parent and getting the textView but that does not return the correct title. 到目前为止,我已经尝试检索父级并获取textView,但是没有返回正确的标题。 So...how would I get the specific title affiliated to the adapter that has the button the user clicked on? 那么...我将如何获得适配器附带的特定标题,该适配器具有用户单击的按钮?

I'd be happy to clarify if you need more information. 如果您需要更多信息,我们很乐意澄清。

Here is the layout of the adapter. 这是适配器的布局。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">


<TextView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:id="@+id/fragment_student_home_classTitle_textView"
        android:paddingTop="20dp"
    android:layout_toLeftOf="@+id/linearLayout"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />

<LinearLayout
    android:orientation="horizontal"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:layout_gravity="right"
    android:layout_alignParentTop="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:id="@+id/linearLayout">

    <LinearLayout
            android:orientation="vertical"
            android:layout_width="wrap_content"
            android:layout_height="match_parent">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:id="@+id/fragment_student_home_grades"
                android:background="#ffff130c" />

            <ImageButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/fragment_student_home_grades_imageButton"
                android:src="@drawable/ic_grades_512"
                android:background="#00000000" />
        </LinearLayout>

    <LinearLayout
            android:orientation="vertical"
            android:layout_width="wrap_content"
            android:layout_height="match_parent">

        <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:id="@+id/fragment_student_home_notification_textEdit"
                android:background="#ffff130c" />

            <ImageButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/fragment_student_home_notification_imageButton"
                android:src="@drawable/ic_bell_512"
                android:background="#00000000" />
        </LinearLayout>

    <LinearLayout
            android:orientation="vertical"
            android:layout_width="wrap_content"
            android:layout_height="match_parent">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:id="@+id/fragment_student_home_homework"
                android:background="#ffff130c" />

            <ImageButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/fragment_student_home_homework_imageButton"
                android:src="@drawable/ic_foundation_book_bookmark_simple_black_512x512"
                android:contentDescription="@string/homework_notification_icon"
                android:background="#00000000" />
        </LinearLayout>

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:weightSum="1">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:id="@+id/fragment_student_home_attendance"
            android:background="#ffff130c" />

        <ImageButton
            android:layout_width="42dp"
            android:layout_height="40dp"
            android:id="@+id/fragment_student_home_attendance_imageButton"
            android:src="@drawable/ic_attendance"
            android:contentDescription="@string/homework_notification_icon"
            android:background="#00000000" />
    </LinearLayout>
</LinearLayout>

And here is the adapter where I implemented the onClick 这是我实现onClick的适配器

public class ClassAdapter extends ArrayAdapter<SchoolClass> implements View.OnClickListener{
private SchoolClass aClass;
public ClassAdapter(Context context, int resource, ArrayList<SchoolClass> objects) {
    super(context, resource, objects);

}

public SchoolClass getSchoolClass(){
    return this.aClass;
}

public void setSchoolClass(SchoolClass schoolClass){
    this.aClass = schoolClass;
}

@Override
public View getView(int position, View convertView, ViewGroup parent){
    View v = convertView;

    if (v == null) {
        LayoutInflater vi;
        vi = LayoutInflater.from(getContext());
        v = vi.inflate(R.layout.classes_adapter, null);
    }

    aClass = getItem(position);


    if(aClass != null){
        TextView title = (TextView) v.findViewById(R.id.fragment_student_home_classTitle_textView);
        if(title != null){
            title.setText(aClass.getTitle());
        }
        setSchoolClass(aClass);
    }
    //set buttons
    ImageButton notificationsButton = (ImageButton) v.findViewById(R.id.fragment_student_home_notification_imageButton);
    ImageButton gradesButton = (ImageButton) v.findViewById(R.id.fragment_student_home_grades_imageButton);
    ImageButton attendanceButton = (ImageButton) v.findViewById(R.id.fragment_student_home_attendance_imageButton);
    ImageButton homeworkButton = (ImageButton) v.findViewById(R.id.fragment_student_home_homework_imageButton);
    notificationsButton.setOnClickListener(this);
    gradesButton.setOnClickListener(this);
    attendanceButton.setOnClickListener(this);
    homeworkButton.setOnClickListener(this);

    return v;
}

@Override
public void onClick(View v) {
    Intent intent = new Intent(getContext(), ClassActivity.class);
    View parent = (View) v.getRootView();
    //v.getParent() returns null when I look for the textView
    TextView title = (TextView) parent.findViewById(R.id.fragment_student_home_classTitle_textView);
    System.out.println("\n\n title: " + title.getText().toString() + " \n\n");
    intent.putExtra("__CLASS_NAME__", title.getText().toString());

It populates a list view and if I were to click on fragment_student_home_attendance_imageButton I would want to get the fragment_student_home_classTitle_textView associated with that ClassAdapter. 它填充了一个列表视图,如果我要单击fragment_student_home_attendance_imageButton,我想获取与该ClassAdapter关联的fragment_student_home_classTitle_textView。

Just give a idea to you, hope this can help to fix the problem :) 只是给您一个想法,希望这可以帮助解决问题:)

@Override
public View getView(int position, View convertView, ViewGroup parent){
    View v = convertView;
  // ....
ImageButton notificationsButton = (ImageButton) v.findViewById(R.id.fragment_student_home_notification_imageButton);
notificationsButton.setTag(R.id.fragment_student_home_notification_imageButton);
  // ....
return v;
}

@Override
public void onClick(View v) {

    if(v!=null && v.getTag()!=null && v.getTag()==R.id.fragment_student_home_notification_imageButton){

      // do something you like
   }

}

Ok nevermind, I got it. 好吧,没关系,我明白了。

@Override
public void onClick(View v) {
    Intent intent = new Intent(getContext(), ClassActivity.class);


    //It's the parent of the parent OF THE parent where
    //the textView lies in my layout.
    View parent = (View) v.getParent().getParent().getParent();


    TextView title = (TextView) parent.findViewById(R.id.fragment_student_home_classTitle_textView);
    System.out.println("\n\n title: " + title.getText().toString() + " \n\n");
    intent.putExtra("__CLASS_NAME__", title.getText().toString());

If there is a better way to get this solution, as it seems like a head on approach to the problem, I welcome any comments! 如果有更好的方法来获得此解决方案,就好像可以直接解决问题,欢迎提出任何意见!

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

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