简体   繁体   中英

How to call method from activity to change layout?

How can I call the changeTitle() method of VideoLayout inside the onItemClick event of the activity?

I'm trying to figure out what's the best practice to change the content of a list item.

Doing v.changeTitle((VideoModel) videoAdapter.getItem(position).title); (which I think it's the the first solution that's come up to my mind) throws a "undefined method" error.

Any toughts?

I won't explain the code because I think it's self explanatory:


Activity:

public class MainActivity extends BaseActivity {

    public void setVideosList()
    {
        videoAdapter = new VideoAdapter(this, videoItems);
        listView.setAdapter(videoAdapter);
        listView.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() {
                @Override
        public void onItemClick(AdapterView<?> av, View v, int position, long arg3) {

        }
    });
    new getVideosTask().execute();
}

}

video.xml (I just pasted the beggining of it. This layout is what goes inside a list item)

<com.company.project.VideoLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="15dp"
android:paddingTop="15dp"
android:paddingLeft="15dp"
android:paddingRight="15dp"
android:descendantFocusability="blocksDescendants"
android:id="@+id/wrapper">

VideoAdapter.java:

public class VideoAdapter extends BaseAdapter{
@Override
public View getView(final int position, View convertView, ViewGroup parent)
{
    VideoLayout view;
    if (convertView == null) {
        view = (VideoLayout) LayoutInflater.from(context).inflate(R.layout.video, parent, false);
    }else{
        view = (VideoLayout) convertView;
    }

    VideoModel item = (VideoModel) getItem(position);
    if (item != null) {
        view.prepare(item);
    }

    return view;
}
}

VideoModel.java:

class VideoModel {

public String title;
public String videoUrl;
public String imageUrl;

}

VideoLayout.java:

class VideoLayout extends LinearLayout{
public Context context;

public MyImageView imageView;
public MyVideoView videoView;
public TextView titleView;

public VideoLayout(Context context) {
    super(context);
    this.context = context;
}

public VideoLayout (Context context, AttributeSet attrs) {
    super(context, attrs);
}

public VideoLayout (Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
}

protected void onFinishInflate()
{
    super.onFinishInflate();
    imageView = (MyImageView) findViewById(R.id.placeholder);
    videoView = (MyVideoView) findViewById(R.id.video);
    titleView = (TextView) findViewById(R.id.title);
}

public void changeTitle(String text){
    titleView.setText(text);
}   
}
 @Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    View wrapper = view.findViewById(R.id.wrapper);
    if(wrapper != null && wrapper instanceof VideoLayout) {
        wrapper.changeTitle("title");
    }
}

Solved!

I've simplified it (based on @thinear's answer):

@Override
        public void onItemClick(AdapterView<?> av, View v, int position,
                long id) {
            VideoLayout wrapper = (VideoLayout) v.findViewById(R.id.wrapper);
            wrapper.changeTitle("title");
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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