将多个Textview添加为ListView项

时间:2016-12-28 09:10:53

标签: android listview textview android-adapter

在android中,我正在尝试创建一个ListView,每个项目都是3个textviews。当我运行我的程序时,它显示一个白页(空列表)。作为一个初学者,我不确定我做错了什么。

下面是我的自定义适配器和自定义对象类。

class CustomObject {

    private String title;
    private String date;
    private String body;

    public CustomObject(String prop1, String prop2, String prop3) {
        this.title = prop1;
        this.date = prop2;
        this.body=prop3;
    }

    public String getTitle() {
        return title;
    }

    public String getDate() {
        return date;
    }

    public String getBody(){
        return body;
    }

}


public class CustomAdapter extends BaseAdapter {

    private LayoutInflater inflater;
    private ArrayList<CustomObject> objects;
    private Activity activity;

    private class ViewHolder {
        TextView titleTextView;
        TextView dateTextView;
        TextView bodyTextView;
    }


    public CustomAdapter(Context context, ArrayList<CustomObject> objects, Activity activity) {
        super();
        inflater = LayoutInflater.from(context);
        this.objects = objects;
        this.activity= activity;
    }

    @Override
    public int getCount() {
        return objects.size();
    }

    @Override
    public CustomObject getItem(int position) {
        return objects.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if(convertView == null) {
            holder = new ViewHolder();
            convertView = inflater.inflate(R.layout.activity_listview, null); //NOT SURE


            holder.titleTextView = new TextView(activity);
            //holder.titleTextView.setText(titleList.get(i));
            holder.titleTextView.setTextSize(14);
            holder.titleTextView.setTypeface(holder.titleTextView.getTypeface(), Typeface.BOLD); //title in bold
            holder.titleTextView.setGravity(Gravity.RIGHT);


            holder.dateTextView = new TextView(activity);
            //tvDate[i].setText(dateList.get(i));
            holder.dateTextView.setTextSize(14);
            holder.dateTextView.setTypeface(holder.dateTextView.getTypeface(), Typeface.ITALIC); //Date in ITALIC
            holder.dateTextView.setGravity(Gravity.RIGHT);

            holder.bodyTextView = new TextView(activity);
            //holder.bodyTextView.setText(textList.get(i));
            holder.bodyTextView.setTextSize(14);
            holder.bodyTextView.setGravity(Gravity.RIGHT);


            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.titleTextView.setText(objects.get(position).getTitle());
        holder.dateTextView.setText(objects.get(position).getDate());
        holder.bodyTextView.setText(objects.get(position).getBody());
        return convertView;
    }


}

以下是创建和显示listView

的方法
  private void displayNews()
    {

        ListView listView =  new ListView(activity);
        //listView.setLayoutParams();


        ArrayList<CustomObject> objects = new ArrayList<CustomObject>();
        for(int i=0; i<titleList.size(); i++)
            objects.add(new CustomObject(titleList.get(i), dateList.get(i),textList.get(i) ));
        CustomAdapter customAdapter = new CustomAdapter(activity, objects, activity);
        listView.setAdapter(customAdapter);


        //THE WHOLE VIEW
        ScrollView sv = new ScrollView(activity);
        LinearLayout ll = new LinearLayout(activity);
        ll.setOrientation(LinearLayout.VERTICAL);
        ll.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT));
        ll.setGravity(Gravity.RIGHT);

        ll.addView(listView,LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT); //adding


        sv.addView(ll);//adding the layout view to the scorllable view
        activity.setContentView(sv);//adding the scrollable view to the activity :)

    }

以下是 activity_listview.xml

<?xml version="1.0" encoding="utf-8"?>
<!--  Single List Item Design -->

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/label"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="10dip"
    android:textSize="16dip"
    android:textStyle="bold"
    android:gravity="right">
</TextView>

2 个答案:

答案 0 :(得分:1)

更改import akka.pattern.pipe override def receive: Receive = { case obj: DataObject => val firstFuture = OneActor ? obj val secondFuture = SecondActor ? obj firstFuture .zip(secondFuture) .map { case (first, second) => AccumulatedResult(first + second) } .pipeTo(self) case AccumulatedResult(res) => println(res) } 例如如下:

activity_listview.xml

更改夸大视图并初始化<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:id="@+id/txtTitle" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="10dip" android:textSize="16dip" android:textStyle="bold"> </TextView> <TextView android:id="@+id/txtDate" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="10dip" android:textSize="16dip"> </TextView> <TextView android:id="@+id/txtBody" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="10dip" android:textSize="16dip"> </TextView> </LinearLayout> 实例的代码:

ViewHolder

请注意,holder = new ViewHolder(); convertView = inflater.inflate(R.layout.activity_listview, null); holder.titleTextView = (TextView) convertView.findViewById(R.id.txtTitle); holder.dateTextView = (TextView) convertView.findViewById(R.id.txtDate); holder.bodyTextView = (TextView) convertView.findViewById(R.id.txtBody); R.id.txtTitleR.id.txtDate与布局文件(activity_listview.xml)中的R.id.txtBodyandroid:id="@+id/txtTitle"android:id="@+id/txtDate"相对应。

<强>更新

我编辑布局文件。您可以尝试使用Android Studio中的GUI设计布局。

答案 1 :(得分:0)

如果要动态创建文本视图,则必须将它们添加到viewgroup。 与您在此行中对listview所做的类似:

ll.addView(listView,LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT); //adding

只需创建新的ViewGroup(LinearLayout,相对布局等..)并添加文本视图。

希望它有所帮助!