获取列表视图项目上的名称

时间:2014-10-01 11:13:36

标签: android listview

我有从json解析的(id,name,image)列表视图:当点击图像时我会得到名字,我会得到名字但不总是有时会得到错误的名字。

    listView = (ListView) findViewById(R.id.list);

    feedItems = new ArrayList<FeedItem>();

    listAdapter = new FeedListAdapter(this, feedItems);
    listView.setAdapter(listAdapter);
    listView.setOnScrollListener(this);

适配器代码;

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

    if (inflater == null)
        inflater = (LayoutInflater) activity
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    if (convertView == null)
        convertView = inflater.inflate(R.layout.feed_item, null);

    if (imageLoader == null)
        imageLoader = AppController.getInstance().getImageLoader();

    TextView name = (TextView) convertView.findViewById(R.id.name);
    TextView timestamp = (TextView) convertView
            .findViewById(R.id.timestamp);
    TextView statusMsg = (TextView) convertView
            .findViewById(R.id.txtStatusMsg);
    TextView url = (TextView) convertView.findViewById(R.id.txtUrl);
    NetworkImageView profilePic = (NetworkImageView) convertView
            .findViewById(R.id.profilePic);
    FeedImageView feedImageView = (FeedImageView) convertView
            .findViewById(R.id.feedImage1);

    FeedItem item = feedItems.get(position);

    name.setText(item.getName());
到目前为止

代码

   public void clicked(View view) {

    TextView name = (TextView) findViewById(R.id.name);
    Intent i = new Intent(MainActivity.this, GalleryUrlActivity.class);
    String message = name.getText().toString();
    i.putExtra("message", message);
    startActivity(i);

我会帮助你......

2 个答案:

答案 0 :(得分:1)

您可以在图像上单击设置onclicklistener,然后在单击图像时获取名称。您可以在适配器类getView()中设置以下代码。

  final int pos=position;
    feedImageView.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
          Log.v("name :: ", feedItems.get(pos). getName());

          Intent i = new Intent(context, GalleryUrlActivity.class);
          String message = feedItems.get(pos). getName();
          i.putExtra("message", message);
          context.startActivity(i);
        }
    });

答案 1 :(得分:0)

此代码将在列表视图的项目上设置单击侦听器。它将使用您用于适配器的相同阵列。

listView.setOnItemClickListener(new AdapterView.OnItemClickListener()
    {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l)
        {
           FeedItem myItem = feedItems.get(i);
           Intent intent = new Intent(MainActivity.this, GalleryUrlActivity.class);
           String message = myItem.getName(); //change this according to your feeditem
           intent.putExtra("message", message);
           startActivity(intent );
        }
    });