How to refresh list View in android? - android

I am using .net web services. I am trying to get list in ListView. Right now it is showing me the first list, but when I am trying to get it again using the same method it is giving me a response in log but not displaying in list.
I have used mAdapter.notifyDataSetChanged(); in my Adapter but it's not working. Please help. thanks
My code:
Intent mIntent = getIntent();
mIntent.getStringExtra("folder_name");
Id = mIntent.getStringExtra("folder_ID");
mIntent.getStringExtra("item_parent");
User_ID = mIntent.getStringExtra("User_ID");
subfolderTreedata();
}
public void subfolderTreedata() {
try {
--------
--------
-------- //some code here...
SoapObject SubfolderResponse = (SoapObject)envelope.getResponse();
Log.i("SubFolders", SubfolderResponse.toString());
String File_Ext=" ";
subfoldersitem = new String[SubfolderResponse.getPropertyCount()];
System.out.println(subfoldersitem.length);
for(int i=0; i < SubfolderResponse.getPropertyCount(); i++) {
SoapObject SingleSubFolder = (SoapObject)SubfolderResponse.getProperty(i);
subfoldersitem[0] = SingleSubFolder.getProperty(1).toString();
subfoldersitem[1] = SingleSubFolder.getProperty(0).toString();
subfoldersitem[2] = SingleSubFolder.getProperty(3).toString();
if(KEY_SUBJECTTYPE.equalsIgnoreCase("Folder")) {
item = new FolderList(Folderimages[0], subfoldersitem[0], subfoldersitem[1], subfoldersitem[2]);
Subfolderdata.add(item);
} else{
StringTokenizer tokens = new StringTokenizer(Name, ".");
#SuppressWarnings("unused")
String first_string = tokens.nextToken();
File_Ext = tokens.nextToken();
if(File_Ext.equalsIgnoreCase("TIF")) {
item = new FolderList(TIFimages[0], subfoldersitem[0], subfoldersitem[1], subfoldersitem[2]);
Subfolderdata.add(item);
} else {
item = new FolderList(noImage[0], subfoldersitem[0], subfoldersitem[1], subfoldersitem[2]);
Subfolderdata.add(item); }
}
}
subfolderslistview = (ListView)findViewById(R.id.subfolderslistview);
mAdapter = new LazyAdapter(this, R.layout.jpg_row, Subfolderdata);
subfolderslistview.setAdapter(mAdapter);
mAdapter.notifyDataSetChanged();
subfolderslistview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
LazyAdapter ca = (LazyAdapter)parent.getAdapter();
FolderList item_name = (FolderList)ca.getItem(position);
FolderList DocumentID = (FolderList)ca.getItem(position);
FolderList type = (FolderList)ca.getItem(position);
Intent mIntent = new Intent();
mIntent.putExtra("item_name", item_name.folder_name);
mIntent.putExtra("item_id", DocumentID.ID);
mIntent.putExtra("item_type", type.type);
mIntent.getStringExtra("item_name");
String Type = mIntent.getStringExtra("item_type");
Log.i("Type", Type);
if(Type.equalsIgnoreCase("Folder")){
Id = mIntent.getStringExtra("item_id");
mAdapter.notifyDataSetChanged();
subfolderTreedata();
} else {
Intent i = new Intent(getApplicationContext(), Display_image.class);
i.putExtra("item_name", item_name.folder_name);
i.putExtra("ID", DocumentID.ID);
i.putExtra("item_type", type.type);
i.putExtra("User_ID",User_ID);
i.getStringExtra("item_name");
Id = i.getStringExtra("ID");
i.getStringExtra("item_type");
Log.i("id", Id);
startActivity(i);
}
}
});

public void list() {
mAdapter = new LazyAdapter(this, R.layout.jpg_row, Subfolderdata);
subfolderslistview.setAdapter(mAdapter);
}
Use like this list();.
Call this method from Where do you want.

Related

this is giving error.plz suggest some answer

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
DataModel dataModel = (DataModel) parent.getItemAtPosition(position);
// Log.d("imageId--- ", String.valueOf(dataModel.getImageid()));
//
// Intent intent = new Intent(MainActivity.this, DetailActivity.class);
// intent.putExtra(IntentKeyConstant.INTENT_KEY_ITEM_DRAWABLE_ID, dataModel.getImageid());
// intent.putExtra(IntentKeyConstant.INTENT_KEY_ITEM_NAME, dataModel.getName());
// intent.putExtra(IntentKeyConstant.INTENT_KEY_ITEM_COST, dataModel.getPrice());
// intent.putExtra(IntentKeyConstant.INTENT_KEY_ITEM_NAME_DETAIL, dataModel.getDetailName());
// intent.putExtra(IntentKeyConstant.INTENT_KEY_ITEM_DETAIL, dataModel.getDetail());
Intent intent1 = new Intent(MainActivity.this, DetailActivity.class);
intent1.putExtra("keyclassobject",dataModel);
startActivity(intent1);
}
});
another activity is
final DataModel dataModel = (DataModel) intent.getSerializableExtra("keyclass");
// drawableId = intent.getIntExtra(IntentKeyConstant.INTENT_KEY_ITEM_DRAWABLE_ID, 0);
// String itemName = intent.getStringExtra(IntentKeyConstant.INTENT_KEY_ITEM_NAME);
// String itemCost = intent.getStringExtra(IntentKeyConstant.INTENT_KEY_ITEM_COST);
// String itemNameDetail = intent.getStringExtra(IntentKeyConstant.INTENT_KEY_ITEM_NAME_DETAIL);
// String itemDetail = intent.getStringExtra(IntentKeyConstant.INTENT_KEY_ITEM_DETAIL);
//Log.d("name", itemName);
// Log.d("itemcost",itemCost);
//Log.d("imageId ",String.valueOf(drawableId)+ "-"+Integer.parseInt(drawableId));
Typeface typeface=Typeface.createFromAsset(getAssets(),"fonts/fontawesome-webfont.ttf");
tv_item_rupee_symbol=(TextView)findViewById(R.id.tv_item_rupee_symbol);
tv_item_rupee_symbol.setTypeface(typeface);
toolbar_title.setText(dataModel.getName());
iv_item.setImageResource(dataModel.getImageid());
tv_item_name_detail.setText(dataModel.getDetailName());
tv_item_rupee_symbol.setText("\uf156");
tv_item_cost.setText(dataModel.getPrice());
tv_item_detail.setText(dataModel.getDetail());
Intent intent = getIntent();
If your extra data is represented as strings, then you can use intent.getStringExtra(String name) method. In your case:
String id = intent.getStringExtra("keyclassobject");

how to update listview in Popup window in Android

I have one TextView with title All Post.I'm displaying the popup window click on TextView and populate list view with two items in popup window.And trying to implement this - whenever click on list view item that item will be display in TextView and previous TextView title All Post will be add in listview in the place of clickable item.But my listview is not update on dismiss the popup window.
Here is my code of popup window.
String[] values = new String[] { "All Post", "My Post", "R-Post"};
ArrayList<String> filter_PostType = new ArrayList<String>();
str_LastSelectionValue = sharedPreferences.getString("filter_post_title", "");
if(str_LastSelectionValue.equals("NoVal"))
{
editor.putString("filter_post_title","All Post");
editor.commit();
}
else
{
str_LastSelectionValue = sharedPreferences.getString("filter_post_title", "");
}
textSearch.setText(str_LastSelectionValue);
Log.e("", " textSearch.getText().toString().trim() = " +
textSearch.getText().toString().trim());
textSearch.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v)
{
popup_searchview = new PopupWindow(CustomActionActivity.this);
View layout = getLayoutInflater().inflate(R.layout.allpostsearch_popup, null);
popup_searchview.setContentView(layout);
popup_searchview.setHeight(220);
popup_searchview.setWidth(250);
popup_searchview.setOutsideTouchable(true);
popup_searchview.setFocusable(true);
popup_searchview.setBackgroundDrawable(new BitmapDrawable());
popup_searchview.showAsDropDown(v);
filter_list = (ListView) layout.findViewById(R.id.filter_ListView);
arr = new ArrayList<String>(Arrays.asList(values));
filter_PostType.clear();
for (int i = 0; i < arr.size(); i++) {
String str_PostType = arr.get(i);
if (!str_PostType.equals(str_LastSelectionValue)) {
filter_PostType.add(str_PostType);
}
}
filterPost_adapter = new ArrayAdapter<String>(getApplicationContext(), R.layout.filter_popup_list_item, R.id.text_filter_title, filter_PostType);
filter_list.setAdapter(filterPost_adapter);
filter_list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String selectedFromList = (filter_list.getItemAtPosition(position).toString());
editor.putString("filter_post_title", selectedFromList);
editor.commit();
textSearch.setText(selectedFromList);
popup_searchview.dismiss();
removeItemFromList(position);
filterPost_adapter.notifyDataSetChanged();
arr = new ArrayList<String>(Arrays.asList(values));
filter_PostType.clear();
for (int i = 0; i < arr.size(); i++)
{
String str_PostType = arr.get(i);
if (!str_PostType.equals(textSearch.getText().toString().trim()))
{
filter_PostType.add(str_PostType);
int filterSize = filter_PostType.size();
Log.e(" UnMatched ", " In ListItem Click filterSize = " + filterSize);
}
}
filterPost_adapter = new ArrayAdapter<String>(getApplicationContext(), R.layout.filter_popup_list_item, R.id.text_filter_title, filter_PostType);
filter_list.setAdapter(filterPost_adapter);
}
});
}
});
Use something like this:
String[] values = new String[] { "All Post", "My Post", "R-Post"};
ArrayList<String> filter_PostType = new ArrayList<String>();
str_LastSelectionValue = sharedPreferences.getString("filter_post_title", "NoVal");
if(str_LastSelectionValue.equals("NoVal"))
{
editor.putString("filter_post_title","All Post");
editor.commit();
}
else
{
str_LastSelectionValue = sharedPreferences.getString("filter_post_title", "");
}
textSearch.setText(str_LastSelectionValue);
Log.e("", " textSearch.getText().toString().trim() = " +
textSearch.getText().toString().trim());
textSearch.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v)
{
str_LastSelectionValue = sharedPreferences.getString("filter_post_title", "");
popup_searchview = new PopupWindow(CustomActionActivity.this);
View layout = getLayoutInflater().inflate(R.layout.allpostsearch_popup, null);
popup_searchview.setContentView(layout);
popup_searchview.setHeight(220);
popup_searchview.setWidth(250);
popup_searchview.setOutsideTouchable(true);
popup_searchview.setFocusable(true);
popup_searchview.setBackgroundDrawable(new BitmapDrawable());
popup_searchview.showAsDropDown(v);
filter_list = (ListView) layout.findViewById(R.id.filter_ListView);
if(!filter_PostType.isEmpty())
filter_PostType.clear();
for (int i = 0; i < values.length; i++) {
if (!values[i].equals(str_LastSelectionValue)) {
filter_PostType.add(values[i]);
}
}
filterPost_adapter = new ArrayAdapter<String>(getApplicationContext(), R.layout.filter_popup_list_item, R.id.text_filter_title, filter_PostType);
filter_list.setAdapter(filterPost_adapter);
filter_list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String selectedFromList = (filter_list.getItemAtPosition(position).toString());
editor.putString("filter_post_title", selectedFromList);
editor.commit();
str_LastSelectionValue=selectedFromList;
textSearch.setText(selectedFromList);
popup_searchview.dismiss();
}
});
}
});
}
You don't need to change your adapter after user selects something because the next time user clicks the textview, you are making a new popup window with a new adapter.
If there is anything you don't understand, leave a comment.
Hope this helps :)

Android code to check condition if variable exits or not from rest api

Hi my question is regarding Android programming. I have a varible called company_name which i must fetch from Rest api and print on my Android device.
But my question is i must check for a condition if the company_name(variable) exists or not, if not then it must skip it and print others.
My code:
try {
JSONObject jsonObject = new JSONObject(response);
final JSONArray jsonArray = jsonObject.getJSONArray("data");
final List<String> names = new ArrayList<String>();
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject inter = new JSONObject(jsonArray.get(i).toString());
ApplyJobItem jobItem = new ApplyJobItem();
jobItem.setCompanyName(inter.getString("company_name"));
jobItem.setIndustry(inter.getString("industry_selected"));
jobItem.setJobPost(inter.getString("title"));
jobItem.setJobType(inter.getString("type"));
jobItem.setJobId(inter.getString("_id"));
//jobItem.setJobCity(inter.getString("location"));
//jobItem.setJobSalary(inter.getJSONArray("salary_range").getString(0) + " to " + inter.getJSONArray("salary_range").getString(1));
jobItem.setdescription(inter.getString("description"));
names.add(inter.getString("company_name"));
names.add(inter.getString("industry_selected"));
names.add(inter.getString("title"));
// names.add(inter.getString("description"));
applyJobItems.add(jobItem);
android.util.Log.e("Company name", inter.getString("company_name"));
}
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(apply.this, android.R.layout.simple_list_item_2, names);
final boolean isCareer = jsonObject.getBoolean("careerUpdated");
android.util.Log.e("Entered ", "1");
applyJobAdapter = new ApplyJobAdapter(apply.this, applyJobItems, isCareer);
android.util.Log.e("Entered ", "2");
ListView mylistview = (ListView) findViewById(R.id.list);
android.util.Log.e("Entered ", "3");
mylistview.setAdapter(applyJobAdapter);
android.util.Log.e("Entered ", "4");
mylistview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(getApplicationContext(), applypage.class);
intent.putExtra("careerUpdate", isCareer);
intent.putExtra("position", position);
startActivity(intent);
}
});
Use inter.has("company_name") to check if company_name key exist or not in your JSONObject.
//...
jobItem.setdescription(inter.getString("description"));
if(inter.has("company_name")){
names.add(inter.getString("company_name"));
names.add(inter.getString("industry_selected"));
names.add(inter.getString("title"));
applyJobItems.add(jobItem);
android.util.Log.e("Company name", inter.getString("company_name"));
} else {
android.util.Log.e("Company name", "Not exist");
}
You can check in your JsonObject if 'company_name' key exists or not -
if(inter.has("company_name"))
//Continue you processing
else
//Stop reading the data

listview onitemclick can't refer

So, i plan to make a listview that if one of its item clicked, it goes to other activity. it's obvious that i want to use intent with extras.
But in my code, the extras is not reffering.
I tried to refer the extras with exact value, it worked, but if I put the extras to refer my object/variable, it won't work.
So far my code goes like this
public class bulan extends ListActivity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tagihan_list);
Intent i = getIntent();
final String no_pel = i.getStringExtra("no_pel");
List<NameValuePair> pairs = new ArrayList<NameValuePair>();
pairs.add(new BasicNameValuePair("nopel", no_pel));
ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
JSON json = new JSON();
JSONObject jobj = json.getJSON("http://10.0.2.2/KP/tagihan.php", pairs);
try {
int length = jobj.getInt("panjang");
for(int n = 1; n <= length; n++){
String m = Integer.toString(n);
JSONObject row = jobj.getJSONObject(m);
String id_tagihan = "No. tagihan : " + row.getString("id");
String bulan = row.getString("bulan");
String tahun = row.getString("tahun");
String tagihan = "Rp. " + row.getString("tagihan");
String status = row.getString("status");
HashMap<String, String> map = new HashMap<String, String>();
map.put("id_tagihan", id_tagihan);
map.put("bulan", bulan);
map.put("tahun", tahun);
map.put("tagihan", tagihan);
map.put("status", status);
list.add(map);
}
} catch (JSONException e) {
e.printStackTrace();
}
ListAdapter adapter = new SimpleAdapter(this, list, R.layout.list,
new String[] {"id_tagihan","bulan","tahun","tagihan","status"}, new int[] {
R.id.id_tagihan, R.id.bulan, R.id.tahun, R.id.tagihan, R.id.status});
setListAdapter(adapter);
ListView lv = getListView();
lv.setOnItemClickListener(new OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
// TODO Auto-generated method stub
String sid = ((TextView) view.findViewById(R.id.id_tagihan)).getText().toString();
String snopel = no_pel;
Intent i = new Intent(bulan.this, rincian.class);
i.putExtra("id", sid);
i.putExtra("no_pel", snopel);
startActivity(i);
}
});
}
}
Like I explained before, it's this part
Intent i = new Intent(bulan.this, rincian.class);
i.putExtra("id", sid);
i.putExtra("no_pel", snopel);
startActivity(i);
that change my code to work if I write them like this
Intent i = new Intent(bulan.this, rincian.class);
i.putExtra("id", "00001");
i.putExtra("no_pel", "0620120001");
startActivity(i);
try
Intent i = new Intent(bulan.this, rincian.class);
Bundle b=new Bundle();
b.putString("id",sid);
b.putString("no_pel",snopel);
i.putExtras(b);
startActivity(i);
In your rincian class get the values as follows.
b=getIntent().getExtras();
String fri_id=b.getString("id");
String nopel=b.getString("no_pel");

Saving Android Preferences

Hi, I have a listview and i'm wanting it so that when you press on an item it stores your selection in shared preferences. The purpose for this is so that next time I open the app it skips the selection process.
So what I'm wanting to know is how do I incorporate this function into my code?
Here is my code so far:
public class SelectTeamActivity extends ListActivity {
public String fulldata = null;
public String position = null;
public String divisionName= null;
public List<String> teamsList = null;
public String divName = null;
protected void loadData() {
fulldata = getIntent().getStringExtra("fullData");
position = getIntent().getStringExtra("itemIndex");
Log.v("lc", "selectteamActivity:" + fulldata);
Log.v("lc", "position:" + position);
int positionInt = Integer.parseInt(position);
try{
JSONObject obj = new JSONObject(fulldata);
teamsList = new ArrayList<String>();
JSONObject objData = obj.getJSONObject("data");
JSONArray teamInfoArray = objData.getJSONArray("structure");
for(int r = 0; r < teamInfoArray.length(); r++ ){
JSONObject teamFeedStructureDict = teamInfoArray.getJSONObject(r);
JSONArray teamStructureArray =
(JSONArray) teamFeedStructureDict.get("divisions");
JSONObject teamFeedDivisionsDictionary =
teamStructureArray.getJSONObject(positionInt);
divName = teamFeedDivisionsDictionary.getString("name");
JSONArray teamNamesArray =
(JSONArray) teamFeedDivisionsDictionary.get("teams");
for(int t = 0; t < teamNamesArray.length(); t++){
JSONObject teamNamesDict = teamNamesArray.getJSONObject(t);
teamsList.add(teamNamesDict.getString("name"));
}
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.selectact);
loadData();
TextView headerText = (TextView) findViewById(R.id.header_text);
headerText.setText(divName);
TextView redHeaderText = (TextView) findViewById(R.id.redheadertext);
redHeaderText.setText("Please select your team:");
setListAdapter( new ArrayAdapter<String>(this, R.layout.single_item,
teamsList));
ListView list = getListView();
list.setTextFilterEnabled(true);
}
#Override
protected void onListItemClick (ListView l, View v, int position, long id) {
Intent intent = new Intent(this, HomeActivity.class);
String curPos = Integer.toString(position);
//or just use the position:
intent.putExtra("itemIndex", curPos);
intent.putExtra("fullData", fulldata); //or just the part you want
startActivity(intent);
}
}
In your Activity's onCreate():
SharedPreferences preferences = getSharedPreferences("preferences", MODE_WORLD_WRITEABLE);
In your onListItemClick():
preferences.edit().putInt("KEY", position).commit();
Everywhere in your project:
int position = preferences.getInt("KEY", -1);
(-1 is a default value that means when there is no value with the given key, return this value)

Categories

Resources