1

I found sample code to show the battery stats. The only one "problem" with it is that I want to display this information in a listview and not with setText. This is the relevant part of the code:

private BroadcastReceiver batteryInfoReceiver = new BroadcastReceiver() {
  @Override
  public void onReceive(Context context, Intent intent) {

     int  health= intent.getIntExtra(BatteryManager.EXTRA_HEALTH,0);
     int  icon_small= intent.getIntExtra(BatteryManager.EXTRA_ICON_SMALL,0);
     int  level= intent.getIntExtra(BatteryManager.EXTRA_LEVEL,0);
     int  plugged= intent.getIntExtra(BatteryManager.EXTRA_PLUGGED,0);
     boolean  present= intent.getExtras().getBoolean(BatteryManager.EXTRA_PRESENT); 
     int  scale= intent.getIntExtra(BatteryManager.EXTRA_SCALE,0);
     int  status= intent.getIntExtra(BatteryManager.EXTRA_STATUS,0);
     String  technology= intent.getExtras().getString(BatteryManager.EXTRA_TECHNOLOGY);
     int  temperature= intent.getIntExtra(BatteryManager.EXTRA_TEMPERATURE,0);
     int  voltage= intent.getIntExtra(BatteryManager.EXTRA_VOLTAGE,0);


     batteryInfo.setText(
             "Health: "+health+"\n"+
             "Icon Small:"+icon_small+"\n"+
             "Level: "+level+"\n"+
             "Plugged: "+plugged+"\n"+
             "Present: "+present+"\n"+
             "Scale: "+scale+"\n"+
             "Status: "+status+"\n"+
             "Technology: "+technology+"\n"+
             "Temperature: "+temperature+"\n"+
             "Voltage: "+voltage+"\n");
         imageBatteryState.setImageResource(icon_small);
  }
};

Is there any way to do it?

4

2 に答える 2

2

行ごとにレイアウトを作成し、そのTextView中に入れる必要があります。

次に、List<Map<String, String> list = new ArrayList<>();Key -> Value のようにバッテリー情報を保持するための を作成します。

最後に、2 つの配列を作成する必要があります。

String[] from = new String[] {<the keys>};
int[] to = new int[] {<the id\'s in your layout>};

そして、それをアダプターに入れるだけです:

SimpleAdapter adapter = new SimpleAdapter(this, list,
                                   R.layout.your_layout, from, to);
listView.setAdapter(adapter);
于 2013-06-27T09:52:10.257 に答える