setAdapter is not calling getView. - android

I know this question has been asked several times, but the solutions have been specific to the askers' problems. Consequently, none of those solutions helped me, even though I tried following all of their suggestions.
So here goes.
I have a movies Activity like this. Notice that I have a MoviesAdapter inner class, that's supposed to populate the moviesDisplay ListView.
package com.example.midtermexam;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Comparator;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.database.DataSetObserver;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.Filter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
public class MoviesActivity extends Activity {
public String url;
public ListView moviesDisplay;
public static ArrayList<Movie> thisMovies;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_movies);
url=getIntent().getExtras().getString("url");
moviesDisplay = (ListView)findViewById(R.id.listView1);
new AsyncMoviesGet(this).execute(url);
}
public void populateListView()
{
Log.d("listview","adapter created");
Log.d("listview","Listview declared");
Log.d("listview","adapter populated");
moviesDisplay.setAdapter(new MoviesListAdapter());
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.movies, menu);
return true;
}
private class MoviesListAdapter extends ArrayAdapter<Movie> {
public MoviesListAdapter() {
super(MoviesActivity.this, R.layout.movies_activity_listview, thisMovies);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// Make sure we have a view to work with (may have been given null)
View movieView = convertView;
if (movieView == null) {
movieView = getLayoutInflater().inflate(R.layout.movies_activity_listview, parent, false);
}
// Find the car to work with.
return movieView;
}
}
}
The populateListView() method is called from an AsyncTask called AsyncMoviesGet, whose postExecute() looks like this.
#Override
protected void onPostExecute(ArrayList<Movie> result) {
if(result != null){
m.thisMovies = result;
m.populateListView();
Log.d("demo", result.toString());
} else{
Log.d("demo", "null result");
}
}
You can see the Log messages inside the populateListView() method. These 3 statements get executed. However, the setAdapter() function doesn't seem to call the "GetView" function.

Add stuff to ArrayAdapter at least, use mAdapter.addAll():
private MoviesListAdapter mAdapter;
. . . . . .
#Override
protected void onCreate(Bundle savedInstanceState) {
. . . . .
mAdapter = new MoviesListAdapter();
moviesDisplay.setAdapter(mAdapter);
}
. . . . .
protected void onPostExecute(ArrayList<Movie> result) {
if(result != null){
m.thisMovies = result;
mAdapter.clear();
mAdapter.addAll(result);
mAdapter.notifyDatasetInvalidated();
Log.d("demo", result.toString());
} else{
Log.d("demo", "null result");
}
}

You have to add a constructor to your Custom Adapter class, one that takes a context, resource id & a data structure containing the items you'd like to display, in this case your thisMovies.
public MoviesListAdapter(Context context, int resourceId,
ArrayList<String> viewItems)
{
super(context, resourceId, viewItems);
}
Then you have to construct your adapter based on the results you've received in your onPostExecute(). Before this is done, create an instance variable in the activity which will be used to store the adapter.
MoviesListAdapter mMovieAdapter = null;
Afterwards, construct it by changing this
moviesDisplay.setAdapter(new MoviesListAdapter());
to something like below
if(thisMovies != null && thisMovies.size() > 0) {
mMovieAdapter = new MoviesListAdapter(getApplicationContext(), R.layout.movies_activity_listview, thisMovies);
moviesDisplay.setAdapter(mMovieAdapter);
} else {
Log.w("MyApplication","onPostExecute() did not return any items!");
}

You should initialise and call adapter by this way..........this is just an e.g. you need to modify it according to your controls:
ListView lv = (ListView) v.findViewById(R.id.listview1);
ListViewAdapter adapter = new ListViewAdapter(container.getContext(),
android.R.layout.simple_list_item_1, R.id.textview1);
adapter.notifyDataSetChanged();
lv.setAdapter(adapter);
And inside the custom adapter class, its constructor should also have these kind of parameters:
public ListViewAdapter(Context context,int resource, int textViewResourceId, List<YourObjectType> items) {
super(context,resource,textViewResourceId, items);
this.context1 = context;
// TODO Auto-generated constructor stub
}

Related

When is onEvent called in a fragment and is there a way of determining when it can be called?

I was under the impression that onEvent can be triggered as soon as the fragment starts if it is placed in the onStart() and onResume method . Please have a look at my fragment below . That method prepareListData is ment to sort the list before I display it but the onEvent(contains list data from another fragment) for some reason is being called after prepareListData() which means there is not actual list and hence I get a null pointer . How can I make onEvent to be called before prepareListData
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.View;
//import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.Toast;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import com.androidquery.AQuery;
import com.xera.deviceinsight.Errors;
import com.xera.deviceinsight.Globals;
import com.xera.deviceinsight.R;
import com.xera.deviceinsight.api.OrganisationDeviceSensorsResult;
import com.xera.deviceinsight.api.Results;
import com.xera.deviceinsight.net.Claritech;
import com.xera.deviceinsight.net.ClaritechClient;
import com.xera.deviceinsight.receivers.IEvent;
import com.xera.deviceinsight.sensors.IotTabFragment;
import com.xera.deviceinsight.sensors.ItemClickedEvent;
import com.xera.deviceinsight.structs.DistanceUpdatedEvent;
import com.xera.deviceinsight.structs.HelloWorldEvent;
import com.xera.deviceinsight.structs.OrganisationDeviceSensorsEvent;
public class CostCentreListFragment extends Fragment {
public static final String TAG = Globals.TAG + ".ALF";
ViewPager mViewPager;
ExpandableListAdapter listAdapter;
ExpandableListView expListView;
List<String> listDataHeader;
HashMap<String, List<String>> listDataChild;
public static List<OrganisationDeviceSensorsResult> Items;
public String currentReportingGroup;
private ViewPager viewPager;
private IEvent onDeviceSensorsObtained;
private IEvent event;
public List sensorList;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
//EventBus eventBus = EventBus.getDefault();
//if (!eventBus.isRegistered(this)) eventBus.register(this);
View view = inflater.inflate(R.layout.layout_expandable, container, false);
AQuery aq = new AQuery(view);
expListView = (ExpandableListView) view.findViewById(R.id.lvExp);
//prepareListData();
prepareSensorListData();
listAdapter = new ExpandableListAdapter(this.getActivity(), listDataHeader, listDataChild);
// setting list adapter
listAdapter = new ExpandableListAdapter(this.getActivity(), listDataHeader, listDataChild);
expListView.setAdapter(listAdapter);
expListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
#Override
public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
//Nothing here ever fires
System.err.println("child clicked");
Toast.makeText(getActivity(), "child clicked", Toast.LENGTH_SHORT).show();
// Navigate to second tab
EventBus.getDefault().post(new ItemClickedEvent(IotTabFragment.TAB_SENSOR));
return true;
}
});
return view;
}
#Override
public void onStart() {
super.onStart();
EventBus.getDefault().register(this);
}
#Override
public void onPause() {
EventBus.getDefault().unregister(this);
super.onPause();
}
private void getDeviceSensorCostCentres() {
//int costCentreID =0
final Context context = this.getActivity();
ClaritechClient client = new ClaritechClient(context);
Claritech.api(context).getDeviceSensorCostCentres(client.getCostCentreID()).enqueue(new Callback<Results<OrganisationDeviceSensorsResult>>() {
#Override
public void onResponse(Call<Results<OrganisationDeviceSensorsResult>> call, Response<Results<OrganisationDeviceSensorsResult>> response) {
if (response.isSuccessful()) {
// Reload data source
Items.clear();
Items.addAll(response.body());
//ItemsAdapter.notifyDataSetChanged();
Log.i(TAG, "onResponse: ");
}
}
#Override
public void onFailure(Call<Results<OrganisationDeviceSensorsResult>> call, Throwable t) {
Errors.handleException(t);
}
});
}
public void onEvent(OrganisationDeviceSensorsEvent event){
String Tag ="";
sensorList = event.deviceSensors;
Log.i("EventBus",Tag);
//prepareSensorListData();
//Toast.makeText(getActivity(), event.deviceSensors, Toast.LENGTH_SHORT).show();
};
private void prepareSensorListData() {
final Context context = this.getActivity();
ClaritechClient client = new ClaritechClient(context);
Claritech.api(context).getDeviceSensorCostCentres(client.getCostCentreID()).enqueue(new Callback<Results<OrganisationDeviceSensorsResult>>() {
#Override
public void onResponse(Call<Results<OrganisationDeviceSensorsResult>> call, Response<Results<OrganisationDeviceSensorsResult>> response) {
if (response.isSuccessful()) {
// Reload data source
Items.clear();
Items.addAll(response.body());
//ItemsAdapter.notifyDataSetChanged();
Log.i(TAG, "onResponse: ");
String Tag ="";
listDataHeader = new ArrayList<String>();
listDataChild = new HashMap<String, List<String>>();
Log.i("EventBus",Tag);
//List sensorList = event.deviceSensors;
int check = sensorList.size();
int check2 = Items.size();
List<String> top250 = new ArrayList<String>();
for (int i = 1; i < Items.size(); i++) {
//if(i!=1 && sensorList.get(i).= currentReportingGroup)
// do a check here to see if the reporting group has changed or not
{
listDataHeader.add(Items.get(i).ReportingGroup);
top250.add(Items.get(i).SensorLocation);
// top250.add(String.valueOf(Items.get(i).SensorID));
}
}
listDataChild.put(listDataHeader.get(0), top250);
}
}
#Override
public void onFailure(Call<Results<OrganisationDeviceSensorsResult>> call, Throwable t) {
Errors.handleException(t);
}
});
};
}
onEvent will be called as soon as you do somewhere in your code the following
EventBus.getDefault().post(organisationDeviceSensorsEvent);
IFF EventBus is registered, so, if your fragment is up and running.
No, it will not be called when you resume the fragment. When you resume the fragment you register the EventBus listener to wait for events. when event are being posted "onEvent(){}" is being called.
You can do something like the thing you want if you post a sticky event (read on EventBut documentation how to post it and how to get it). In that case you can post the event at some random point in time, and your fragment will receive it at the same time if its running or when you register the listener. But again,
prepareSensorListData();
is being called on your onCreateView, so it will be called before the EventBus is even registered.

How to assing an Image to Imageview based on a query results?

Good afternoon everyone,
I have a quick question. I have a query result that I store in a list/array and I am trying to display the appropriate image in the imageview. The query provides a name of the image that is stored in the resource folder(res/drawable). I am getting error syntax. I am not sure how to solve this issue. I have a database that stores the name of the image in a field (database) called spic.
I have tried this code:
how to work with images and database in android?
but it doesnt work in this class. I am not sure is it because this class is "extends ArrayAdapter {" instead of "extends Activity {"
here is my code:
zCustomUsersAdapter class:
package com.example.yao;
import java.util.ArrayList;
import java.util.List;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class zCustomUsersAdapter extends ArrayAdapter<YAOYVD> {
public zCustomUsersAdapter(Context context, List<YAOYVD> users) {
super(context, 0, users);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// Get the data item for this position
// User user = getItem(position);
YAOYVD user = getItem(position);
// Check if an existing view is being reused, otherwise inflate the view
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.zitem_user, parent, false);
}
// Lookup view for data population
TextView tvName = (TextView) convertView.findViewById(R.id.tvName);
TextView tvHome = (TextView) convertView.findViewById(R.id.tvHometown);
ImageView tvImage = (ImageView) convertView.findViewById(R.id.ivUserIcon);
// Populate the data into the template view using the data object
tvName.setText(String.valueOf(user.getID_YAO()));
//.name);
tvHome.setText(String.valueOf(user.getNAME_YAO()));
//.hometown);
// tvImage.setBackgroundResource( getResourceID (String.valueOf(user.getSPIC_YAO()), "drawable",getApplicationContext() ));
// Return the completed view to render on screen
tvImage.setBackgroundResource(user.getSPIC_YAO()); //getSpic_YAO this is string in the YAO class. getSpic_YAO retrive the name of the image in the drawable folder.
return convertView;
}
}
zCustomListActivity
package com.example.yao;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.database.SQLException;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
public class zCustomListActivity extends Activity {
private YAOGetDataSource2 datasource;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.zactivity_custom_list);
//calling the method to create the database from asset folder
copydbfromassest();
// datasource = new YAODeckListDataSource(this);
datasource = new YAOGetDataSource2(this);
datasource.open();
populateUsersList();
}
private void populateUsersList() {
// Construct the data source
//List<YAOYVD> values = datasource.SQLYVDTABLESEARCH();
List<YAOYVD> arrayOfUsers = datasource.SQLYVDTABLESEARCH();
//.getUsers();
// Create the adapter to convert the array to views
zCustomUsersAdapter adapter = new zCustomUsersAdapter(this, arrayOfUsers);
// Attach the adapter to a ListView
ListView listView = (ListView) findViewById(R.id.lvUsers);
listView.setAdapter(adapter);
}
//#Override
public void onClick(View view) {
// TODO Auto-generated method stub
}
//database open and close
#Override
protected void onResume() {
datasource.open();
super.onResume();
}
#Override
protected void onPause() {
datasource.close();
super.onPause();
}
#Override
protected void onDestroy(){
datasource.close();
super.onDestroy();
}
/****
* new things to considered
*/
///copy the database from assest folder
private void copydbfromassest() {
// TODO Auto-generated method stub
YAOMySQLiteHelper myhelper = new YAOMySQLiteHelper (this);
try{
//create datbase
myhelper.importIfNotExist();
}catch (IOException e){
throw new Error("Unable to Create the Database ");
}
try{
myhelper.openDataBase();
}catch (SQLException sqle){
throw sqle;
}
}
}
You need to retrieve the resource identifier associated with that image's name. For this, you will need to get your app's Resources, which requires a context.
Context ctx = getContext();
int resId = ctx.getResources().getIdentifier(user.getSPIC_YAO(), "drawable", ctx.getPackageName());
if(resId != 0){
tvImage.setBackgroundResource(resId);
}

Populate a List View with Parse Objcets

My Main Activity has a button which redirects to ResterauntList Activity. I want to get a couple of Objects from my Parse Cloud, and want to add only the name to the ListView. This the code so far
package com.example.gastronomaapp;
import java.util.List;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ListView;
import com.parse.ParseObject;
import com.parse.ParseQuery;
import com.parse.ParseQueryAdapter;
import com.parse.ParseQueryAdapter.OnQueryLoadListener;
public class ResterauntList extends ActionBarActivity {
String mValue;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_resteraunt_list);
Bundle bdl = getIntent().getExtras();
mValue = bdl.getString("Value");
setContentView(R.layout.activity_resteraunt_list);
populateList(mValue);
}
private void populateList(String Value) {
ParseQueryAdapter.QueryFactory<ParseObject> factory = new ParseQueryAdapter.QueryFactory<ParseObject>() {
#SuppressWarnings({ "unchecked", "rawtypes" })
public ParseQuery create() {
ParseQuery query = new ParseQuery("Restraunt");
query.whereEqualTo("Location", Value);
return query;
}
};
ParseQueryAdapter<ParseObject> adapter = new ParseQueryAdapter<ParseObject>(
this, factory);
adapter.setTextKey("name");
adapter.addOnQueryLoadListener(new OnQueryLoadListener<ParseObject>() {
public void onLoading() {
// Trigger any "loading" UI
}
#Override
public void onLoaded(List<ParseObject> objects, Exception e) {
// TODO Auto-generated method stub
}
});
// Attach it to your ListView, as in the example above
ListView listView = (ListView) findViewById(R.id.restListView);
listView.setAdapter(adapter);
}
}
Not sure whats wrong, but the ListView never populates. My Parse Data Browser claims it has received requests though. Checked the Logcat, it claims the application may be doing too much work.Not really sure whats wrong.
(EDIT) Made a change as suggested in the comments. But now the list view has 2 entries but empty. I know there are 2 entries namely because they are clickable. Completely confused on what is wrong. Have edited the code too!
This is my emulator, as you can see the line there are list view entries
You are forgetting to call the adapter.notifyDatasetChanged() method. If I am not wrong, Parse queries are executed in background, right ? If so, then you need to call this method when the background thread is done.
Plus, as a suggestion, you can simplify your code as:
ParseQuery<ParseObject> query = new ParseQuery<ParseObject>("ClassName");
query.whereEqualTo("KEY","VALUE");
query.findInBackground(new FindCallback<ParseObject>(){
#Override
public void done(List<ParseObject> dataFromServer, ParseException e){
if( e == null ) { /** DO SOMETHING */ }
else { /** DO SOMETHING ELSE */ }
}
});

Android: Jsoup: GET put all items on ListView

If doc isn't null i want to put all data from doc to ListView, how do it?
If to write Element = doc.select("someSelector"); then i can't to put it in ListView;
Sorry for my english(i'am Russian)
Code:
package com.example.phpfunctions;
import java.io.IOException;
import java.util.Locale;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.widget.AutoCompleteTextView;
import android.widget.ListView;
import android.widget.Toast;
public class MainActivity extends Activity {
private final String lang = Locale.getDefault().getLanguage();
private final String functions_list = "someURL";
private final ListView lv = (ListView) findViewById(R.id.listView1);
Document doc = null;
AutoCompleteTextView input;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new getData().execute(functions_list);
if(doc != null)
{
//--Write code here--//
}
else
Toast.makeText(this, "error", Toast.LENGTH_LONG).show();
}
#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, menu);
return true;
}
class getData extends AsyncTask<String, Void, Document> {
protected Document doInBackground(String... urls) {
try {
Document data = Jsoup.connect(urls[0]).get();
return data;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
}
protected void onPreExecute() {
}
protected void onPostExecute(Document result) {
doc = result;
}
}
}
You can select all of the Elements from a page if you use the * as a wildcard character when calling doc.select(). To add all of the elements to a ListView you need to save each element into some type of array, e.g. an ArrayList and also use an ArrayAdapter.
For example:
ArrayList<String> htmlElements = new ArrayList<String>();
if(doc != null)
{
//--Write code here--//
Elements elements = doc.select("*"); // select all elements from that page
for (Element e : elements) {
htmlElements.add(e.html()); // or e.text(), depends on what you require
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1, htmlElements);
lv.setAdapter(adapter);
}
If you only want to list the elements from the body of the document, call doc.body().select("*") instead. The documentation is worth a read for some other tricks.

My Android-App doesn't Display ListView with custom Adapter

i'm writing an android app that should fetch song data from a parse.com database and display it with a custom Listview.
I tried it the way Nitin suggested but still dont get any listview displayed.
Here is the new code:
package de.android;
import java.util.ArrayList;
import java.util.List;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import com.parse.FindCallback;
import com.parse.Parse;
import com.parse.ParseObject;
import com.parse.ParseQuery;
public class Lyrics extends ListActivity {
ImageButton back;
ListView songList;
TextView textAnzeige;
private ArrayList<String> Titelliste = new ArrayList<String>();
private ArrayList<String> Interpretliste = new ArrayList<String>();
private ArrayList<String> Dauerliste = new ArrayList<String>();
ProgressDialog dialog;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_lyrics);
Parse.initialize(this, "hGxasGU6e0WQAOh5JIOGDfvFBKrYyBJKXIzxBfAG", "WsOPsXerpsFjsjekKKbZnnjAHvXy5PQHVQEB8Cqu");
initDataToView();
}
private void initDataToView() {
new getData().execute();
songList = (ListView)findViewById(android.R.id.list);
}
private class getData extends AsyncTask<Void, Void, SongAdapter>{
ProgressDialog dialog;
#Override
protected void onPreExecute() {
dialog = new ProgressDialog(Lyrics.this);
dialog.setMessage("Please wait, while loading!");
dialog.setIndeterminate(true);
dialog.setCancelable(false);
dialog.show();
Log.d("Parse", "PreExecute");
}
#Override
protected SongAdapter doInBackground(Void... params) {
ParseQuery pq = new ParseQuery("SongDatenbank");
pq.whereExists("Titel");
pq.findInBackground(new FindCallback() {
#Override
public void done(List<ParseObject> liederListe,
com.parse.ParseException e) {
if(e==null){
Log.d("Parse", "Objektliste empfangen");
ParseObject x;
for(int i=0;i<liederListe.size();i++){
x = liederListe.get(i);
Titelliste.add(x.getString("Titel"));
Dauerliste.add(x.getString("Dauer"));
Interpretliste.add(x.getString("Interpret"));
}
initDataToView();
x = liederListe.get(0);
Log.d("Parse", x.getString("Titel"));
Log.d("Parse", x.getString("Dauer"));
Log.d("Parse", x.getString("Interpret"));
}else{
Log.d("Parse", "Objektliste nicht empfangen");
}
}
});
return null;
}
protected void onPostExecute(SongAdapter result) {
songList.setAdapter(result);
dialog.dismiss();
Log.d("Parse", "Postexecute");
}
}
public class SongAdapter extends ArrayAdapter<String> {
private final Context context;
private final ArrayList<String> valuesTitel;
private final ArrayList<String> valuesInterpret;
private final ArrayList<String> valuesDauer;
public SongAdapter(Context context, ArrayList<String> valuesTitel, ArrayList<String> valuesInterpret, ArrayList<String> valuesDauer) {
super(context, R.layout.list_row);
this.context = context;
this.valuesTitel = valuesTitel;
this.valuesInterpret = valuesInterpret;
this.valuesDauer = valuesDauer;
}
public View getView(int position, View convertView, ViewGroup parent){
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.list_row, parent, false);
ImageView imageView = (ImageView)rowView.findViewById(R.id.list_image);
TextView titelText = (TextView)rowView.findViewById(R.id.title);
TextView artistText = (TextView)rowView.findViewById(R.id.artist);
TextView duration = (TextView)rowView.findViewById(R.id.duration);
titelText.setText(valuesTitel.get(position));
artistText.setText(valuesInterpret.get(position));
duration.setText(valuesDauer.get(position));
Log.d("Parse", valuesTitel.get(position));
return rowView;
}
}}
I think the problem is in parsing the data and give them to the adapter as parameter.
Any idea how to that?
I would really appreciate it if someone could look through my code and help me with solving the problem.
thanks, Paul
I suggest you to make a Handler to update the user interface or use Asynctask
Update UI from Thread
check the link in the first answer it tells about long operation in doInBackground method you should fetch the data and in onPostExecute set the adapter
read the comments in the following code.
private class PrepareAdapter1 extends AsyncTask<Void,Void,ContactsListCursorAdapter > {
ProgressDialog dialog;
#Override
protected void onPreExecute() {
dialog = new ProgressDialog(viewContacts.this);
dialog.setMessage(getString(R.string.please_wait_while_loading));
dialog.setIndeterminate(true);
dialog.setCancelable(false);
dialog.show();
}
/* (non-Javadoc)
* #see android.os.AsyncTask#doInBackground(Params[])
*/
#Override
protected ContactsListCursorAdapter doInBackground(Void... params) {
//do here parsing
return adapter1;
}
protected void onPostExecute(ContactsListCursorAdapter result) {
dialog.dismiss();
//Set Adapter
list.setAdapter(result);
}
}
Check R.layout.list_row. that is properly designed or not. i.e the layout is having the height and width as fill_parent. because from your code everything looks fine.
there is no getCount method in the Adapter class , try the following code
#Override
public int getCount()
{
return x.size();// number of datas in the list .
}
this method specifies the number of times the getView to be populated automatically .
............ One more suggession is that use extend BaseAdapter instead of ArrayAdapter<String>

Categories

Resources