多次调用getView方法

Posted

技术标签:

【中文标题】多次调用getView方法【英文标题】:calling of getView method multiple times 【发布时间】:2012-11-13 14:13:47 【问题描述】:

我正在实现自定义列表视图,但问题是 getview 方法被多次调用,即如果列表视图中有 4 个项目,则每个列表项调用 getview 方法 4 次

代码在这里..

Notification.java

public class Notifications extends ListActivity implements
    AsyncTaskCompleteListener, OnClickListener 

private ArrayList<HashMap<String, String>> unreadNotifications = new ArrayList<HashMap<String, String>>();
private ArrayList<HashMap<String, String>> upcommingNotifications = new ArrayList<HashMap<String, String>>();
private ArrayList<String> days = new ArrayList<String>();

private MyAsyncTask notificationsAsyncTask;

private NotificationAdapter adapter;
Button btn_Unread, btn_upcomming;

@Override
protected void onCreate(Bundle savedInstanceState) 

    super.onCreate(savedInstanceState);
    setContentView(R.layout.dash_notifications_layout);
    initialise();
    ArrayList<NameValuePair> argumentsList = new ArrayList<NameValuePair>();
    argumentsList = PlutoNameValuePairs.getNotificationNVPair("1",
            "consumer_show");
    notificationsAsyncTask = new MyAsyncTask(Notifications.this,
            PlutoConstants.notificationUrl, argumentsList, 1);
    notificationsAsyncTask.execute();
    notificationsAsyncTask.setOnResultsListener(this);
    btn_Unread.setOnClickListener(this);
    btn_upcomming.setOnClickListener(this);



private void initialise() 
    btn_Unread = (Button) findViewById(R.id.btn_unread_notifications);
    btn_upcomming = (Button) findViewById(R.id.btn_upcomming_notifications);


@Override
public void onResultsSucceeded(String result, int asyncTaskNo) 

    Logger.logger("Notification  ", "Result = " + result);

    try 
        JSONObject jsonResponce = new JSONObject(result);
        Logger.logger("Result : " + jsonResponce);
        switch (asyncTaskNo) 
        case 1:
            if (unreadNotifications == null) 
                unreadNotifications = new ArrayList<HashMap<String, String>>();

             else 

                unreadNotifications.clear();
            
            parseDataForNotification(jsonResponce, unreadNotifications,
                    asyncTaskNo);
            break;
        case 2:
            if (upcommingNotifications == null) 
                upcommingNotifications = new ArrayList<HashMap<String, String>>();

             else 
                unreadNotifications.clear();
                upcommingNotifications.clear();
            
            parseDataForNotification(jsonResponce, upcommingNotifications,
                    asyncTaskNo);
        default:
            break;
        

     catch (JSONException e) 
        Logger.errorLog("Does not found json Object");
        e.printStackTrace();
    


public void parseDataForNotification(JSONObject jsonResponce,
        ArrayList<HashMap<String, String>> listToModify, int arrayCheck) 
    int i = 0;

    JSONArray dislpayArray = null;
    try 
        if (arrayCheck == 1) 

            dislpayArray = jsonResponce.getJSONArray("Unread");
         else 
            dislpayArray = jsonResponce.getJSONArray("Unsend");

        

        for (i = 0; i < dislpayArray.length(); i++) 
            HashMap<String, String> map = new HashMap<String, String>();
            JSONObject e = dislpayArray.getJSONObject(i);

            map.put("category_name", e.getString("category_name"));
            map.put("time", e.getString("time"));
            map.put("repeat_status", e.getString("repeat_status"));

            if (e.has("day")) 
                JSONArray day_array = e.getJSONArray("day");

                for (int j = 0; j < day_array.length(); j++) 
                    JSONObject day = day_array.getJSONObject(j);
                    map.put("day", day.getString("day"));
                    days.add(day.getString("day"));

                
            
            unreadNotifications.add(map);

            adapter = new NotificationAdapter(Notifications.this,
                    R.layout.dash_custom_notifications_layout,
                    unreadNotifications, i, days);

            setListAdapter(adapter);

        

     catch (JSONException e) 
        Log.e("log_tag", "Error parsing data " + e.toString());
    

    //



@Override
public void onClick(View v) 

    switch (v.getId()) 
    case R.id.btn_unread_notifications:
        ArrayList<NameValuePair> argumentsList = new ArrayList<NameValuePair>();
        argumentsList = PlutoNameValuePairs.getNotificationNVPair("1",
                "consumer_show");
        notificationsAsyncTask = new MyAsyncTask(Notifications.this,
                PlutoConstants.notificationUrl, argumentsList, 1);
        notificationsAsyncTask.execute();
        notificationsAsyncTask.setOnResultsListener(this);

        break;

    case R.id.btn_upcomming_notifications:
        days.clear();
        ArrayList<NameValuePair> argumentsList1 = new ArrayList<NameValuePair>();
        argumentsList1 = PlutoNameValuePairs.getNotificationNVPair("1",
                "consumer_show");
        notificationsAsyncTask = new MyAsyncTask(Notifications.this,
                PlutoConstants.notificationUrl, argumentsList1, 2);
        notificationsAsyncTask.execute();
        notificationsAsyncTask.setOnResultsListener(this);

        break;
    




NotificationAdapter.java

public class NotificationAdapter extends BaseAdapter 

Context context;
private ArrayList<HashMap<String, String>> listUnread;
private LayoutInflater inflater;

String daysInAdapter[] = new String[]  "Sun", "Mon", "Tue", "Wed", "Thur",
        "Fri", "Sat" ;
private ArrayList<String> days;

public NotificationAdapter(Context context, int landedMyCustomLayout,
        ArrayList<HashMap<String, String>> listUnread, int i,
        ArrayList<String> days) 

    this.context = context;
    this.listUnread = listUnread;
    //this.count = i;
    this.days = days;

    // daysInAdapter = "Sun","Mon","Tue","Wed","Thur","Fri","Sat";


@Override
public int getCount() 

    return listUnread.size();


@Override
public HashMap<String, String> getItem(int position) 

    return listUnread.get(position);


@Override
public long getItemId(int position) 

    return position;


@Override
public View getView(int position, View convertView, ViewGroup parent) 

    HashMap<String, String> map = getItem(position);

    if (convertView == null) 
        inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(
                R.layout.dash_custom_notifications_layout, parent, false);

    

    TextView txt_time = (TextView) convertView.findViewById(R.id.txt_time);
    TextView txt_category_name = (TextView) convertView
            .findViewById(R.id.txt_category_name_1);
    txt_time.setText(map.get("time"));
    txt_category_name.setText(map.get("category_name"));
    ImageView img = (ImageView) convertView
            .findViewById(R.id.img_repeat_status);
    if (map.get("repeat_status").equals("1")) 
        img.setImageResource(R.drawable.ic_launcher);
     else 
        img.setImageResource(R.drawable.temp_stub);
    

    TextView txt_day_sun = (TextView) convertView
            .findViewById(R.id.txt_days_sun);
    txt_day_sun.setText("");

    for (int j = 0; j < days.size(); j++) 

        txt_day_sun.append(days.get(j)+" ");

    

    return convertView;



dash_notification_)layout.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:foo="http://schemas.android.com/apk/res/com.pluto"
android:layout_
android:layout_
android:orientation="vertical" >

<RelativeLayout
    android:id="@+id/rl_notification_buttons"
    android:layout_
    android:layout_
    android:background="#ffffff" >

    <com.oab.widgets.ButtonPlus
        android:id="@+id/btn_unread_notifications"
        style="@style/button_theme"
        foo:customFont="@string/font_name"
        android:text="Unread" />

    <com.oab.widgets.ButtonPlus
        android:id="@+id/btn_upcomming_notifications"
        style="@style/button_theme"
        android:layout_toRightOf="@id/btn_unread_notifications"
        foo:customFont="@string/font_name"
        android:text="Upcomming" />

    <com.oab.widgets.TextViewPlus
        android:id="@+id/txt_Notification_count"
        style="@style/textview_theme"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:layout_marginRight="15dp"
        foo:customFont="@string/font_name"
        android:gravity="center_horizontal"
        android:text="2 New Notifications"
        android:textSize="12sp" />
</RelativeLayout>

<ListView
    android:id="@id/android:list"
    android:layout_
    android:layout_
    android:layout_below="@id/rl_notification_buttons"
    android:layout_weight="1"
    android:drawSelectorOnTop="false" />

<include
    android:id="@+id/btn_menu_bar"
    android:layout_alignParentBottom="true"
    layout="@layout/app_menuforxml_layout" />

</RelativeLayout>

【问题讨论】:

这就是他们的工作方式。你想要什么? 查看这里***.com/questions/9940971/… 它应该是这样工作的。 getView 填充列表视图的每个项目。 Get a View that displays the data at the specified position in the data set 如果这就是您所关心的,它会为每个项目调用一次,而不是一次又一次地重新填充整个列表。此外,对于屏幕上可见的每个新项目,它都会调用一次,它不会填充不应立即可见的项目。 【参考方案1】:

自定义 ListView 应该以这种方式工作。 每次显示新行时都会调用 getView。

【讨论】:

以上是关于多次调用getView方法的主要内容,如果未能解决你的问题,请参考以下文章

多次调用自定义列表视图适配器 getView 方法,并且顺序不一致

自定义列表视图适配器 getView 方法被多次调用,并且顺序不一致

适配器 getView 被多次调用,位置为 0

Android LIstView初次创建getview方法执行多次问题

android-继承BaseAdapter--自己定义适配器,getView运行多次的解决方法

android 分享一个处理BaseAdapter,getView()多次加载的方法