Minggu, 05 Januari 2014

List System Properties of Android system

The example call getProperties() method of java.lang.System class to get the system properties. It modify the custom ListView from last example "Implement OnItemClickListener for custom ListView" and "Create custom ListView of custom object".

List System Properties
List System Properties
MainActivity.java
package com.example.androidcustomlistview;

import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Properties;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.app.Activity;
import android.content.Context;

public class MainActivity extends Activity {

private class SystemProperties{
private String propertiesName;
private String properties;

SystemProperties(String pName, String p){
propertiesName = pName;
properties = p;
}

public String getPropertiesName(){
return propertiesName;
}

public String getpPoperties(){
return properties;
}
}

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView listView = (ListView)findViewById(R.id.listview);

//Init ArrayList of MyObject
ArrayList<SystemProperties> myPropertyList = new ArrayList<SystemProperties>();

//get System Properties
Properties properties = System.getProperties();
Enumeration<String> propEnum =
(Enumeration<String>)properties.propertyNames();

while(propEnum.hasMoreElements()){
String propName = propEnum.nextElement();
String prop = System.getProperty(propName);
SystemProperties nexProp = new SystemProperties(propName, prop);
myPropertyList.add(nexProp);
}


MyAdapter myAdapter = new MyAdapter(this, myPropertyList);
listView.setAdapter(myAdapter);

listView.setOnItemClickListener(new OnItemClickListener(){

@Override
public void onItemClick(AdapterView<?> parent,
View view, int position, long id) {
SystemProperties clickedObj = (SystemProperties)parent.getItemAtPosition(position);
Toast.makeText(MainActivity.this,
clickedObj.getPropertiesName() + ":\n" +
clickedObj.getpPoperties(),
Toast.LENGTH_LONG).show();
}});

}

private class MyAdapter extends BaseAdapter {

private ArrayList<SystemProperties> myList;

private Activity parentActivity;
private LayoutInflater inflater;

public MyAdapter(Activity parent, ArrayList<SystemProperties> l) {
parentActivity = parent;
myList=l;
inflater = (LayoutInflater)parentActivity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

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

@Override
public Object getItem(int position) {
return myList.get(position);
}

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

@Override
public View getView (int position, View convertView,
ViewGroup parent) {
View view = convertView;
if(convertView==null)
view = inflater.inflate(R.layout.row, null);

TextView text1 = (TextView)view.findViewById(R.id.text1);
TextView text2 = (TextView)view.findViewById(R.id.text2);
SystemProperties myObj = myList.get(position);
text1.setText(String.valueOf(myObj.getPropertiesName()));
text2.setText(myObj.getpPoperties());
return view;
}
}
}

For thw XMLs of the main layout and the ListView row layout, refer to the post "Create custom ListView of custom object".

Next: Implement auto scroll marquee TextView in ListView

0 komentar:

Posting Komentar

Copyright © 2012 Codding News All Right Reserved