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

Posted 千里之外kb

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android中listView中的button控件获取item的值相关的知识,希望对你有一定的参考价值。

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的值的主要内容,如果未能解决你的问题,请参考以下文章

android中的Listview设置刷新时,Item是当前的Item,而不是第一个Item

Android:如何在 ListView 的列表项中为 Button 设置 onClick 事件

请问在android的listView中怎么动态加入radioButton和Button按钮?

Android实战简易教程-第十五枪(实现ListView中Button点击事件监听)

Android: ListView与Button的共存问题解决

ListView Android中的EditText和按钮