【问题标题】:Android: Unable to setText in Custom ListViewAndroid:无法在自定义 ListView 中设置文本
【发布时间】:2016-01-28 02:15:13
【问题描述】:

我创建了一个带有 2 个 ImageView 和一个 TextView 的自定义 ListView。

在Adapter中,我在TextView中做setText,但是在app中是看不到的。

这是我的清单:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffffff">

<View
    android:layout_width="fill_parent"
    android:layout_height="5dp"
    android:background="#289FA5"/>
<RelativeLayout
    android:layout_marginTop="7dp"
    android:layout_marginLeft="7dp"
    android:layout_marginBottom="15dp"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="#289FA5">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Popular products"
        android:textSize="23sp"
        android:padding="7dp"/>
</RelativeLayout>
<HorizontalScrollView
    android:layout_width="match_parent"
    android:layout_height="120dp">
    <LinearLayout
        android:id="@+id/pop_pro_men"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
    </LinearLayout>
</HorizontalScrollView>
<RelativeLayout
    android:layout_marginTop="15dp"
    android:layout_marginLeft="7dp"
    android:layout_marginBottom="15dp"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="#289FA5">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Catagories"
        android:textSize="23sp"
        android:padding="7dp"/>
</RelativeLayout>
<ListView
    android:id="@+id/cata_men"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"></ListView>

这是我的自定义列表:

<LinearLayout 
android:id="@+id/custom_list"
android:layout_width="match_parent"
android:layout_height="130dp"
android:layout_marginTop="10dp"
android:orientation="horizontal"
android:background="#ffffff">


<ImageView
    android:layout_gravity="left"
    android:id="@+id/imageView2"
    android:layout_height="130dp"
    android:layout_marginLeft="7dp"
    android:layout_width="120dp"
    android:src="@drawable/clothing_men"/>

<TextView
    android:id="@+id/cata_name"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Clothing"
    android:layout_marginTop="35dp"
    android:textStyle="bold"
    android:textSize="27sp"
    android:padding="10dp"
    android:layout_weight="1"/>
<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/forward_arrow"
    android:layout_marginTop="42dp"/>

这是我的适配器的 getView 方法:

public View getView(final int position, View convertView, ViewGroup parent) {
    final Holder holder = new Holder();
    View rowView;

    rowView = inflater.inflate(R.layout.custom_subcatagory_list, null,true);

    TextView sub_catagory= (TextView) rowView.findViewById(R.id.cata_name);
    ImageView pic = (ImageView) rowView.findViewById(R.id.imageView2);

    sub_catagory.setText(catagory[position]);
    pic.setImageResource(image[position]);
    Log.d("Strings========", sub_catagory.getText().toString());

    rowView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Toast.makeText(context, "You Clicked " + catagory[position], Toast.LENGTH_LONG).show();
        }
    });
    return rowView;
}

请帮忙,因为我无法找到其中的问题。

【问题讨论】:

  • 调试并检查目录[位置]是否为空。
  • @Code_Yoga,我检查了,日志中的文本可用,clickListener 显示 Toast with Text...
  • 好的,你在 Adapter 类的 getCount() 方法中返回了什么?
  • @Code_Yoga,返回 catagory.length;
  • 能否请您发布完整的代码?

标签: android android-layout android-listview


【解决方案1】:

我现在得到了答案。我不知道我的 textColor 是如何设置为白色的,因为我没有提到它。所以,把颜色换成别的颜色就行了。这浪费了我一整天。

很抱歉打扰大家。

【讨论】:

    【解决方案2】:

    TextView 中删除您的 layout_weight 属性。如果删除 sub_catagory.setText(category[position]);运行应用时会出现“服装”吗?

    【讨论】:

    • 删除 layout_weight 不起作用,并且在删除 sub_catagory.setText(catagory[position]) 时,“服装”没有出现,但它出现在我的 Logcat 中...
    • 那一定是布局问题。
    • 对不起,我的 textColor 是白色的(我没有设置)。现在它正在工作
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-16
    • 1970-01-01
    • 2021-04-24
    • 1970-01-01
    • 2016-07-19
    相关资源
    最近更新 更多