在适配器上调用notifyDataSetChanged()之后,带有LinkedHashMap的Android Listview不会添加列表行

时间:2013-12-13 12:59:47

标签: android listview hashmap adapter baseadapter

我正在开发一个应用程序,我们希望显示自定义列表视图,该视图显示正在运行的应用程序列表以及它们的传输和接收数据使用情况。

我已经使用LinkedHashMap数据结构用于android列表视图的适配器,并且在运行应用程序时调用同一适配器上的notifyDataSetChanged()方法已更改它将在列表视图中反映的数据用法。

到目前为止它工作正常但是当新应用程序开始传输或接收数据时,此应用程序将不会添加到列表视图中。

以下是代码段:

Adapter : ProcessListAdapterWithMap

public class ProcessListAdapterWithMap extends BaseAdapter {

// List context
private final Context context;
private static LayoutInflater inflater=null;

private LinkedHashMap<Integer, Long> linkedHashMap = new LinkedHashMap<Integer, Long>();
private Integer[] mKeys;


public ProcessListAdapterWithMap(Context context, LinkedHashMap<Integer, Long> data) {
    this.context = context; 
    this.linkedHashMap = data;

    mKeys = linkedHashMap.keySet().toArray(new Integer[data.size()]);

    inflater = (LayoutInflater)this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);      
}

/**
 * Constructing list element view
 */
@SuppressWarnings("deprecation")
@Override
public View getView(int position, View convertView, ViewGroup parent) {

    View rowView = convertView;
    ViewHolder holder; // to reference the child views for later actions        

    if (rowView == null) {

        rowView = inflater.inflate(R.layout.process_view_list_row, parent, false);

        // cache view fields into the holder
        holder = new ViewHolder();

        holder.appName = (TextView) rowView.findViewById(R.id.app_name);
        holder.text_upload = (TextView) rowView.findViewById(R.id.upload_data);
        holder.text_download = (TextView) rowView.findViewById(R.id.download_data);
        holder.appIcon = (ImageView) rowView.findViewById(R.id.app_icon);
        // associate the holder with the view for later lookup
        rowView.setTag(holder);
    }else{

        // view already exists, get the holder instance from the view
        holder = (ViewHolder) rowView.getTag(); 
        rowView = convertView;
    }   

    Logger.vLog("ProcessListAdapterWithMap :-> getView", "mKeys Length : "+mKeys.length);

    int app_uid = mKeys[position];

    ProcessInfo processInfo = new ProcessInfo().getApplicationDetailFromUID(this.context, app_uid);

    holder.appName.setText(processInfo.getApp_name());

    long actual_transmit = (processInfo.getApp_transmit());
    holder.text_upload.setText(""+actual_transmit);

    long actual_receive = (processInfo.getApp_receive());       
    holder.text_download.setText(""+actual_receive);

    holder.appIcon.setBackgroundDrawable(processInfo.getApp_icon());

    return rowView;

}

@Override
public int getCount() {
    return linkedHashMap.size();
}

@Override
public ProcessInfo getItem(int position) {

    int app_uid = mKeys[position];
    ProcessInfo processInfo = new ProcessInfo().getApplicationDetailFromUID(context, app_uid);

    return processInfo;
}

@Override
public long getItemId(int position) {
    return position;
}

class ViewHolder {

    TextView appName;
    TextView text_upload;
    TextView text_download;     
    ImageView appIcon;

}
}

Generating a List view and assigned a adapter to it -

    processList = (ListView) findViewById(R.id.process_list);
    lhmOfAppsWithDataUsage = getLinkedHashMapFormSet(setOfRunningAppUID);

    listAdapterWithMap = new ProcessListAdapterWithMap(DataCollectionScreenActivity.this, lhmOfAppsWithDataUsage);

apply notifyDataSetChanged() method

printMap(DataCollectionScreenActivity.this, lhmOfAppsWithDataUsage);                
Logger.vLog("Runnable : ", "LinkedHashMap Size : "+lhmOfAppsWithDataUsage.size());                              

listAdapterWithMap.notifyDataSetChanged();

UPDATE :

我在日志中获得了正确的应用程序列表。但它不会复制到列表视图中。

问题出在哪里?提前谢谢......

0 个答案:

没有答案