如何在MainActivity中使用ArrayAdapter类和runOnUiThread方法显示ListView?

时间:2015-07-14 10:14:21

标签: android multithreading listview android-arrayadapter runnable

当我尝试运行我的应用时,我收到以下错误:

  java.lang.ClassCastException: com.protogeo.moves.demos.apps.MainActivity$6 cannot be cast to android.content.Context
            at com.protogeo.moves.demos.apps.StorylineAdapter.<init>(StorylineAdapter.java:28)
            at com.protogeo.moves.demos.apps.MainActivity$6.run(MainActivity.java:285)

我的主要活动代码如下所示:

public class MainActivity extends FragmentActivity implements OnClickListener{
...
    public void updateResponse(final String message) {
        runOnUiThread(new Runnable() {

    @Override
    public void run() {
        //mEditTextResponse.setText(message);
        StorylineAdapter adapter = new StorylineAdapter(this, R.layout.item_storyline, summary);
       // mEditTextResponse.setAdapter(adapter);
        mEditTextResponse.setAdapter(adapter);

...

我的ArrayAdapter代码如下所示:

 public class StorylineAdapter extends ArrayAdapter<SummaryData>{
    private Context context;
    private ArrayList<SummaryData> summary;

    public StorylineAdapter(Context context, int item_storyline, ArrayList<SummaryData> objects) {
        super( context, item_storyline, objects);
        this.context = context;
        this.runnable = runnable;
        this.summary = objects;

    }



    @Override
    public View getView(int position, View convertView, ViewGroup parent){

        LayoutInflater inflater = ((Activity) context).getLayoutInflater();
        View view = inflater.inflate(R.layout.item_storyline, parent, false);


           summary = summary.get(position);
        TextView tv = (TextView) view.findViewById(R.id.textView1);
        tv.setText(summary.getActivity());

        return view;
    }
}

0 个答案:

没有答案