【问题标题】:Get textview of custom adapter android获取自定义适配器android的textview
【发布时间】:2015-12-10 22:28:09
【问题描述】:

我有一个自定义视图适配器,它有四个按钮和一个填充 ListView 的标题。

当用户单击其中一个按钮时,我想检索与该特定适配器关联的标题。 到目前为止,我已经尝试检索父级并获取 textView,但这并没有返回正确的标题。 那么...我如何获得附属于具有用户单击按钮的适配器的特定标题?

如果您需要更多信息,我很乐意澄清。

这是适配器的布局。

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

这是我实现 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());

它填充了一个列表视图,如果我要单击 fragment_student_home_attendance_imageButton,我希望获得与该 ClassAdapter 关联的 fragment_student_home_classTitle_textView。

【问题讨论】:

    标签: java android android-listview android-custom-view


    【解决方案1】:

    只是给你一个想法,希望这可以帮助解决问题:)

    @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
       }
    
    }
    

    【讨论】:

    • 感谢您的回复!实际上,我的适配器中的四个按钮已经有了一个 switch 语句。我只是没有在我的代码末尾添加它。现在的主要问题是 onClick(View v) 函数,我试图获得一个视图,它是适配器的一部分,但不可点击。我试过 View parent = v.getParent() 但是 TextView textview = (TextView) parent.findViewById(R.id.fragment_student_home_classTitle_textView) 返回 null,即使它列在我上面的 XML 文件中。
    【解决方案2】:

    好吧,没关系,我明白了。

    @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());
    

    如果有更好的方法来获得这个解决方案,因为它似乎是解决问题的正面方法,我欢迎任何 cmets!

    【讨论】:

      猜你喜欢
      • 2014-04-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-23
      • 2012-06-18
      • 2021-03-15
      相关资源
      最近更新 更多