Android如何从baseadapter进行onclick实际上无法弄明白

时间:2015-09-15 16:35:51

标签: android listview onclick baseadapter

我有一个listview,它绑定到一个实现自定义适配器的baseadapter。我从数据库中获取数据,因此当用户单击该textview时,listview行中的每个Textview都有不同的值我想在Toast中显示该值。

这是我的customAdadapter.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="match_parent"
    android:background="#FFFFFF"
    android:layout_marginRight="5dp"
    android:layout_marginLeft="5dp"
    android:layout_height="match_parent">

    <TextView
        android:layout_width="match_parent"
      android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:clickable="true"
        android:textSize="10sp"
        android:text="# votes"
        android:id="@+id/votes" />

</RelativeLayout>

这是我的基础适配器

public class LocalFeed_CustomView extends BaseAdapter {

    JSONObject names;
    Context ctx;
    LayoutInflater myiflater;



    public LocalFeed_CustomView(JSONObject arr,Context c) {
        ctx = c;
        names = arr;
        myiflater = (LayoutInflater)c.getSystemService(c.LAYOUT_INFLATER_SERVICE);

    }


    @Override
    public int getCount() {
        try {
            JSONArray jaLocalstreams = names.getJSONArray("localstreams");

            return jaLocalstreams.length();
        } catch (Exception e) {
            Toast.makeText(ctx,"Error: Please try again",Toast.LENGTH_LONG).show();
            return names.length();
        }


    }




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

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

    @Override
    public View getView(int position, final View convertView, ViewGroup parent) {

        View v = convertView;
        final CompleteListViewHolder viewHolder;
        try {
           if(convertView==null) {
                LayoutInflater li = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                v = li.inflate(R.layout.customadapter, null);
                viewHolder = new CompleteListViewHolder(v);
                v.setTag(viewHolder);

            }

            else {
                viewHolder = (CompleteListViewHolder) v.getTag();


            }


            JSONArray jaLocalstreams = names.getJSONArray("localstreams");
            final JSONObject jsonObject = jaLocalstreams.getJSONObject(position);


            viewHolder.votes.setText(jsonObject.getString("votes")+ " Votes");
            viewHolder.idnumber.setText(jsonObject.getInt("id"));


            viewHolder.votes.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    LocalFeed dd= new LocalFeed();
                    String vote_id= String.valueOf(viewHolder.idnumber.getText());
  // this toast does not fire
                    Toast.makeText(ctx,"hello"+ vote_id,Toast.LENGTH_SHORT).show();
                }
            });



            return v;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return v;


    }
}
class CompleteListViewHolder {
    public TextView votes,idnumber;

    public CompleteListViewHolder(View base) {

        votes=(TextView)base.findViewById(R.id.votes);
       idnumber=(TextView)base.findViewById(R.id.idnumber);
    }
}

1 个答案:

答案 0 :(得分:0)

请将视图分配给视图中的运行时标识符,如下所示

变化

 if(convertView==null) {
                LayoutInflater li = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                v = li.inflate(R.layout.customadapter, null);
                viewHolder = new CompleteListViewHolder(v);
                v.setTag(viewHolder);

            }

 if(convertView==null) {
                LayoutInflater li = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                v = li.inflate(R.layout.customadapter, null);
                viewHolder = new CompleteListViewHolder(v);
                viewholder.votes = v.findViewById(R.id.votes);
                viewholder.idnumber = v.findViewById(R.id.idnumber);
                v.setTag(viewHolder);

            }

还要确保TextView与填充和边距的对齐是正确的,以便它可以正确捕获触摸事件