Alternative Row Color For list view while using Json Data in Asyc..
This will give alternative color
This will give alternative color
import
android.content.Context;
import
android.view.View;
import
android.view.ViewGroup;
import
android.widget.SimpleAdapter;
public
class
SpecialAdapter
extends
SimpleAdapter {
private
int
[] colors =
new
int
[] {
0x30FF0000
,
0x300000FF
};
public
SpecialAdapter(Context context, List<HashMap<String, String>> items,
int
resource, String[] from,
int
[] to) {
super
(context, items, resource, from, to);
}
@Override
public
View getView(
int
position, View convertView, ViewGroup parent) {
View view =
super
.getView(position, convertView, parent);
int
colorPos = position % colors.length;
view.setBackgroundColor(colors[colorPos]);
return
view;
}
}
Here is Main List Activity
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import nanoakhi.rcfinternal.R;
import nanoakhi.rcfinternal.WelcomeScreen;
import nanoakhi.rcfinternal.salary.NotificationRCF;
import org.apache.http.NameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.Dialog;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;
public class AlbumsActivity extends ListActivity {
// Connection detector
ConnectionDetector cd;
// Alert dialog manager
AlertDialogManager alert = new AlertDialogManager();
// Progress Dialog
private ProgressDialog pDialog;
// Creating JSON Parser object
JSONParser jsonParser = new JSONParser();
ArrayList<HashMap<String, String>> albumsList;
// albums JSONArray
JSONArray albums = null;
// ALL JSON node names
// private static final String TAG_ID = "ALLOTTEE_CODE";
private static final String TAG_NAME = "DPT_NAME";
// private static final String TAG_SONGS_COUNT = "EMAIL_ID";
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
@SuppressLint("NewApi")
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.albums_activity);
getActionBar().setDisplayHomeAsUpEnabled(true);
cd = new ConnectionDetector(getApplicationContext());
// Check for internet connection
if (!cd.isConnectingToInternet()) {
// Internet Connection is not present
alert.showAlertDialog(AlbumsActivity.this,
"Internet Connection Error",
"Please connect to working Internet connection", false);
// stop executing code by return
return;
}
// Hashmap for ListView
albumsList = new ArrayList<HashMap<String, String>>();
// Loading Albums JSON in Background Thread
new LoadAlbums().execute();
// get listview
ListView lv = getListView();
/**
* Listview item click listener TrackListActivity will be lauched by
* passing album id
* */
lv.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View view, int arg2,
long arg3) {
// on selecting a single album
// TrackListActivity will be launched to show tracks inside the
// album
Intent i = new Intent(
getApplicationContext(),
nanoakhi.rcfinternal.departmentnumbers.TrackListActivity.class);
// send album id to tracklist activity to get list of songs
// under that album
String album_name = ((TextView) view
.findViewById(R.id.album_name)).getText().toString();
i.putExtra("album_name", album_name);
// String album_id = ((TextView)
// view.findViewById(R.id.album_id)).getText().toString();
// i.putExtra("album_id", album_id);
startActivity(i);
}
});
}
/**
* Background Async Task to Load all Albums by making http request
* */
class LoadAlbums extends AsyncTask<String, String, String> {
/**
* Before starting background thread Show Progress Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(AlbumsActivity.this, R.style.MyTheme);
pDialog.setTitle("Processing..");
pDialog.setMessage("Please wait.");
pDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
pDialog.setCancelable(true);
pDialog.show();
}
/**
* getting Albums JSON
* */
protected String doInBackground(String... args) {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
// getting JSON string from URL
String json = jsonParser.makeHttpRequest(URL_ALBUMS, "GET", params);
// Check your log cat for JSON reponse
Log.d("Albums JSON: ", "> " + json);
try {
albums = new JSONArray(json);
if (albums != null) {
// looping through All albums
for (int i = 0; i < albums.length(); i++) {
JSONObject c = albums.getJSONObject(i);
// Storing each json item values in variable
// String id = c.getString(TAG_ID);
String name = c.getString(TAG_NAME);
// String songs_count = c.getString(TAG_SONGS_COUNT);
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
// map.put(TAG_ID, id);
map.put(TAG_NAME, name);
// map.put(TAG_SONGS_COUNT, songs_count);
// adding HashList to ArrayList
albumsList.add(map);
}
} else {
Log.d("Albums: ", "null");
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
/**
* After completing background task Dismiss the progress dialog
* **/
protected void onPostExecute(String file_url) {
// dismiss the dialog after getting all albums
pDialog.dismiss();
// updating UI from Background Thread
runOnUiThread(new Runnable() {
public void run() {
/**
* Updating parsed JSON data into ListView
* */
SpecialAdapter adapter = new SpecialAdapter(
AlbumsActivity.this, albumsList,
R.layout.list_item_albums,
new String[] { TAG_NAME },
new int[] { R.id.album_name });
// updating listview
setListAdapter(adapter);
}
});
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main_coach, menu);
return true;
}
@Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
switch (item.getItemId()) {
case R.id.coaches:
// Intent not = new Intent(
// this, NotificationRCF.class);
// startActivity(not);
// overridePendingTransition(R.anim.activity_open_translate,R.anim.activity_close_scale);
// return true;
}
/*
* Intent demo11 = new Intent(HomeActivity.this, How_to_use.class);
* startActivity(demo11); return true;
*/
return super.onMenuItemSelected(featureId, item);
}
}
Post a Comment
0Comments