【问题标题】:ArrayAdapter's getView() method getting called automatically on load and while scrolling listviewArrayAdapter 的 getView() 方法在加载和滚动列表视图时自动调用
【发布时间】:2012-11-10 05:50:58
【问题描述】:

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

问题是我调用 getView 例如 2 次,它被自动调用三次,当我滚动 lsitview 时,会自动添加一行。

请告诉我为什么会发生这种情况以及我应该做些什么来克服这个问题。

用于生成自定义视图的xml

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 类

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;
    }

}

【问题讨论】:

  • 当你滚动 listview 比 getview 方法调用。它是您无法更改的默认方法.....并且调用时间取决于屏幕上的可见项目..
  • 我应该怎么做才能克服这个问题
  • 这是 listview 的功能,可以使 Listview 滚动高效。我不知道你为什么不想要这个?。
  • @Dharmendra 你可以看到我上传了一张图片,添加了额外的一行,数据的重复显示无论如何都是不合适的,这是我的问题

标签: android android-listview android-arrayadapter


【解决方案1】:

您似乎没有覆盖getCount 方法。此方法应返回行数。

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

此外,您如何知道要创建的行数?你需要为标题创建一次吗?在这种情况下,您需要在 List 的 headerView 中对其进行膨胀。

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

【讨论】:

  • 不,我不计算行数,返回 1 只显示行中的 1 项我添加了两行
  • 你需要返回你想要的行数。因此,如果您想要 2,则返回 2。如果您想要 n,则返回 n。
  • 我不知道 count 的值,所以我应该返回什么
  • 必须知道,您需要在构造函数中传递该计数,否则适配器将如何知道要创建多少行?
  • 而 getCount 不能解决我的问题
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多