ArrayAdapter的getView()方法在加载时和滚动listview时自动调用 [英] ArrayAdapter's getView() method getting called automatically on load and while scrolling listview

查看:84
本文介绍了ArrayAdapter的getView()方法在加载时和滚动listview时自动调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建自定义列表活动.有两类,一类具有扩展的ListActivity,另一类具有针对该列表活动的ArrayAdapter.此列表视图包含一个文本视图和一个表格布局.

I am creating the custom list activity. There are two classes one is having extended ListActivity and other is having ArrayAdapter for that list activity. This listview contains a textview and a table layout.

问题是我正在调用getView两次,例如它被自动调用三次,当我滚动lsitview时也会自动添加一行.

Problem is that i am calling getView for eg 2 times its is called automatically thrice, also when i am scrolling the lsitview a row is added automatically.

请告诉我为什么会发生这种情况,我应该怎么做才能克服这种情况.

Please suggest me why this is happening and what should i do to overcome this.

用于生成自定义视图的xml

xml for generating custom view

displayweek.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/tvTime"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="" />

    <HorizontalScrollView android:id="@+id/horizontalScrollView1" android:layout_width="match_parent" android:layout_height="wrap_content">
               <TableLayout android:layout_width="wrap_content" android:id="@+id/tableLayout1" android:layout_height="wrap_content">
               </TableLayout>
      </HorizontalScrollView>

</LinearLayout>

调用getview

if (weekAdapter == null) {
            weekAdapter = new weekAdapter(this, 0);
        }
        lvSchedule.setAdapter(weekAdapter);
        weekAdapter.add("true"+","+"10:10Am"+","+"Monday"+","+"Tuesday"+","+"Wednesday"+","+"Thusday"+","+"Friday"
                +","+"Saturday"+","+"Sunday");
        weekAdapter.add("true"+","+"10:10Am"+","+" "+","+" "+","+" "+","+"Sumit"+","+" "+","+" "+","+"Humam");

weekAdapter类

weekAdapter class

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Color;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.ArrayAdapter;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

public class weekAdapter extends ArrayAdapter<String>{

    Context context;

    public weekAdapter(Context context, int textViewResourceId) {
        super(context, textViewResourceId);
        this.context=context;
    }

    public weekAdapter(Context context, int textViewResourceId, String[] objects) {
        super(context,textViewResourceId, objects);
        this.context=context;
    }



    @SuppressLint("NewApi")
    @Override
    public View getView(int position,View convertView,ViewGroup parent)
    {

        if(convertView==null)
        {
            convertView=LayoutInflater.from(getContext()).inflate(R.layout.displayweek,null);       
        }

        TableLayout table=(TableLayout)convertView.findViewById(R.id.tableLayout1);
        TextView tvTime=(TextView)convertView.findViewById(R.id.tvTime);

        String str=getItem(position);
        String []schedule=str.split(",");
        Log.d("schedule length",""+schedule.length);


        if(schedule[0].trim().equals("true"))
            tvTime.setText(""+schedule[1]);
        else
            tvTime.setText("");

         TableRow tr = new TableRow(context);
         tr.setId(1);
         tr.setTag("");                         
         tr.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
         TextView tvMon = new TextView(context);
         tvMon.setId(1);
         tvMon.setText(""+schedule[2]);
         tvMon.setPadding(2, 0, 5, 0);
         tvMon.setTextColor(Color.BLACK);
         tvMon.setHeight(50);
         tvMon.setGravity(Gravity.RIGHT);
         tr.addView(tvMon);

         TextView tvTue = new TextView(context);
         tvTue.setId(1);
         tvTue.setText(""+schedule[3]);
         tvTue.setPadding(2, 0, 5, 0);
         tvTue.setTextColor(Color.BLACK);
         tvTue.setHeight(50);
         tvTue.setGravity(Gravity.RIGHT);
         tr.addView(tvTue);

         TextView tvWed = new TextView(context);
         tvWed.setId(1);
         tvWed.setText(""+schedule[4]);
         tvWed.setPadding(2, 0, 5, 0);
         tvWed.setTextColor(Color.BLACK);
         tvWed.setHeight(50);
         tvWed.setGravity(Gravity.RIGHT);
         tr.addView(tvWed);

         TextView tvThus = new TextView(context);
         tvThus.setId(1);
         tvThus.setText(""+schedule[5]);
         tvThus.setPadding(2, 0, 5, 0);
         tvThus.setTextColor(Color.BLACK);
         tvThus.setHeight(50);
         tvThus.setGravity(Gravity.RIGHT);
         tr.addView(tvThus);

         TextView tvFri = new TextView(context);
         tvFri.setId(1);
         tvFri.setText(""+schedule[6]);
         tvFri.setPadding(2, 0, 5, 0);
         tvFri.setTextColor(Color.BLACK);
         tvFri.setHeight(50);
         tvFri.setGravity(Gravity.RIGHT);
         tr.addView(tvFri);

         TextView tvSat = new TextView(context);
         tvSat.setId(1);
         tvSat.setText(""+schedule[7]);
         tvSat.setPadding(2, 0, 5, 0);
         tvSat.setTextColor(Color.BLACK);
         tvSat.setHeight(50);
         tvSat.setGravity(Gravity.RIGHT);
         tr.addView(tvSat);

         TextView tvSun = new TextView(context);
         tvSun.setId(1);
         tvSun.setText(""+schedule[8]);
         tvSun.setPadding(2, 0, 5, 0);
         tvSun.setTextColor(Color.BLACK);
         tvSun.setHeight(50);
         tvSun.setGravity(Gravity.RIGHT);
         tr.addView(tvSun);
         table.addView(tr, new TableLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));             
        return convertView;
    }

}

推荐答案

似乎您尚未覆盖

It seems you have not overridden getCount method. This method should return the number of row.

public int getCount () {
    return n; /* where n is the number of row you want */
}

此外,您如何知道要创建的行数?您是否只需为标头创建一次?在这种情况下,您需要在List的headerView中对其进行充气.

Besides how do you know the number of rows to be created? Do you need to create it just once for header? In that case you need to inflate that in headerView of List.

调用getView是正确的行为,因为适配器试图在运行时膨胀视图以显示在列表中.

getView getting called is right behavior as the adapter is trying to inflate views at runtime to show in the list.

这篇关于ArrayAdapter的getView()方法在加载时和滚动listview时自动调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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