How to populate a ListView with Parse Array using ArrayAdapter - android

So I'm new to Android and I'm using Parse as my backend. I have an Array column in Parse called PrescriptionArray. I want to take the contents of this array (String values) and populate a listView with them. I think I'm only missing a couple of lines but not sure where? Any help would be greatly appreciated.. Thanks in advance!
public class PrescriptionsArrayActivity extends AppCompatActivity {
private ListView lvPrescriptions;
private EditText etMedicalID;
private ArrayAdapter<String> adapter ;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_prescriptions_array);
lvPrescriptions = (ListView) findViewById(R.id.lvPrescriptions);
etMedicalID = (EditText) findViewById(R.id.etMedicalID);
//Get the Medical ID number from previous activity
Bundle bundle = getIntent().getExtras();
String str = bundle.getString("MedicalID");
Toast.makeText(getBaseContext(), str, Toast.LENGTH_LONG).show();
etMedicalID.setText(str);
final List<String> arrayprescription = new ArrayList<String>();
final ArrayAdapter adapter = new ArrayAdapter(PrescriptionsArrayActivity.this, android.R.layout.simple_list_item_1, arrayprescription);
final ParseQuery<ParseObject> query = ParseQuery.getQuery("PrescriptionObject");
query.whereEqualTo("MedicalID", etMedicalID.getText().toString());
query.findInBackground(new FindCallback<ParseObject>() {
public void done(List<ParseObject> prescriptionList, ParseException e) {
if (e == null) {
for (int i = 0; i < prescriptionList.size(); i++) {
ParseObject p = prescriptionList.get(i);
if (p.getList("PrescriptionArray") != null) {
String s = arrayprescription.toString();
adapter.addAll(Arrays.asList(s));
}
}
lvPrescriptions.setAdapter(adapter);
}
}
});
}
}

Related

I need to show this data from Parser in a listview, how do I?

I need to show this data in a listview, it's relational data, I already know how to re-emerge, what I do not know is how it shows it, I'm doing it inside an activity.
final ListView listview = (ListView) findViewById(R.id.lvComentario);
final String[][] values = {new String[]{" linda", " bonita"}};
final ArrayList<String> list = new ArrayList<String>();
for (int i = 0; i < values[0].length; ++i) {
list.add(values[0][i]);
}
final StableArrayAdapter adapter = new StableArrayAdapter(this,
android.R.layout.simple_list_item_1, list);
listview.setAdapter(adapter);
comentario = (EditText) findViewById(R.id.text_comentario);
botaoSalvar = (Button) findViewById(R.id.button_salvar);
Intent i = getIntent();
obID = i.getStringExtra("imagem");
//exibirComentario();
ParseQuery<ParseObject> query = ParseQuery.getQuery("comentario");
query.whereEqualTo("parentesco", obID);
query.findInBackground(new FindCallback<ParseObject>() {
public void done(List<ParseObject> commentList, ParseException e) {
if (e == null) {
Log.d("score", "no doctor available: ");
} else {
Log.w("Parse query", e.getMessage());
}
adapter.notifyDataSetChanged();
}
});
You need to set adapter after you receive data from parse.
final ListView listview = (ListView) findViewById(R.id.lvComentario);
ParseQuery<ParseObject> query = ParseQuery.getQuery("comentario");
query.whereEqualTo("parentesco", obID);
query.findInBackground(new FindCallback<ParseObject>() {
public void done(List<ParseObject> commentList, ParseException e) {
if (e == null) {
Log.d("score", "no doctor available: ");
} else {
Log.w("Parse query", e.getMessage());
//Use your custom adapter according to your comment list or design
final StableArrayAdapter adapter = new StableArrayAdapter(this,android.R.layout.simple_list_item_1, commentList);
listview.setAdapter(adapter);
}
}

How to display name in spinner dynamically from server

Hi Now I'm retrieving data to spinner dynamically, but now it displaying some id's but those Id details are stored in other table, I want display those details in the spinner instead of id. And if I select particular product in spinner according that product details should display in list.
I'm using Retrofit method for retrieving data from server
package cfirst.live.com.activity;
public class Pos_outlet extends AppCompatActivity implements RestCallback,View.OnClickListener{
Spinner spinner;
ArrayList<String> products;
String numberAsString, product_name;
int i;
private int cartProductNumber = 0;
String[] items;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.pos_outlet);
sharedPreference = new MySharedPreference(Pos_outlet.this);
GsonBuilder builder = new GsonBuilder();
gson = builder.create();
initViews();
}
private void initViews() {
row1 =(TableRow)findViewById(R.id.row1);
spinner=(Spinner)findViewById(R.id.spinner);
POSStoreID = (TextView) findViewById(R.id.POSStoreID);
POSLocationID = (TextView) findViewById(R.id.POSLocationID);
Intent intent = getIntent();
id = intent.getStringExtra("id");
index_id= intent.getStringExtra("index_id");
callStoreDetaislsAPI();
callSmbProductsAPI();
getProductAPI();
}
// **Using this api I'm setting id'd to spinner**
private void getProductAPI() {
HashMap<String, String> map = new HashMap<String, String>();
map.put("store", index_id);
Toast.makeText(getApplicationContext(),index_id, Toast.LENGTH_LONG).show();
RestService.getInstance(Pos_outlet.this).getproductlist(map, new MyCallback<List<PosmultistoresModel>>(Pos_outlet.this,
Pos_outlet.this, true, "Finding products....", GlobalVariables.SERVICE_MODE.GET_PRODUCTS));
}
// **this API have product Id's Details**
private void callSmbProductsAPI() {
HashMap<String, String> map = new HashMap<String, String>();
map.put("index_id", product);
//Toast.makeText(getApplicationContext(),added_by, Toast.LENGTH_LONG).show();
RestService.getInstance(Pos_outlet.this).getSmbProduct(map, new MyCallback<List<PosSmbProductModel>>(Pos_outlet.this,
Pos_outlet.this, true, "Fetching details....", GlobalVariables.SERVICE_MODE.SMB_PRODUCT));
}
#Override
public void onClick(View v) {
switch(v.getId()) {
case R.id.AddtoBasket:
callPosProductsAPI();
callSmbProductsAPI();
break;
}
}
#Override
public void onFailure(Call call, Throwable t, GlobalVariables.SERVICE_MODE mode) {
//Toast.makeText(getApplication(),"failure",Toast.LENGTH_LONG).show();
}
#Override
public void onSuccess(Response response, GlobalVariables.SERVICE_MODE mode) {
switch (mode) {
//** I'm setting data to spinner **
case GET_PRODUCTS:
try {
List<PosmultistoresModel> businessgroups = (List<PosmultistoresModel>) response.body();
product_name = businessgroups.get(0).getProduct();
List<PosmultistoresModel> list=null;
for(i=0;i<businessgroups.size();i++)
{
list=businessgroups;
}
items = new String[list.size()];
for(int i=0; i<businessgroups.size(); i++){
//Storing names to string array
items[i] = list.get(i).getProduct();
}
ArrayAdapter<String> adapter1;
adapter1 = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_spinner_dropdown_item, items);
//setting adapter to spinner
spinner.setAdapter(adapter1);
} catch (Exception e) {
e.printStackTrace();
}
break;**
// ** Product id details (title, image, etc) Api
case SMB_PRODUCT:
try {
ArrayList<PosSmbProductModel> products = (ArrayList<PosSmbProductModel>) response.body();
//Product_id = products.get(0).getProduct();
// for (int i = 0; i < products(); i++) {
Title = products.get(0).getTitle();
productname1.setText(Title);
imageid12 = products.get(0).getMain_image();
Picasso.with(this).load("https://www.consumer1st.in/pre_production/uploads/" + imageid12).into(imageid1);
}
// }
catch(Exception e)
{
e.printStackTrace();
}
break;
}
}
}
I am assuming that your product_name = businessgroups.get(0).getProduct(); is getting a pruduct.
First of all please remove this part
product_name = businessgroups.get(0).getProduct();
List<PosmultistoresModel> list=null;
for(i=0;i<businessgroups.size();i++)
{
list=businessgroups;
}
items = new String[list.size()];
for(int i=0; i<businessgroups.size(); i++){
//Storing names to string array
items[i] = list.get(i).getProduct();
}
then can you try
ArrayList<String> productList = new ArrayList<>();
for (int i = 0; i < businessgroups.size(); i++) {
productList.add(businessgroups.get(i).getProduct());
}
And finally
adapter1 = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_spinner_dropdown_item, productList);

arraylist<object > received in activity different from the one sent

I have database 1 contains all books data (name,id,description , and so on)
and i have another database contains id of books i added to favorite
i get the data in asynctask
first i get all books data from the first database and add them in Arraylist<Book> then i get the ids of favorite books ids from the second database and add them in Arraylist<Integer> and then compare and add a parameter in books to 1 if it's in favourite
#Override
protected ArrayList<Book> doInBackground(Void... params) {
ArrayList<Book> bookList = new ArrayList<>();
ArrayList<Integer> favlist = new ArrayList<>();
ArrayList<Integer> downlist = new ArrayList<>();
try {
if (mDBAdapter != null) {
mDBAdapter.createDataBase();
mDBAdapter.openDataBase();
bookList = mDBAdapter.selectAllBooks();
}
} catch (IOException ioe) {
throw new Error("Unable to create database");
}
if (mAnnotationDBAdapter!=null) {
favlist = mAnnotationDBAdapter.selectAllFavourite();
}
for (int j = 0; j <bookList.size() ; j++) {
for (int i = 0; i <favlist.size() ; i++) {
if (bookList.get(j).getId()==favlist.get(i))
bookList.get(j).setInFavourite(1);
}
}
return bookList;
}
#Override
protected void onPostExecute(ArrayList<Book> books) {
super.onPostExecute(books);
if (books != null && books.size() > 0) {
Intent mainActivityintent = new Intent(mContext, Main2Activity.class);
mainActivityintent.putExtra(AppConstance.ALL_BOOKS_KEY,books);
mContext.startActivity(mainActivityintent);
}
}
here books list is ok and has infavourite parameter = 1 if the book is in favorite table
but when i receive it in main activity
i got infavourite = 0
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
PublicMethod.mDBAdapter = new AnnotationDBAdapter(this);
Intent intent = getIntent();
if (intent != null) {
if (intent.hasExtra(AppConstance.ALL_BOOKS_KEY)) {
mBooks = intent.getParcelableArrayListExtra(AppConstance.ALL_BOOKS_KEY) ;
//here all book have infavourite = 0
}
}
I think you need to use Intent.putParcelableArrayListExtra(String name, ArrayList<? extends Parcelable> value)

List doesn't show data

I have strings stored (objectIDs) in my internal storage. When I want to display the list of the IDs saved it works, however when I try to get the Names from Parse.com It doesn't show up on the list.
This shows ObjectIds:
public class UserListForHistory extends ListActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user_list_for_history);
SharedPreferences settings = getSharedPreferences("NotificationIDs", Context.MODE_PRIVATE);
Set<String> myStrings = settings.getStringSet("myStrings", new HashSet<String>());
ListView idList;
List<String> idListData;
idListData = new ArrayList<String>();
idList = (ListView) findViewById(android.R.id.list);
for(String userIds : myStrings){
idListData.add(userIds);
}
ArrayAdapter<String> str = new ArrayAdapter<String>(getBaseContext(), android.R.layout.simple_list_item_1, idListData);
idList.setAdapter(str);
}
However when add this to "for" It doesn't display anything in the list! But it does display all on the names in logcat. Anyone see the problem?
public class UserListForHistory extends ListActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user_list_for_history);
SharedPreferences settings = getSharedPreferences("NotificationIDs", Context.MODE_PRIVATE);
Set<String> myStrings = settings.getStringSet("myStrings", new HashSet<String>());
final ListView idList;
final List<String> idListData;
idListData = new ArrayList<>();
idList = (ListView) findViewById(android.R.id.list);
for(String userIds : myStrings) {
ParseQuery<ParseObject> query = ParseQuery.getQuery("_User");
query.getInBackground(userIds, new GetCallback<ParseObject>() {
#Override
public void done(ParseObject parseObject, ParseException e) {
String name = parseObject.getString("fullname");
Log.d("These are the names: ", name);
idListData.add(name);
}
});
}
ArrayAdapter<String> str = new ArrayAdapter<>(getBaseContext(), android.R.layout.simple_list_item_1, idListData);
idList.setAdapter(str);
}
The calls inside this for loop:
for(String userIds : myStrings) {
query.getInBackground(userIds, new GetCallback<ParseObject>() {
#Override
public void done(ParseObject parseObject, ParseException e) {
String name = parseObject.getString("fullname");
Log.d("These are the names: ", name);
idListData.add(name);
}
});
}
are asynchronous, which means that idListData is still empty when your code reaches the following lines:
ArrayAdapter<String> str = new ArrayAdapter<>(getBaseContext(), android.R.layout.simple_list_item_1, idListData);
idList.setAdapter(str);
To fix this, you could do something like the following:
final ArrayAdapter<String> str = new ArrayAdapter<>(getBaseContext(), android.R.layout.simple_list_item_1, new ArrayList<>());
idList.setAdapter(str);
for(String userIds : myStrings) {
query.getInBackground(userIds, new GetCallback<ParseObject>() {
#Override
public void done(ParseObject parseObject, ParseException e) {
String name = parseObject.getString("fullname");
Log.d("These are the names: ", name);
str.add(name);
}
});
}
In this code, the adapter is associated with the list synchronously, and asynchronously-retrieved data is then added to the adapter once it is available. Your exact implementation may vary based on how often you are executing this set of queries, but the code above (not compiled, so may contain typos) should illustrate the general principle for you.

Working tweet parser but with empty results

My task is to create app that could get latest tweets from x account.
Here is my code what I've made. However I get result (more than 0) only if i set (as search string) my account name. Otherwise I get nothing. Could someone explain me why? I will appreciate for any help.
private ListView lv;
private EditText et;
private ArrayAdapter<String> adapter;
String[] values;
private Token token = null;
private Credential c = null;
private UserAccountManager m = null;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv = (ListView) findViewById(R.id.listView1);
et = (EditText) findViewById(R.id.editText1);
et.setText("twapime");
doRest();// first run, initialization, first search
}
private void doRest() {
initAccount();
initSearching();
}
private void initSearching() {
ArrayList<String> listax = getSearchResults(et.getText().toString());
initListViewAdapter(listax);
}
private void initListViewAdapter(ArrayList<String> listax) {
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, android.R.id.text1, listax);
lv.setAdapter(adapter);
}
private void initAccount() {
token = new Token("xxxx",
"xxxx");
c = new Credential("xxxx",
"xxxx", token);
m = UserAccountManager.getInstance(c);
}
private ArrayList<String> getSearchResults(String userTwitter) {
ArrayList<String> lista = new ArrayList<String>();
try {
if (m.verifyCredential()) {
SearchDevice sd = SearchDevice.getInstance();
Query q1 = QueryComposer.from(userTwitter);
Tweet[] ts = sd.searchTweets(q1);
System.out.println(ts.length);
for (int i = 0; i < ts.length; i++) {
lista.add(ts[i].getString(MetadataSet.TWEET_CONTENT));
}
}
} catch (IOException e) {
e.printStackTrace();
} catch (LimitExceededException e) {
e.printStackTrace();
}
return lista;
}
public void btnSearcher(View w) {//OnClickListener
initSearching();
}
I think it happens when someone block public information data for untrusted user.

Categories

Resources