-4

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

代码在这里..

通知.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_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<RelativeLayout
    android:id="@+id/rl_notification_buttons"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    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_width="fill_parent"
    android:layout_height="wrap_content"
    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>
4

1 回答 1

0

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

于 2012-11-26T05:55:11.307 回答