2017-05-30 25 views
0

我試圖修改應用程序以使用getShortClassName()方法而不是顯示完整的類名稱(例如「com.google.app.MainActivity」簡單地爲「.MainActivity」) 。目前的設置沒有給我錯誤,但不顯示文本。在Android中使用getShortClassName()方法

我認爲解決方案並不複雜,雖然我只是沒有經驗讓它工作。幫幫我?

\t @Override 
 
\t public View getChildView (int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { 
 
\t \t MyActivityInfo activity = (MyActivityInfo)getChild(groupPosition, childPosition); 
 
\t \t LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
 
\t \t View view = inflater.inflate(R.layout.all_activities_child_item, null); 
 

 
\t \t TextView text1 = (TextView) view.findViewById(android.R.id.text1); 
 
\t \t text1.setText(activity.getName()); 
 

 
     //TextView text2 = (TextView) view.findViewById(android.R.id.text2); 
 
     //text2.setText(activity.getComponentName().getClassName()); 
 

 
\t \t TextView text2 = (TextView) view.findViewById(android.R.id.text2); 
 
\t \t text2.setText(activity.getShortClassName()); 
 
\t 
 
\t \t ImageView icon = (ImageView) view.findViewById(android.R.id.icon); 
 
\t \t icon.setImageDrawable(activity.getIcon()); 
 

 
\t \t return view; 
 
\t }

public class MyActivityInfo implements Comparable<MyActivityInfo> { 
 
\t public MyActivityInfo(ComponentName activity, PackageManager pm) { 
 
\t \t this.component_name = activity; 
 

 
\t \t ActivityInfo act; 
 
\t \t try { 
 
\t \t \t act = pm.getActivityInfo(activity, 0); 
 
\t \t \t this.name = act.loadLabel(pm).toString(); 
 
\t \t \t try { 
 
\t \t \t \t this.icon = (BitmapDrawable)act.loadIcon(pm); 
 
\t \t \t } 
 
\t \t \t catch(ClassCastException e) { 
 
\t \t \t \t this.icon = (BitmapDrawable)pm.getDefaultActivityIcon(); 
 
\t \t \t } 
 
\t \t \t this.icon_resource = act.getIconResource(); 
 
\t \t } catch (NameNotFoundException e) { 
 
\t \t \t this.name = activity.getShortClassName(); 
 
\t \t \t this.icon = (BitmapDrawable)pm.getDefaultActivityIcon(); 
 
\t \t \t this.icon_resource = 0; 
 
\t \t } 
 

 
\t \t this.icon_resource_name = null; 
 
\t \t if(this.icon_resource != 0) { 
 
\t \t \t try { 
 
\t \t \t \t this.icon_resource_name = pm.getResourcesForActivity(activity).getResourceName(this.icon_resource); 
 
\t \t \t } catch (Exception e) {} 
 
\t \t } 
 
\t } 
 

 
\t public ComponentName getComponentName() { 
 
\t \t return component_name; 
 
\t } 
 

 
\t public BitmapDrawable getIcon() { 
 
\t \t return icon; 
 
\t } 
 

 
\t public String getName() { 
 
\t \t return name; 
 
\t } 
 

 
\t public String getIconResourceName() { 
 
\t \t return icon_resource_name; 
 
\t } 
 

 
    public String getShortClassName() { 
 
     return class_name; 
 
    } 
 

 
\t protected ComponentName component_name; 
 
\t protected BitmapDrawable icon; 
 
\t protected int icon_resource; 
 
\t protected String icon_resource_name; 
 
\t protected String name; 
 
    protected String class_name; 
 

 

 
\t @Override 
 
\t public int compareTo(MyActivityInfo another) { 
 
\t \t int cmp_name = this.name.compareTo(another.name); 
 
\t \t if (cmp_name != 0) return cmp_name; 
 

 
\t \t int cmp_component = this.component_name.compareTo(another.component_name); 
 
\t \t return cmp_component; 
 
\t } 
 

 
\t @Override 
 
\t public boolean equals(Object other) { 
 
\t \t if(!other.getClass().equals(MyPackageInfo.class)) { 
 
\t \t \t return false; 
 
\t \t } 
 

 
\t \t MyActivityInfo other_info = (MyActivityInfo)other; 
 
\t \t return this.component_name.equals(other_info.component_name); 
 
\t } 
 
}

回答

0

你可以只使用getSimpleName()類對象的方法:

String a = "Hello"; 
    System.out.println(a.getClass().getSimpleName()); 
    System.out.println(a.getClass().getName()); 

輸出:

String 
java.lang.String