自定义列表视图没有显示出来的字符串 [英] Custom Listview is not showing up the string

查看:156
本文介绍了自定义列表视图没有显示出来的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

列表视图没有显示出相当的字符串列表只有自定义列表视图中的文本

我的字符串声明n个主

 的String []示例=新的String []
        {Android的简介,Android的设置/安装,Android的Hello World的,
                Android的布局/ Viewgroups,Android的活性放;生命周期,在Android中的Intent
        锆石};
ArrayAdapter<串GT; ExampleArrayAdapter =新CustomAdapter(这一点,例);    ListView控件ExampleListView =(ListView控件)findViewById(R.id.listView);
    ExampleListView.setAdapter(ExampleArrayAdapter);

这是我的customAdapter.java

 公共类CustomAdapter扩展ArrayAdapter {
私人LayoutInflater吹气;
公共CustomAdapter(活动活动的String []项){
    超(活动,R.layout.custom_layout,项目);
    吹气= activity.getWindow()getLayoutInflater()。
}@覆盖
公共查看getView(INT位置,查看convertView,父母的ViewGroup){
 随机RND =新的随机();   INT颜色= Color.argb(255,rnd.nextInt(256),rnd.nextInt(257),rnd.nextInt(258));  查看rowView = inflater.inflate(R.layout.custom_layout,父母,假);
    rowView.setBackgroundColor(颜色);    返回rowView;
}
}

下面是我的应用程序的自定义布局,我已经更新了...........
custom_layout.xml

 <?XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
机器人:方向=横向
机器人:layout_width =match_parent
机器人:layout_marginTop =70dp
机器人:layout_height =70dp
机器人:背景=@绘制/ blue_bg><的LinearLayout
    机器人:layout_marginLeft =20dp
    机器人:layout_width =345dp
    机器人:layout_height =70dp
    机器人:背景=@绘制/ white_bg>
    < ImageView的
        机器人:ID =@ + ID / imageView1
        机器人:layout_width =70dp
        机器人:layout_height =60dp
        机器人:SRC =@绘制/ ic_launcher
        机器人:layout_marginLeft =5DP
        机器人:layout_marginTop =5DP/>
    <的LinearLayout
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:方向=垂直
        机器人:layout_marginLeft =10dp
        机器人:重力=左>
        <的TextView
            机器人:ID =@ + ID / textView1
            机器人:layout_width =match_parent
            机器人:layout_height =WRAP_CONTENT
            机器人:文字=大文本
            机器人:layout_marginTop =10dp
            机器人:textAppearance =机器人:ATTR / textAppearanceLarge
            机器人:TEXTSIZE =19sp
            机器人:文字颜色=#000000
            机器人:文字样式=大胆/>
        <的TextView
            机器人:ID =@ + ID / textView2
            机器人:layout_width =match_parent
            机器人:layout_height =15dp
            机器人:文字=鸣叫正文这里
            机器人:textAppearance =机器人:ATTR / textAppearanceMedium
            机器人:layout_marginTop =5DP
            机器人:ellipsize =结束
            机器人:行=3
            机器人:文字颜色=#000000
            机器人:TEXTSIZE =14sp/>
    < / LinearLayout中>< / LinearLayout中>< / LinearLayout中>


解决方案

您必须声明你的CustomAdapter对象活动为:

  CustomAdapter ExampleArrayAdapter =新CustomAdapter(这一点,例);    ListView控件ExampleListView =(ListView控件)findViewById(R.id.listView);
    ExampleListView.setAdapter(ExampleArrayAdapter);

和您的适配器code应该是:

 公共类CustomAdapter扩展ArrayAdapter {
私人LayoutInflater吹气;
私有String []项目;
公共CustomAdapter(活动活动的String []项){
          超(活动,0项);
    吹气= activity.getWindow()getLayoutInflater()。
}@覆盖
公共查看getView(INT位置,查看convertView,父母的ViewGroup){
 随机RND =新的随机();   INT颜色= Color.argb(255,rnd.nextInt(256),rnd.nextInt(257),rnd.nextInt(258));  查看rowView = inflater.inflate(R.layout.custom_layout,父母,假);
    rowView.setBackgroundColor(颜色);
TextView的电视=(TextView中)rowView .findViewById(R.id.textView1);
tv.setText(项目[位置]);
    返回rowView;
}
}

Listview is not showing the string list rather showing only the custom list view text

My string declaration n the main

String[] Example = new String[]
        { "Android Introduction","Android Setup/Installation","Android Hello World",
                "Android Layouts/Viewgroups","Android Activity & Lifecycle","Intents in Android",
        "Cz"};
ArrayAdapter<String> ExampleArrayAdapter = new CustomAdapter(this, Example);

    ListView ExampleListView = (ListView)findViewById(R.id.listView);
    ExampleListView.setAdapter(ExampleArrayAdapter);

here is my customAdapter.java

public class CustomAdapter extends ArrayAdapter {
private LayoutInflater inflater;
public CustomAdapter (Activity activity, String[] items){
    super(activity, R.layout.custom_layout, items);
    inflater = activity.getWindow().getLayoutInflater();
}

@Override
public View getView(int position, View convertView, ViewGroup parent){
 Random rnd = new Random();

   int color = Color.argb(255, rnd.nextInt(256), rnd.nextInt(257), rnd.nextInt(258));

  View rowView = inflater.inflate(R.layout.custom_layout, parent, false);
    rowView.setBackgroundColor(color);

    return rowView;


}
}

here is the custom layout of my app, i have updated its........... custom_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_marginTop="70dp"
android:layout_height="70dp"
android:background="@drawable/blue_bg">

<LinearLayout
    android:layout_marginLeft="20dp"
    android:layout_width="345dp"
    android:layout_height="70dp"
    android:background="@drawable/white_bg">
    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="70dp"
        android:layout_height="60dp"
        android:src="@drawable/ic_launcher"
        android:layout_marginLeft="5dp"
        android:layout_marginTop="5dp" />


    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_marginLeft="10dp"
        android:gravity="left" >


        <TextView
            android:id="@+id/textView1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Large Text"
            android:layout_marginTop="10dp"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:textSize="19sp"
            android:textColor="#000000"
            android:textStyle="bold" />


        <TextView
            android:id="@+id/textView2"
            android:layout_width="match_parent"
            android:layout_height="15dp"
            android:text="Tweet body text here"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:layout_marginTop="5dp"
            android:ellipsize="end"
            android:lines="3"
            android:textColor="#000000"
            android:textSize="14sp" />


    </LinearLayout>

</LinearLayout>

</LinearLayout>

解决方案

You have to declare your CustomAdapter object in Activity As:

CustomAdapter ExampleArrayAdapter = new CustomAdapter(this, Example);

    ListView ExampleListView = (ListView)findViewById(R.id.listView);
    ExampleListView.setAdapter(ExampleArrayAdapter);

And Your Adapter Code should be:

public class CustomAdapter extends ArrayAdapter{
private LayoutInflater inflater;
private String[] items;
public CustomAdapter (Activity activity, String[] items){
          super(activity, 0, items);
    inflater = activity.getWindow().getLayoutInflater();
}

@Override
public View getView(int position, View convertView, ViewGroup parent){
 Random rnd = new Random();

   int color = Color.argb(255, rnd.nextInt(256), rnd.nextInt(257), rnd.nextInt(258));

  View rowView = inflater.inflate(R.layout.custom_layout, parent, false);
    rowView.setBackgroundColor(color);
TextView tv = (TextView) rowView .findViewById(R.id.textView1);
tv.setText(items[position]);
    return rowView;


}
}

这篇关于自定义列表视图没有显示出来的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆