如何在listview中为textview项提供id?

时间:2015-07-13 08:47:08

标签: android

如果将文本视图项目放在Listview中,如何为其提供ID?

             adapter = new SimpleCursorAdapter(this, R.layout.row, c,
                new String[] { "body", "address" }, new int[] {
                        R.id.lblMsg, R.id.lblNumber });

我应该使用lblMsglblNumber做什么?

2 个答案:

答案 0 :(得分:0)

你应该声明这样的身份

 <TextView
    android:id="@+id/yourIdHere"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:layout_weight="1"
    android:gravity="center"
    android:text="yourTextHere" 

在每个元素的xml声明id和 定义

Textview tv; //就在onCreate之前。

之后在你的java代码中转到onCreate&amp;定义如下:

    tv = (TextView) findViewById(R.id.yourIdHere);

答案 1 :(得分:0)

首先为listview行创建xml文件。请参阅:

XML文件:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    <ImageView 
        android:id="@+id/ivImage"
        android:layout_height="80dp"
        android:layout_width="80dp"
        android:layout_alignParentLeft="true"/>
    <TextView 
        android:id="@+id/tvMovieName"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:layout_toRightOf="@+id/ivImage"/>
    <TextView 
        android:id="@+id/tvRating"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:layout_toRightOf="@+id/ivImage"
        android:layout_below="@+id/tvMovieName"/>    

    <TextView 
        android:id="@+id/tvGenre"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:layout_toRightOf="@+id/ivImage"
        android:layout_below="@+id/tvRating"/>

    <CheckBox 
        android:id="@+id/cbSelect"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:layout_above="@+id/tvYear"
        android:layout_alignParentRight="true"/>

    <TextView 
        android:id="@+id/tvYear"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:layout_below="@+id/tvGenre"
        android:layout_alignParentRight="true"/>
</RelativeLayout>

适配器代码:

package com.example.webservicecall;

import java.util.Arrays;
import java.util.List;

import org.w3c.dom.Text;

import com.squareup.picasso.Picasso;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageView;
import android.widget.TextView;

public class MovieAdapter extends ArrayAdapter<MovieVO>{

    Context context;
    List<MovieVO> movieList;
    public MovieAdapter(Context context, int resource, List<MovieVO> objects) {
        super(context, resource, objects);
        this.context = context;
        movieList = objects;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.list_row, null);
        TextView tvMovieName = (TextView) convertView.findViewById(R.id.tvMovieName);
        TextView tvRating = (TextView) convertView.findViewById(R.id.tvRating);
        TextView tvGenre = (TextView) convertView.findViewById(R.id.tvGenre);
        TextView tvYear = (TextView) convertView.findViewById(R.id.tvYear);
        ImageView ivImage = (ImageView) convertView.findViewById(R.id.ivImage);
        CheckBox cbSelect = (CheckBox) convertView.findViewById(R.id.cbSelect);

        if(movieList.get(position).isSelected())
        {
            cbSelect.setChecked(true);
        }
        else
        {
            cbSelect.setChecked(false);
        }

        cbSelect.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked)
                {
                    movieList.get(position).setSelected(true);
                }
                else
                {
                    movieList.get(position).setSelected(false);
                }
            }
        });

        tvMovieName.setText(movieList.get(position).getName());
        tvRating.setText(movieList.get(position).getRating());
        String genreStr = "";
//        for (String str : movieList.get(position).getGenre()) {
//            genreStr += str + ", ";
//        }
        tvGenre.setText(Arrays.toString(movieList.get(position).getGenre()).replace("[", "").replace("]", ""));
        tvYear.setText(movieList.get(position).getYear());
        Picasso.with(context).load(movieList.get(position).getImageUrl()).into(ivImage);
        return convertView;
    }
}

活动代码:

MovieAdapter adptr = new MovieAdapter(MainActivity.this, 0, movieList);
lvMovie.setAdapter(adptr);

这里movieList是List。 lvMovie是列表视图。 MovieAdapter是适配器。

结果是:

Row create this UI in ListView