0

私は現在、 に項目​​をリストする Android 開発者をやっています。ページに JavaScript インターフェースを追加するListViewを作成しWebView、ページは JavaScript インターフェースを介して情報を送信します。HangoutManagerこれはすべて期待どおりに機能するため、 a を拡張するというクラスをセットアップし、/やBaseAdapterなどのいくつかのメソッドをそこに実装しました。addremoveexists

これはすべて正常に機能し、配列スタックに変更があっBaseAdapterたときに を使用して更新する必要がある時点になりました。ViewList

getView()アイテムを生成するために関数が呼び出されることはありません。ここにいくつかのコードがあります。

onCreate

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

main.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()通話中にアプリケーションがクラッシュすることがあります。

4

1 に答える 1

1

このようにインフレータを呼び出すべきではありません。getView() から使用するインフレータを取得するには、次の構文を使用します。

LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

スタックトレースについても、JS インターフェースのコールバックがバックグラウンドで実行されているようです。バックグラウンド スレッドからのno呼び出しにバインドされたデータ コレクションを変更することはできません。ListViewupdateNotifyDataset()

ただし、次のように add メソッドを呼び出すことで、UIThread にそれを実行するように依頼できます。

yourActivityInstance.runOnUiThread(new Runnable() {
    public void run() {
        yourAdapterInstance.add(newHangout);
}});
于 2011-12-09T17:02:29.153 に答える