Bellow is several different styles to show data in listview, you can use it to select your desired style.
Just show it very simply, if you have suggestions, please contact us.
Soure code file have been attached.
Bellow is several different styles to show data in listview, you can use it to select your desired style.
Just show it very simply, if you have suggestions, please contact us.
Soure code file have been attached.
1. App crashes when it starts, because I make a error to set TextView.textcolor to a String resource Value, it should be a drawable resource value
1 2 3 4 5 6 7 8 |
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="mainbtnlogin">Login</string> <drawable name="general_back_color">#FFFFFF</drawable> //this is right to assign to a color type variable <string name="general_text_color">#000000</string> //this is wrong to assign to a color type variable <drawable name="general_linkable_color">#0000FF</drawable> <string name="value_zero">0</string> </resources> |
2. I wrote a function which used Context of the activity. But when I finished coding, eclipse gave an error message. After a about 1 hour research, I finally found I wrote the function in the subclass of the activity. After moving the function to the main class, it works.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
package em8.admin; //import a lot of packages public class Em8MainActivity extends Activity { /** Called when the activity is first created. */ @SuppressLint({ "ParserError", "NewApi" }) @Override public void onCreate(Bundle savedInstanceState) { setContentView(R.layout.main); ... } public class ShowStatisticsTask extends AsyncTask<String, integer, Long>{ LogResItem lrItem = null; SharedPreferences prefs = getSharedPreferences(ConstData.Settings_file_name, MODE_PRIVATE); @Override protected Long doInBackground(String... params) { } protected void onPostExecute(Long result) { } //It is difficult to find, right? public void showNotification(int icon,String tickertext,String title,String content){ Notification notification=new Notification(icon,tickertext,System.currentTimeMillis()); notification.defaults=Notification.DEFAULT_ALL; PendingIntent pt=PendingIntent.getActivity(this, 0, getIntent(), 0); notification.setLatestEventInfo(this,title,content,pt); notifyMgr.notify(notification_id, notification); } } } |
3. (be continued…)
When I gave a margin value 10 to TextView, the eclipse give a below error message.
error: Error: Integer types not allowed (at ‘layout_margin’ with value ’10’).
I searched on internet, and found can’t give a integer value to it, should use 10dip.
StrictMode is a developer tool which used to detect accident in program. For example, if you do internet access or local file operation in the UI directly, the strictmode mechanism will throw an error to the application.
StrictMode is used default since Android 3.0(HoneyComb).
example of a wrong case
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
public void downloadHtml(View view) throws IOException{ String response = ""; DefaultHttpClient client = new DefaultHttpClient(); HttpGet httpGet = new HttpGet("http://svn1.bcoder.com"); HttpResponse execute = client.execute(httpGet); InputStream content = execute.getEntity().getContent(); BufferedReader buffer = new BufferedReader(new InputStreamReader(content)); String s = ""; while ((s = buffer.readLine()) != null) { response += s; } TextView tv = (TextView)findViewById(R.id.textView1); tv.setText(response); } |
If you request an url like above, your app will crashes.
So the right thing we do this is we can run the internet request in thread(Threads, AsyncTask, Handler), I give a simple by AsyncTask.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
public void downloadHtml(View view) throws IOException{ DownloadHtmlTask task = new DownloadHtmlTask(); task.execute(new String[] { "http://svn1.bcoder.com" }); } private class DownloadHtmlTask extends AsyncTask<String, Void, String> { @Override protected String doInBackground(String... urls) { String response = ""; InputStream in = null; for (String url : urls) { DefaultHttpClient client = new DefaultHttpClient(); HttpGet httpGet = new HttpGet(url); try { URL urlget = new URL(url); HttpURLConnection conn = (HttpURLConnection)urlget.openConnection(); conn.connect(); in = (conn.getInputStream()); BufferedReader buffer = new BufferedReader(new InputStreamReader(in)); String s = ""; while ((s = buffer.readLine()) != null) { response += s; } } catch (Exception e) { e.printStackTrace(); } } return response; } @Override protected void onPostExecute(String result) { TextView textView = (TextView)findViewById(R.id.textView1);; textView.setText(result); } } |
It looks very troublesome when we just debug the program, so we can also use a simple code to disable ThreadPolicy to avoid errors.
1 2 3 4 5 6 7 |
public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build(); StrictMode.setThreadPolicy(policy); } |
…