ArrayAdapter's getView() method getting called automatically on load and while scrolling listview

亡梦爱人 提交于 2019-12-11 04:59:57

问题


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.

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

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

}

回答1:


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 */
}

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 getting called is right behavior as the adapter is trying to inflate views at runtime to show in the list.



来源:https://stackoverflow.com/questions/13319669/arrayadapters-getview-method-getting-called-automatically-on-load-and-while-s

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!