在textview android中设置字符串

时间:2016-01-05 05:30:38

标签: android string textview

此代码应该计算所有应用程序组合的总缓存,但我无法测试它。我希望计算出的缓存显示在我的textview中,我该怎么做?

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    myMethod();
}

private void myMethod() {
    PackageManager packageManager = getPackageManager();
    List<ApplicationInfo> packages = packageManager
            .getInstalledApplications(PackageManager.GET_META_DATA);

    for (ApplicationInfo packageInfo : packages) {
        try {
            Context mContext = createPackageContext(
                    packageInfo.packageName, CONTEXT_IGNORE_SECURITY);
            File cacheDirectory = mContext.getCacheDir();
            if (cacheDirectory == null) {
                // cacheArrayList.add("0");
            } else {
                cacheArrayList
                        .add(String.valueOf(cacheDirectory.length() / 1024));
            }
        } catch (NameNotFoundException e) {
            e.printStackTrace();
        }
    }
    TextView myTextView = (TextView) findViewById(R.id.cacheSize);
    myTextView.setText("YOUR STRING"); // replaces textview text
    myTextView.append("YOUR STRING"); // appends to current textview's text

}

Textview如下

  <TextView
    android:id="@+id/cacheSize"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerVertical="true"
    android:layout_marginLeft="30dp"
    android:text="--" />

3 个答案:

答案 0 :(得分:1)

在for-each循环之后

使另一个循环将cacheArrayList中的数据放入某个字符串

for(String s : cacheArrayList){
    string = string+s+"\n"
}

然后将此字符串放入textView

答案 1 :(得分:0)

如果您尝试在TextView上设置文本,则可以使用setText或appendText。

TextView myTextView = (TextView)findViewById(R.id.cacheSize);
myTextView.setText("YOUR STRING"); // replaces textview text
myTextView.append("YOUR STRING"); // appends to current textview's text

答案 2 :(得分:0)

创建一个全局变量

TextView txt_view;

在活动添加的oncreate方法中,

txt_view = findViewById(R.id.cacheSize);

在您的活动中添加此方法

public void displaycache(){
                int k=0;
//on assuming cacheArrayList is a global variable
                for(int i =0;i<cacheArrayList.size();i++){
                   k+=Integer.parseInt(cacheArrayList.get(i));
                 }
                txt_view.setText(String.valueOf(k));
            }
致电displaycache();

后,请致电mymethod();

替换myMethod()

private void myMethod() {
    PackageManager packageManager = getPackageManager();
    List<ApplicationInfo> packages = packageManager.getInstalledApplications(PackageManager.GET_META_DATA);
    for (ApplicationInfo packageInfo : packages) {
        try {
            Context mContext = createPackageContext(
                    packageInfo.packageName, CONTEXT_IGNORE_SECURITY);
            File cacheDirectory = mContext.getCacheDir();
            File[] files = cacheDirectory.listFiles();
            for (File f:files) {
                cacheArrayList.add(String.valueOf(f.length() / 1024));
            }
        } catch (NameNotFoundException e) {
            e.printStackTrace();
        }
    }
}