如何在android中获取listview特定项的id?

时间:2014-11-24 11:27:46

标签: android listview baseadapter

我正在使用Listview并设置多项,每个项目都有赞,分享和评论选项。        实际上我已经获取了项目ID以及setText的{​​{1}},其成功。但是无法在特定项目的Like Button Click事件上获得Item Id。        如何在TextView点击活动上的Listview中为每件商品获取商品ID?

MyAdapter类的演示:

Button

enter image description here

xml文件是,

public class Adapter1 extends BaseAdapter implements OnClickListener {
    public ArrayList<HashMap<String, String>> arr = null;
    Context context = null;
    LayoutInflater layoutInflater = null;
    HashMap<String, String> getData = new HashMap<String, String>();

    String urlLike = null;
    TextView strId = null;

    public NewsFeedAdapter(Context context,
            ArrayList<HashMap<String, String>> arr) {
        this.context = context;
        this.arr= arr;
        layoutInflater = LayoutInflater.from(context);
    }

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

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

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if (convertView == null) {
            convertView = layoutInflater.inflate(
                    R.layout.my_list_item, null);
            /** initialize ViewHolder */
            viewHolder = new ViewHolder();

            /** Initialize Widgets */
            /** Imageview */

            viewHolder.imgLike = (ImageView) convertView
                    .findViewById(R.id.imgLike);
            viewHolder.imgLike.setOnClickListener(this);

            viewHolder.imgComments = (ImageView) convertView
                    .findViewById(R.id.imgComments);
            viewHolder.imgComments.setOnClickListener(this);

            viewHolder.imgShare = (ImageView) convertView
                    .findViewById(R.id.imgShare);
            viewHolder.imgShare.setOnClickListener(this);

            /** TextView */
            viewHolder.txtId = (TextView) convertView
                    .findViewById(R.id.txtId);

            viewHolder.txtDescription = (TextView) convertView
                    .findViewById(R.id.txtDescription);

            getData = arr.get(position);
            viewHolder.txtId.setText(getData
                    .get(Fragment1.TAG_ID));

            viewHolder.txtDescription.setText(getData
                    .get(Fragment1.TAG_MESSAGE));

            convertView.setTag(viewHolder);

        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

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

            }
        });

        return convertView;
    }

    /** ViewHolder Class */
    public class ViewHolder {
        ImageView imgLike = null, imgComments = null,
                imgShare = null;
        TextView txtId = null,txtDescription = null;
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
        case R.id.imgLike:
            strId = ((TextView) view
                    .findViewById(R.id.txtId));
            System.out.println("On Click====="
                    + strId.getText().toString());

            urlLike = "http://www.example.com?id="
                    + strId.getText().toString() + "&user_id="
                    + myDetail.getUserId();

            new sendLikeData().execute();

            break;
        case R.id.imgComments:

            break;
        case R.id.imgShare:

            break;
        default:
            break;
        }
    }

    public class sendLikeData extends AsyncTask<Void, Void, Void> {
        protected void onPreExecute() {
            super.onPreExecute();
        }

        @Override
        protected Void doInBackground(Void... arg0) {
            ServiceHandler sh = new ServiceHandler();
            String jsonStr = sh.makeServiceCall(urlLike, ServiceHandler.GET);
            Log.d("Response : Like", ">" + jsonStr);

            return null;
        }

        protected void onPostExecute(Void result) {
            super.onPostExecute(result);
        };
    }
}
OutPut is, NullPointer Exception in System.out.println("On Click====="
                        + strId.getText().toString());.

Logcat是,

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/masterLayout"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_margin="@dimen/five_dp" >

    <TextView
        android:id="@+id/txtDescription"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_marginLeft="@dimen/ten_dp"
        android:layout_marginRight="@dimen/ten_dp"
        android:layout_marginTop="@dimen/ten_dp" />

    <ImageView
        android:id="@+id/imgImage"
        android:layout_width="fill_parent"
        android:layout_height="150dp"
        android:layout_below="@+id/txtDescription"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="@dimen/ten_dp"
        android:layout_marginLeft="@dimen/ten_dp"
        android:layout_marginRight="@dimen/ten_dp"
        android:layout_marginTop="@dimen/ten_dp"
        android:background="@null"
        android:contentDescription="@string/content_description"
        android:src="@drawable/ic_launcher" />

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/imgImage"
        android:background="@drawable/strip"
        android:baselineAligned="false"
        android:gravity="center_vertical"
        android:orientation="horizontal" >

        <RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1" >

            <ImageView
                android:id="@+id/imgLike"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerHorizontal="true"
                android:layout_centerInParent="true"
                android:contentDescription="@string/content_description"
                android:src="@drawable/like" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignTop="@+id/imgLike"
                android:layout_toRightOf="@+id/imgLike"
                android:background="@drawable/getbg"
                android:gravity="center"
                android:padding="@dimen/two_dp"
                android:textColor="@color/white"
                android:textSize="@dimen/ten_sp" />
        </RelativeLayout>

        <RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1" >

            <ImageView
                android:id="@+id/imgComments"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerHorizontal="true"
                android:layout_centerInParent="true"
                android:contentDescription="@string/content_description"
                android:src="@drawable/comments" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_toRightOf="@+id/imgComments"
                android:background="@drawable/getbg"
                android:gravity="center"
                android:padding="@dimen/two_dp"
                android:text="+1"
                android:textColor="@android:color/white"
                android:textSize="@dimen/ten_sp" />
        </RelativeLayout>

        <RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1" >

            <ImageView
                android:id="@+id/imgShare"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerHorizontal="true"
                android:layout_centerInParent="true"
                android:contentDescription="@string/content_description"
                android:src="@drawable/share" />
        </RelativeLayout>
    </LinearLayout>

    <TextView
        android:id="@+id/txtId"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_margin="@dimen/ten_dp"
        android:text="id"
        android:textSize="20dp" />

</RelativeLayout>

6 个答案:

答案 0 :(得分:2)

尝试使用以下代码:

public class Adapter1 extends BaseAdapter implements OnClickListener {
    public ArrayList<HashMap<String, String>> arr = null;
    Context context = null;
    LayoutInflater layoutInflater = null;
    HashMap<String, String> getData = new HashMap<String, String>();

    String urlLike = null;
    TextView strId = null;

    public NewsFeedAdapter(Context context,
            ArrayList<HashMap<String, String>> arr) {
        this.context = context;
        this.arr= arr;
        layoutInflater = LayoutInflater.from(context);
    }

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

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

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if (convertView == null) {
            convertView = layoutInflater.inflate(
                    R.layout.my_list_item, null);
            /** initialize ViewHolder */
            viewHolder = new ViewHolder();

            /** Initialize Widgets */
            /** Imageview */

            viewHolder.imgLike = (ImageView) convertView
                    .findViewById(R.id.imgLike);


            viewHolder.imgComments = (ImageView) convertView
                    .findViewById(R.id.imgComments);
            viewHolder.imgComments.setOnClickListener(this);

            viewHolder.imgShare = (ImageView) convertView
                    .findViewById(R.id.imgShare);
            viewHolder.imgShare.setOnClickListener(this);

            /** TextView */
            viewHolder.txtId = (TextView) convertView
                    .findViewById(R.id.txtId);

            viewHolder.txtDescription = (TextView) convertView
                    .findViewById(R.id.txtDescription);

            getData = arr.get(position);
            viewHolder.txtId.setText(getData
                    .get(Fragment1.TAG_ID));

            viewHolder.txtDescription.setText(getData
                    .get(Fragment1.TAG_MESSAGE));

            convertView.setTag(viewHolder);

        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

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

            }
        });

         viewHolder.imgLike.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {               
                strId=arr.get(position).getId();//or whatever you want to do do here.
            }
        });

        return convertView;
    }

    /** ViewHolder Class */
    public class ViewHolder {
        ImageView imgLike = null, imgComments = null,
                imgShare = null;
        TextView txtId = null,txtDescription = null;
    }
}

答案 1 :(得分:1)

您需要在getView()

中初始化strId

这里在下面的代码中,它无法根据&#34; view&#34;来初始化它。对象,因此你得到nullpointer。

case R.id.imgLike:
    strId = ((TextView) view.findViewById(R.id.txtId));
如果您觉得答案有用,

标记为已解决。

答案 2 :(得分:1)

试试这个:

strId = (TextView) view.getParent().findViewById(R.id.txtId);

这将解决您的崩溃问题,但您需要重新考虑您的设计。

答案 3 :(得分:1)

您需要做的是在不同的视图上设置差异点击监听器

viewHolder.txtDescription.setTag(R.id.txtDescription,position);
viewHolder.txtDescription.setOnClickListener(new OnClickListener() {    
    @Override
    public void onClick(View v) {
        int position_clicked=(Integer)v.getTag();               
    }
});

为您想要设置侦听器的每个视图重复

答案 4 :(得分:0)

尝试这样,

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder viewHolder = null;
    if (convertView == null) {
        convertView = layoutInflater.inflate(
                R.layout.my_list_item, null);
        /** initialize ViewHolder */
        viewHolder = new ViewHolder();

        /** Initialize Widgets */
        /** Imageview */

        viewHolder.imgLike = (ImageView) convertView
                .findViewById(R.id.imgLike);
        viewHolder.imgLike.setOnClickListener(this);
        viewHolder.imgLike.setTag(position);

        viewHolder.imgComments = (ImageView) convertView
                .findViewById(R.id.imgComments);
        viewHolder.imgComments.setOnClickListener(this);

        viewHolder.imgShare = (ImageView) convertView
                .findViewById(R.id.imgShare);
        viewHolder.imgShare.setOnClickListener(this);

        /** TextView */
        viewHolder.txtId = (TextView) convertView
                .findViewById(R.id.txtId);

        viewHolder.txtDescription = (TextView) convertView
                .findViewById(R.id.txtDescription);

        getData = arr.get(position);
        viewHolder.txtId.setText(getData
                .get(Fragment1.TAG_ID));

        viewHolder.txtDescription.setText(getData
                .get(Fragment1.TAG_MESSAGE));

        convertView.setTag(viewHolder);

    } else {
        viewHolder = (ViewHolder) convertView.getTag();
    }

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

        }
    });

    return convertView;
}

@Override
    public void onClick(View view) {
        switch (view.getId()) {
        case R.id.imgLike:

        View view = listview.getAdapter().getView(((ImageView)v).getTag(), null, listview);
            txtId = (TextView) view.findViewById(R.id.txtId);

            System.out.println("On Click====="
                    + strId.getText().toString());

            urlLike = "http://www.example.com?id="
                    + strId.getText().toString() + "&user_id="
                    + myDetail.getUserId();

            new sendLikeData().execute();

            break;
        case R.id.imgComments:

            break;
        case R.id.imgShare:

            break;
        default:
            break;
        }
    }

答案 5 :(得分:0)

您也可以在适配器类中使用onClickListener。

查看此示例代码

viewHolder.imgLike = (ImageView) convertView.findViewById(R.id.imgLike);
viewHolder.imgLike.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View arg0) {
        // TODO Auto-generated method stub
        String id = getData.get(Fragment1.TAG_ID);
    }
});