码迷,mamicode.com
首页 > 移动开发 > 详细

Android中listView中的button控件获取item的值

时间:2020-03-10 21:29:50      阅读:90      评论:0      收藏:0      [点我收藏+]

标签:esc   finally   elb   systems   stream   适配器   cache   content   getc   

1,适配器类实现OnClickListener

2,接着创建

public interface InnerItemOnclickListener {
void itemClick(View v);
}

public void setOnInnerItemOnClickListener(InnerItemOnclickListener listener){
this.mListener=listener;
}

3,之后在activity中实现

implements teacherCourseAdapter.InnerItemOnclickListener,
        AdapterView.OnItemClickListener

 

4,调用适配器中的方法(已经能够获取到item的下标值了,相信你进行setAdapter的时候,也就是列表渲染的时候,已经获得了list数据,你只需t_course.get(position).getName()具体的东东就ok)

   @Override
    public void itemClick(View v) {

        int position;
        position = (Integer) v.getTag();
        switch (v.getId()) {
            case R.id.btn_mod:{
                Log.e("--修改-->", position + "======"+t_course.get(position).getName());
                t_course.get(position).getName();
            }
                break;
            case R.id.btn_del:
                Log.e("--删除-->", position + "");
                break;
            default:
                break;
        }

    }

5,适配器代码

package com.example.fitness_app.MyAdapter;

import android.content.Context;
import android.util.Log;
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 android.widget.Toast;

import com.example.fitness_app.R;
import com.example.fitness_app.bean.FitnessCourse;

import java.util.List;

public class teacherCourseAdapter extends BaseAdapter implements View.OnClickListener {
    private Context context;
    private List<FitnessCourse> courseList;
    private LayoutInflater inflater;
    private InnerItemOnclickListener mListener;

    public teacherCourseAdapter(Context context, List<FitnessCourse> courseList) {
        this.context = context;
        this.courseList = courseList;
        inflater =  (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

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

    @Override
    public Object getItem(int i) {
        return courseList.get(i);
    }

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

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        InnerViewHolder innerViewHolder = new InnerViewHolder();
        view = inflater.inflate(R.layout.teacher_course_item,null);
        innerViewHolder.delButton = view.findViewById(R.id.btn_del);
        innerViewHolder.modButton = view.findViewById(R.id.btn_mod);
        innerViewHolder.courseday = view.findViewById(R.id.tv_courseday);
        innerViewHolder.description = view.findViewById(R.id.tv_description);
        innerViewHolder.name = view.findViewById(R.id.tv_name);
//给每个按钮设置监听器以及下标
        view.setTag(innerViewHolder);
        innerViewHolder.modButton.setOnClickListener(this);
        innerViewHolder.delButton.setOnClickListener(this);
        innerViewHolder.modButton.setTag(i);
        innerViewHolder.delButton.setTag(i);



        //根据业务处理
        innerViewHolder.courseday.setText(courseList.get(i).getCourseDay().toString());
        innerViewHolder.description.setText(courseList.get(i).getDescription().toString());
        innerViewHolder.name.setText(courseList.get(i).getName().toString());

//        innerViewHolder.delButton.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                Toast.makeText(context,"删除",Toast.LENGTH_LONG).show();
//            }
//        });
//
//        innerViewHolder.modButton.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                Log.i("修改",courseList.get(itemtag).getDescription().toString());
//            }
//        });

        return view;

    }

    @Override
    public void onClick(View v) {
        mListener.itemClick(v);
    }

    public interface InnerItemOnclickListener {
        void itemClick(View v);
    }

    public void setOnInnerItemOnClickListener(InnerItemOnclickListener listener){
        this.mListener=listener;
    }

    class InnerViewHolder{
        TextView courseday,name,description;
        Button modButton,delButton;
    }
}

2,activity代码

package com.example.fitness_app.web.teacher;

import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.example.fitness_app.MyAdapter.teacherCourseAdapter;
import com.example.fitness_app.MyUtil.MyJson;
import com.example.fitness_app.R;
import com.example.fitness_app.bean.FitnessCourse;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;

public class teacher_courseActivity extends AppCompatActivity implements teacherCourseAdapter.InnerItemOnclickListener,
        AdapterView.OnItemClickListener {
    private  String tag = "courseList";
    private MyJson myJson = null;
    private String baseurl = "http://192.168.1.4:7777/";
    private  String teacherId = null;
    private  List<FitnessCourse> t_course = null;
    private Message message = new Message();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_teacher_course);



//        ListView view = findViewById(R.id.teacher_course_view);
//        view.setOnItemClickListener(new AdapterView.OnItemClickListener() {
//            @Override
//            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//                Object item = parent.getItemAtPosition(position);
//                FitnessCourse f = (FitnessCourse) item;
//                Log.i("item内容", item.toString());
//            }
//        });

    }

    @Override
    protected void onStart() {

        super.onStart();

        Intent intent = getIntent();
         teacherId = intent.getStringExtra("teacherId");

//        myJson = new MyJson();
//        Object o = myJson.JsonToObject(user);
//        Log.e(tag, "json格式--->" + o);
//        try {
//            Object id = ((JSONObject) o).get("id");
//            teacherid = Long.parseLong(id.toString());
//        } catch (JSONException e) {
//            e.printStackTrace();
//        }

        //处理消息
        final Handler handler=new Handler(){
            public void handleMessage(Message msg){
                if (msg.obj!=null){
                    ListView view = findViewById(R.id.teacher_course_view);
                    teacherCourseAdapter teacherCourseAdapter = new teacherCourseAdapter(teacher_courseActivity.this, t_course);
                    teacherCourseAdapter.setOnInnerItemOnClickListener(teacher_courseActivity.this);
                    view.setAdapter(teacherCourseAdapter);
                    view.setOnItemClickListener(teacher_courseActivity.this);
                }
            }
        };

        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    HttpURLConnection connection = null;
                    try {
                        URL url = new URL(baseurl + "findTeacherCourseById?"+"id="+teacherId);
                        //开启连接
                        connection = (HttpURLConnection) url.openConnection();
                        connection.setConnectTimeout(5 * 1000);
                        connection.setReadTimeout(5 * 1000);
                        connection.setUseCaches(true);
                        connection.setRequestMethod("GET");
                        //设置请求中的媒体类型信息。
//                    connection.setRequestProperty("Content-Type","application/json");
                        // 开始连接
                        connection.connect();

                        // 判断请求是否成功
                        if (connection.getResponseCode() == 200) {
                            // 获取返回的数据
                            InputStream is = connection.getInputStream();
                            BufferedReader reader = new BufferedReader(new InputStreamReader(is));
                            String result = reader.readLine();
                             myJson = new MyJson();
                             t_course = teacher_courseActivity.this.myJson.JsonToListTeacherCourse(result);
                            for (int i = 0; i < t_course.size(); i++) {
                                Log.e(tag, "Get方式请求成功,courselist--->" + t_course.get(i).getName());

                            }
                            message.obj = t_course;
                            handler.sendMessage(message);
//                            List<FitnessCourse> courses = myJson.JsonToListTeacherCourse(t_course);

//                            Intent intent = new Intent();
//                            intent.setClass(TeacherActivity.this,teacher_courseActivity.class);
//                            intent.putExtra("t_course",result);
//                            startActivity(intent);
//                        JSONObject jsonObject = new JSONObject(result);
//                        Log.e(tag, "json格式--->" + jsonObject);
                        } else {
                            Log.e(tag, "Get方式请求失败");

                        }
                    } finally {
                        // 关闭连接
                        if (connection != null)
                            connection.disconnect();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();

    }

//没啥子用处
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Log.e("整体item----->", position + "");
        Object item = parent.getItemAtPosition(position);
                FitnessCourse f = (FitnessCourse) item;
                Log.i("item内容", item.toString());
    }

//通过它获取 @Override
public void itemClick(View v) { int position; position = (Integer) v.getTag(); switch (v.getId()) { case R.id.btn_mod:{ Log.e("--修改-->", position + "======"+t_course.get(position).getName()); t_course.get(position).getName(); } break; case R.id.btn_del: Log.e("--删除-->", position + ""); break; default: break; } } //添加 public void addcourse(View v){ } }

 

链接:https://blog.csdn.net/chaoyu168/article/details/55253877

Android中listView中的button控件获取item的值

标签:esc   finally   elb   systems   stream   适配器   cache   content   getc   

原文地址:https://www.cnblogs.com/wskb/p/12458473.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!