我目前正在做一些列出项目的 android 开发人员ListView
,我们已经创建了一个WebView
向我们的页面添加一个 JavaScript 接口,并且我们的页面通过 JavaScript 接口发送信息。这一切都按预期工作,所以我们设置了一个名为HangoutManager
扩展 a的类BaseAdapter
,我们在那里实现了几个方法,例如add
/remove
和exists
。
这一切都很好,现在需要使用BaseAdapter
来更新ViewList
Array Stack 的更改时间。
我们似乎不能让它太工作,该getView()
函数永远不会被调用来生成一个项目。这是一些代码。
创建
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
<?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>
item1.xml
<?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>
StackTrace(不是错误堆栈跟踪)
关于部分是我们试图打破但它从未在这一点上打破,我们做错了什么吗?
更新
该应用程序似乎在notifyDataSetChanged()
通话期间的某个时间崩溃。