Reputation: 57268
Im currently doing some android developer that lists items in a ListView
, we have created a WebView
that add's a JavaScript interface to our page and our page sends information via the JavaScript interface. this all works as expected, so we set up a class called HangoutManager
that extends a BaseAdapter
, we have implemented several methods in there such as add
/remove
and exists
.
This all works fine and now were at the point where need to use the BaseAdapter
to update the ViewList
when there changes to the Array Stack.
We can't seem to get it too work, the getView()
function never get's called to generate an item. here is some code.
public void onCreate(Bundle savedInstanceState)
{
//Call parent to construct the Activity
super.onCreate(savedInstanceState);
//Create Instance of HangoutManager, must be called here
HangoutManagerList = HangoutManager.Instance(this);
//Set the content view to the main ListView
setContentView(R.layout.main);
//instantiate the WebView
CanopyWebView = new CanopyWebView(this);
setListAdapter(HangoutManagerList);
}
public class HangoutManager extends BaseAdapter
{
public static HangoutManager _Instance;
private ArrayList<JSONObject> DataSet = new ArrayList<JSONObject>();
protected LayoutInflater Inflater;
public static HangoutManager Instance(Context context)
{
if(_Instance == null)
{
_Instance = new HangoutManager(context);
Log.v("HangoutManager", "Instance Created");
}
return _Instance;
}
public HangoutManager(Context context)
{
this.Inflater = LayoutInflater.from(context);
}
public boolean remove(String id)
{
try
{
for(int i=0 ; i< DataSet.size() ; i++ )
{
if(DataSet.get(i).getString("id").equals(id))
{
DataSet.remove(i);
Log.v("HangoutManager", "hangout Removed");
return true;
}
}
}
catch (JSONException e)
{
Log.e("HangoutManager::exists",e.getMessage());
return false;
}
return false;
}
public boolean add(String hangout)
{
try
{
JSONObject HangoutJson = new JSONObject(hangout);
if(this.exists(HangoutJson.getString("id")))
{
this.remove(HangoutJson.getString("id"));
}
DataSet.add(HangoutJson);
Log.v("HangoutManager", "hangout Added");
notifyDataSetChanged();
}
catch(JSONException e)
{
Log.e("HangoutManager",e.getMessage());
}
return true;
}
public boolean exists(String id)
{
try
{
for(int i=0 ; i< DataSet.size() ; i++ )
{
if(DataSet.get(i).getString("id").equals(id))
{
Log.v("HangoutManager", "hangoutExists: " + id);
return true;
}
}
}
catch (JSONException e)
{
Log.e("HangoutManager::exists",e.getMessage());
return false;
}
return false;
}
@Override
public int getCount()
{
return DataSet.size();
}
@Override
public Object getItem(int position)
{
return DataSet.get(position);
}
@Override
public long getItemId(int position)
{
return position;
}
@Override
public View getView(int position, View view, ViewGroup viewGroup)
{
if(view == null)
{
view = Inflater.inflate(R.layout.item1, viewGroup, false);
}
//Get the JSONObject for the Item
JSONObject entity = DataSet.get(position);
//Set the JSONObject as the tag for the row
view.setTag(entity);
//return the view to be drawn
return view;
}
}
<?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:cacheColorHint="#00000000"
android:id="@android:id/list">
</ListView>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:layout_width="fill_parent">
<TextView
android:id="@+id/text"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:background="#FFFFFFFF"
android:gravity="center_vertical"
android:text="@string/app_name"
android:textColor="#FF000000"
android:visibility="visible" />
</LinearLayout>
The section about is where we attempt to break but it never breaks at that point, am we doing something wrong ?
The application seems to crash sometime during the notifyDataSetChanged()
calls.
Upvotes: 0
Views: 549
Reputation: 7641
You shouldn't call the inflater like this. Use the following syntax to get an Inflater to use from your getView()
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
Also about the stacktrace, it looks like your JS interface callbacks are executed in background. You cannot modify the data collection binded to the ListView
nor call updateNotifyDataset()
from a background thread.
But you can ask the UIThread to do it for you by calling your add method like this:
yourActivityInstance.runOnUiThread(new Runnable() {
public void run() {
yourAdapterInstance.add(newHangout);
}});
Upvotes: 1