Android25图灵聊天项目------接口传参

Android25图灵聊天项目------接口传参_第1张图片

把HttpData中的数据通过Myinterface传到MainActivity中

MainActivity:

package com.zhh.tuling;


import android.app.Activity;
import android.os.Bundle;


public class MainActivity extends Activity implements Myinterface {
    HttpData httpData = null;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        try {
            /*HttpData(this)要的是一个HttpGetDataListener的对象,因为MainActivity实现了
             * HttpGetDataListener接口所以给this表示当前对象,有与是子类所以要强转
             */
            httpData = (HttpData) new HttpData(this).execute();
        } catch (Exception e) {
            e.printStackTrace();
        }


    }


    /**
     *接口传参
     *自动调用这个方法 
     *
     */
    @Override
    public void getDataUrl(String data) {
        System.out.println("服务器返回的数据:" + data);


    }


}

Myinterface:

 

 

package com.zhh.tuling;


/**
 * 
 * 用来传参的接口
 *
 */
public interface Myinterface {


    void getDataUrl(String data);
}

HttpData:

package com.zhh.tuling;


import android.os.AsyncTask;


public class HttpData extends AsyncTask {


    private Myinterface listener; //接口传参


    public HttpData(Myinterface listener) {


        this.listener = listener;
    }


    @Override
    protected String doInBackground(String... params) {
        String result = "人的生命是有限的,可是为人民服务是无限的,我要把有限的生命投入到无限的为人民服务中去";
        return result;
    }


    @Override
    protected void onPostExecute(String result) {


        listener.getDataUrl(result);


    }


}

源码下载:

http://download.csdn.net/download/zhaihaohao1/8778063

下面我再贴一个例子

ListView的 适配器 给 Activity 传递数据

先写一个接口:IJieKouArgs

package com.zhh.android.test2;

/**
 * 传参的接口
 */
public interface IJieKouArgs {
    public void setCan(String str);
}

适配器  TestAdapterTwo 传数据

package com.zhh.android.test2;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;

import com.zhh.android.R;

import java.util.List;

/**
 * 用来测试适配器
 */
public class TestAdapterTwo extends BaseAdapter {
    private Context context;
    private List list;

    public TestAdapterTwo(Context context, List list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }



    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(
                    R.layout.item_layout, null);
            holder = new ViewHolder();
            holder.button = (Button) convertView.findViewById(R.id.button);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.button.setText("two"+list.get(position));

        // 按钮点击事件
        holder.button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // 把数据传给 Activity
                if (iJieKouArgs != null) {
                    iJieKouArgs.setCan(position + "");
                }
            }
        });

        return convertView;
    }

    /**
     * 接口回调
     */
    IJieKouArgs iJieKouArgs;
    public void setMethod(IJieKouArgs iJieKouArgs) {
        this.iJieKouArgs = iJieKouArgs;
    }




    class ViewHolder {
        Button button;
    }



}

 

接收数据:
MainActivityTwo 中实现接口 IJieKouArgs,在setCan(str) 中接收数据,注意调 testAdapterTwo.setMethod(this);

代码如下:

package com.zhh.android.test2;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.ListView;
import android.widget.Toast;

import com.zhh.android.R;

import java.util.ArrayList;
import java.util.List;

public class MainActivityTwo extends Activity implements IJieKouArgs{

    private ListView listView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        listView = findViewById(R.id.listView);

        // 绑定适配器
        TestAdapterTwo testAdapterTwo = new TestAdapterTwo(this,getData());
        listView.setAdapter(testAdapterTwo);

        // 接收适配传来的参数
        testAdapterTwo.setMethod(this);
    }

    /**
     * 造一组假数据
     */
    private List getData(){
        List list = new ArrayList<>();
        for(int i = 0; i <20 ; i++) {
            list.add("zhh"+i);
        }
        return list;
    }
//    接收数据
    @Override
    public void setCan(String str) {
        Toast.makeText(MainActivityTwo.this, "接收数据"+str, Toast.LENGTH_SHORT).show();
    }
}

我的另一篇 接口传参的文章

https://blog.csdn.net/zhaihaohao1/article/details/70236490

 

 

你可能感兴趣的:(数据传递,Java基础)