1

我找到了显示电池统计信息的示例代码。唯一的“问题”是我想在 alistview而不是setText. 这是代码的相关部分:

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);
  }
};

有什么办法吗?

4

2 回答 2

2

您需要为每一行创建一个布局,并TextView在其中。

然后你创建一个List<Map<String, String> list = new ArrayList<>();用于保存电池信息,如 Key -> Value。

最后,您需要创建两个数组

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 回答
0

ListViewBaseAdapter一起使用

编辑 1

创建一个String数组,如:

String[] status = {
                "Health: "+health,
                "Icon Small:"+icon_small,
                "Level: "+level,
                "Plugged: "+plugged,
                "Present: "+present,
                "Scale: "+scale,
                "Status: "+status,
                "Technology: "+technology,
                "Temperature: "+temperature,
                "Voltage: "+voltage
};

并将这个数组传入setAdapter()

lvStatus.setAdapter(new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1, status));

编辑 2 你必须ListView在你的布局中实现

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >

<ListView
    android:id="@+id/lv_Status"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

</ListView>

然后,你必须在你的Activity

    ListView lvStatus = (ListView) findViewById(R.id.lv_Status);
于 2013-06-27T09:46:13.940 回答