BaseAdaptor用于动态内容

时间:2011-12-09 13:45:43

标签: java android listview baseadapter

我目前正在做一些列出ListView中项目的Android开发人员,我们创建了一个WebView,它为我们的页面添加了一个JavaScript接口,我们的页面通过JavaScript界面​​发送信息。这一切都按预期工作,所以我们设置了一个名为HangoutManager的类,它扩展了BaseAdapter,我们在那里实现了几个方法,例如add / remove和{{ 1}}。

这一切都运行正常,现在需要在更改数组堆栈时使用exists更新BaseAdapter

我们似乎无法使它工作太多,ViewList函数永远不会被调用来生成项目。这是一些代码。

的onCreate

getView()

HangoutManager

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);
}

main.xml中

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;
    }
}

item1.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>

StackTrace(不是错误堆栈跟踪)

关于我们试图打破的部分,但在那一点上它从未破裂,我们做错了吗?

更新

<?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> 来电期间,应用程序似乎崩溃了。

1 个答案:

答案 0 :(得分:1)

你不应该这样叫inflater。 使用以下语法从GetView()

中获取要使用的Inflater
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

关于stacktrace,看起来你的JS接口回调是在后台执行的。您无法修改绑定到ListView的数据集合,也无法从后台线程调用updateNotifyDataset()

但你可以通过调用这样的add方法让UIThread为你做这件事:

yourActivityInstance.runOnUiThread(new Runnable() {
    public void run() {
        yourAdapterInstance.add(newHangout);
}});