samedi 31 janvier 2015

how to set arraylist value in asynk task to another method


Vote count:

0




I want to set memberlist value which is in asynk task method into onresume method but i am getting null value how i get this value?


this is my code.



public class IndexBar extends Activity implements OnItemClickListener {

HashMap<Character, Integer> alphabetToIndex;
static String TAG = "IndexBar";
int number_of_alphabets = -1;
static IndexBarHandler handler;
private EditText editsearch;
private ArrayAdapter<String> adapter1;
public ArrayAdapter<String> mainadapter;
public ArrayList<String> memberlist;
public ArrayList<String> first;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.indexbarlayout);
editsearch = (EditText) findViewById(R.id.textview_header);
editsearch.addTextChangedListener(new TextWatcher() {


@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
// TODO Auto-generated method stub
mainadapter.getFilter().filter(s.toString());
}

@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub

}

@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub

}
});
getScreenHeight(this);
getScreenWidth(this);
handler = new IndexBarHandler(this);

}

protected void onResume() {
super.onResume();

/* populating the base listview */
CustomListView mainlistview = (CustomListView) findViewById(R.id.listView_main);
String main_list_array[] = getResources().getStringArray(
R.array.base_array);
if (main_list_array == null) {
Log.d(TAG, "Array of the main listview is null");
return;
}
mainadapter = new ArrayAdapter<String>(
getBaseContext(), R.layout.mainlistview_row/*
* android.R.layout.
* simple_list_item_1
*/,
main_list_array);
mainlistview.setAdapter(mainadapter);
populateHashMap();

}

public int convertDipToPx(int dp, Context context) { // 10dp=15px
Resources r = context.getResources();
float px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp,
r.getDisplayMetrics());
return (int) px;
}

public int convertPxtoDip(int pixel) { // 15px=23dp
float scale = getResources().getDisplayMetrics().density;
int dips = (int) ((pixel / scale) + 0.5f);
return dips;
}

/**
* Determines the width of the screen in pixels
*
* @return width
*/
public int getScreenWidth(Activity activity) {
Display display = activity.getWindowManager().getDefaultDisplay();
int width = display.getWidth();
Log.d(TAG, "Screen Width in pixels=" + width);
return width;
}

/**
* Determines the height of the screen in pixels
*
* @return height
*/
public int getScreenHeight(Activity activity) {
Display display = activity.getWindowManager().getDefaultDisplay();
int height = display.getHeight();
Log.d(TAG, "Screen Height in pixels=" + height);
return height;
}

public float pixelsToSp(Context context, Float px) {
float scaledDensity = context.getResources().getDisplayMetrics().scaledDensity;
Log.d(TAG, "GetTextSize in pixels=" + px + " In Sp="
+ (px / scaledDensity));
return px / scaledDensity;
}

public void onItemClick(AdapterView<?> arg0, View view, int arg2, long arg3) {

if (!(view instanceof TextView || view == null))
return;
TextView rowview = (TextView) view;

CharSequence alpahbet = rowview.getText();

if (alpahbet == null || alpahbet.equals(""))
return;

String selected_alpahbet = alpahbet.toString().trim();
Integer position = alphabetToIndex.get(selected_alpahbet.charAt(0));
Log.d(TAG, "Selected Alphabet is:" + selected_alpahbet
+ " position is:" + position);

ListView listview = (ListView) findViewById(R.id.listView_main);
listview.setSelection(position);
}

/**
* This populates the HashMap which contains the mapping between the
* alphabets and their relative position index.
*/
private void populateHashMap() {
alphabetToIndex = new HashMap<Character, Integer>();
String base_list[] = getResources().getStringArray(R.array.base_array);
int base_list_length = base_list.length;

for (int i = 0; i < base_list_length; i++) {
char firstCharacter = base_list[i].charAt(0);
boolean presentOrNot = alphabetToIndex.containsKey(firstCharacter);
if (!presentOrNot) {
alphabetToIndex.put(firstCharacter, i);
// Log.d(TAG,"Character="+firstCharacter+" position="+i);
}
}
number_of_alphabets = alphabetToIndex.size(); // Number of enteries in
// the map is equal to
// number of letters
// that would
// necessarily display
// on the right.

/*
* Now I am making an entry of those alphabets which are not there in
* the Map
*/
String alphabets[] = getResources().getStringArray(
R.array.alphabtes_array);
int index = -1;

for (String alpha1 : alphabets) {
char alpha = alpha1.charAt(0);
index++;

if (alphabetToIndex.containsKey(alpha))
continue;

/*
* Start searching the next character position. Example, here alpha
* is E. Since there is no entry for E, we need to find the position
* of next Character, F.
*/
for (int i = index + 1; i < 26; i++) { // start from next character
// to last character
char searchAlphabet = alphabets[i].charAt(0);

/*
* If we find the position of F character, then on click event
* on E should take the user to F
*/
if (alphabetToIndex.containsKey(searchAlphabet)) {
alphabetToIndex.put(alpha,
alphabetToIndex.get(searchAlphabet));
break;
} else if (i == 25) /*
* If there are no entries after E, then on
* click event on E should take the user to
* end of the list
*/
alphabetToIndex.put(alpha, base_list_length - 1);
else
continue;

}//
}//
}
public void getAllMember() {

new AsyncTask<Void, Void, String>() {
ProgressDialog mProgressDialog;
CustomListView mainlistview = (CustomListView) findViewById(R.id.listView_main);
public String firstname;

protected void onPostExecute(String result) {
mProgressDialog.dismiss();
memberlist = new ArrayList<String>();
first = new ArrayList<String>();

try {
JSONObject jsob = new JSONObject(result.toString());
if (jsob.getString("msg").equalsIgnoreCase("Success")) {
JSONArray datajson = jsob.getJSONArray("data");
for (int i = 0; i < datajson.length(); i++) {

JSONObject c = datajson.getJSONObject(i);
String id = c.getString("iMember_id");
firstname = c.getString("vUsername");
Log.d("firstname val", firstname);

memberlist.add(firstname);

}
mainadapter = new ArrayAdapter<String>(
getBaseContext(),
R.layout.mainlistview_row/*
* android.R.layout.
* simple_list_item_1
*/, memberlist);
mainlistview.setAdapter(mainadapter);

} else {
System.out.println("not a valid user");
}

} catch (Exception e) {
Log.e("login problem", "" + e);

}

}

private void startActivity(Intent i) {
// TODO Auto-generated method stub

}

@Override
protected String doInBackground(Void... arg0) {
// Creating service handler class instance
try {
HttpPost httppost1 = null;
HttpClient httpclient1 = new DefaultHttpClient();

httppost1 = new HttpPost(JsonKey.MAIN_URL);

// Add your data
List<NameValuePair> nameValuePairs1 = new ArrayList<NameValuePair>(
1);
nameValuePairs1.add(new BasicNameValuePair("action",
"GetAllMembers"));
httppost1.setEntity(new UrlEncodedFormEntity(
nameValuePairs1));
// Execute HTTP Post Request
HttpResponse response1 = httpclient1.execute(httppost1);
BufferedReader in1 = new BufferedReader(
new InputStreamReader(response1.getEntity()
.getContent()));
StringBuffer sb1 = new StringBuffer("");
String line1 = "";
while ((line1 = in1.readLine()) != null) {
sb1.append(line1);
}
in1.close();
Log.e(" Get All magazine original data", sb1.toString());
return sb1.toString();
} catch (Exception e) {
Log.e("Get All magazine response problem", "" + e);
return " ";
}
}

@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
mProgressDialog = new ProgressDialog(IndexBar.this);
mProgressDialog.setTitle("");
mProgressDialog.setCanceledOnTouchOutside(false);
mProgressDialog.setMessage("Please Wait...");
mProgressDialog.show();

}
}.execute();

}


}
the value of memberlist in getallmember is i want to set in
mainadapter = new ArrayAdapter<String>(
getBaseContext(), R.layout.mainlistview_row/*
* android.R.layout.
* simple_list_item_1
*/,
main_list_array);


in above code in place of main_array_list but doing so i am getting null pointer exception how i get this value?



asked 31 secs ago







how to set arraylist value in asynk task to another method

Aucun commentaire:

Enregistrer un commentaire