在ListView中设置TextView可见(或消失)

时间:2014-02-05 15:46:18

标签: android listview textview visible

我想在我的ListView中设置一个textview,取决于另一个变量。但是当我尝试

TextView sdate;
TextView stime;
sdate= (TextView) findViewById(R.id.date);
stime= (TextView) findViewById(R.id.time);

好像在布局中找不到2?

从我收集的内容中,需要在findViewbyId()前面有一些东西,但我不确定是什么。列表中的项目布局由list_item.xml完成​​。处理所有这些的代码是:

                ListAdapter adapter;
                if(add.equals("Event")){
                    stime.setVisibility(View.VISIBLE);
                    sdate.setVisibility(View.VISIBLE);
                    adapter = new SimpleAdapter(
                            AllSpotsActivity.this, spotsList,
                            R.layout.list_item, new String[] { TAG_PID,
                                    TAG_NAME, "sdistance","dateof","timeof" }, new int[] {
                                    R.id.pid, R.id.name, R.id.distance, R.id.date, R.id.time});
                }else{
                adapter = new SimpleAdapter(
                        AllSpotsActivity.this, spotsList,
                        R.layout.list_item, new String[] { TAG_PID,
                                TAG_NAME, "sdistance" }, new int[] {
                                R.id.pid, R.id.name, R.id.distance });
                }

提前谢谢你,

泰勒

编辑:

<RelativeLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
android:background="@drawable/gradient_background" 
>


<!-- Product id (pid) - will be HIDDEN - used to pass to other activity -->
<TextView
    android:id="@+id/pid"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:visibility="gone" />

<!-- Name Label -->
<TextView
    android:id="@+id/name"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:paddingTop="6dip"
    android:paddingLeft="6dip"
    android:textSize="17dip"
    android:textStyle="bold" />
        <!-- Name Label -->
<TextView
    android:id="@+id/distance"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:paddingRight="6dip"/>
    <TextView
    android:id="@+id/date"
    android:layout_below="@id/name"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:paddingLeft="12dip"
    android:visibility="gone"/>
    <TextView
    android:id="@+id/time"
    android:layout_below="@id/name"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:paddingRight="6dip"
    android:visibility="gone"/>


</RelativeLayout>

编辑2: 我也尝试过使用lv.findViewById(),因为lv = getListView();但那也不起作用。

2 个答案:

答案 0 :(得分:2)

我认为你应该在每一行的视图上调用findViewById()。例如,如果您从适配器的getView()方法调用该方法,则应使用convertView.findViewById

答案 1 :(得分:0)

如果您希望只有一个简单的TextView作为ListView中每个项目的布局,那么您需要做的是创建一个扩展ArrayAdapter的自定义适配器。在自定义适配器的getView()方法内部,您将可以访问自定义xml布局(constructor of the ArrayAdapter的一个参数是要为每个项目设置的布局文件的资源ID。列表[例如,R.layout.your_custom_list_item])。从该方法的内部,您可以访问新视图或回收视图,您可以在其中充气您的自定义布局。

您在getView()方法中调用的LayoutInflater.inflate()方法将返回一个视图,您可以调用findViewById()以访问您要与之交互的自定义​​布局中的所有视图。您可以根据传递到适配器的数据使视图可见或不可见(适配器构造函数的第三个参数是列表或包含您要使用的数据的适配器填充视图)。我强烈建议您观看I/O video on ListViews以获得更完整的解释。我还在下面提供了一些自定义适配器的示例代码。

public class BookingSearchResultsAdapter extends ArrayAdapter<SearchResult> {

private static final String TAG = BookingSearchResultsAdapter.class.getName();

private int mLayoutResourceId;  
private Context mContext;
private List<SearchResult> mResults = null;

public BookingSearchResultsAdapter(Context context, int resource, List<SearchResult> results) {
    super(context, resource, results);

    mContext = context;
    mLayoutResourceId = resource;
    mResults = results;
}

/**
 * @return A view to display search result
 */
@Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View row = convertView;
        SearchResultVendorViewHolder holder = null;

        // if there is no recycled view available
        if(row == null) {
            Log.d(TAG, "No recyclable custom view found. New view created.");
        // get a new search result view
            LayoutInflater inflater = ((Activity) mContext).getLayoutInflater();
            row = inflater.inflate(mLayoutResourceId, parent, false);

            // store all static view information in a holder
            holder = new SearchResultVendorViewHolder();
            holder.imgIcon = (ImageView) row.findViewById(R.id.search_result_establishment_icon);
            holder.txtTitle = (TextView) row.findViewById(R.id.search_result_establishment_name);
            holder.txtDescription = (TextView) row.findViewById(R.id.search_result_establishment_description);

            // attach the holder to the view so that it can be recycled
            row.setTag(holder);
        }
        else {
            Log.d(TAG, "View recycled");
        // get the recycled view (stored in tag)
            holder = (SearchResultVendorViewHolder)row.getTag();
        }

        // initialize data in custom search result view
        SearchResult result = (SearchResult) mResults.get(position);
        holder.txtTitle.setText(result.getEstablishmentName());
        holder.imgIcon.setImageResource(result.getImageId());
        holder.txtDescription.setText(result.getDescription());
        Log.d(TAG, "Custom search view data initialized");
        return row;
    }


    static class SearchResultVendorViewHolder {
        ImageView imgIcon;
        TextView txtTitle;
        TextView txtDescription;
    }
}