-1

我正在从网站获取数据,并且在 android 中显示为 listview,但是它显示了一些 html 标签,其中还包括我的 listview 中的数据。

应用适配器.java

public class ApplicationAdapter extends ArrayAdapter<Application>
{
    private List<Application> items;
    private LayoutInflater inflator;

    public ApplicationAdapter(Context context, List<Application> items)
    {
        super(context, R.layout.activity_row, items);
        this.items = items;
        inflator = LayoutInflater.from(getContext());
    }

    @Override
    public int getCount()
    {
        return items.size();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent)
    {
        ViewHolder holder = null;
        //View v = convertView;
        if ( convertView == null )
        {   
            convertView = inflator.inflate(R.layout.activity_row, null);
            LayoutInflater li = LayoutInflater.from(getContext());
            //convertView = inflator.inflate(R.layout.app_custom_list, null);
            holder = new ViewHolder();
            holder.text1 = (TextView) convertView.findViewById(R.id.text1);
            holder.chk = (CheckBox) convertView.findViewById(R.id.checkbox);
            /*holder.chk
            .setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

                /*@Override
                public void onCheckedChanged(CompoundButton view,
                        boolean isChecked) {
                    int getPosition = (Integer) view.getTag();
                    items.get(getPosition).setSelected(view.isChecked());

                }
            });*/
            convertView.setTag(holder);
            convertView.setTag(R.id.text1, holder.text1);
            convertView.setTag(R.id.checkbox, holder.chk);
        }else {
            holder = (ViewHolder) convertView.getTag();
        }
        Application app = items.get(position);
        holder.chk.setTag(position);
        holder.text1.setText(items.get(position).getContent());
        //holder.chk.setChecked(items.get(position).isSelected());


        if ( app != null )
        {
            TextView titleText = (TextView) convertView.findViewById(R.id.titleTxt);
            if ( titleText != null )
                titleText.setText(Html.fromHtml(app.getContent()).toString());
                //titleText.setText(app.getContent());
               //holder.chk.setChecked(((View) Html.fromHtml(app.getContent())).isSelected());
        }
        return convertView;
    }
     static class ViewHolder {
        public TextView text1;
        public CheckBox chk;
    }
        //return convertView;


}

尽管我使用了一行来删除 listview 中的 html 标签,但我不知道它是否像
我的 listview 一样显示 html 标签。我的输出在 listview 中显示类似 hi <br>bye <br>' hello 。<br>但我不想在我的 listview 中显示 html 标签,我该如何解决这个问题。

Mainactivity.java

public class MainActivity extends Activity implements FetchDataListener
{
    private static final int ACTIVITY_CREATE=0;
    private ProgressDialog dialog;
    ListView lv;
    //private ProjectsDbAdapter mDbHelper;
    //private SimpleCursorAdapter dataAdapter;


    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);


        setContentView(R.layout.activity_list_item);  
         //mDbHelper = new ProjectsDbAdapter(this);
            //mDbHelper.open();
            //fillData();
            //registerForContextMenu(getListView());

     lv =(ListView)findViewById(R.id.list);
        initView();
    }



    private void initView()
    {
        // show progress dialog
        dialog = ProgressDialog.show(this, "", "Loading...");
        String url = "http://dry-brushlands-3645.herokuapp.com/posts.json";
        FetchDataTask task = new FetchDataTask(this);
        task.execute(url);

        //mDbHelper.open();     
        //Cursor projectsCursor = mDbHelper.fetchAllProjects();
        //startManagingCursor(projectsCursor);

        // Create an array to specify the fields we want to display in the list (only TITLE)
        //String[] from = new String[]{ProjectsDbAdapter.KEY_TITLE};

        // and an array of the fields we want to bind those fields to (in this case just text1)
        //int[] to = new int[]{R.id.text1};

        /* Now create a simple cursor adapter and set it to display
        SimpleCursorAdapter projects = 
                new SimpleCursorAdapter(this, R.layout.activity_row, projectsCursor, from, to);
        setListAdapter(projects);
        */
        // create the adapter using the cursor pointing to the desired data 
        //as well as the layout information
         /*dataAdapter  = new SimpleCursorAdapter(
          this, R.layout.activity_row, 
          projectsCursor, 
          from, 
          to,
          0);
         setListAdapter(dataAdapter);
        */
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        //getMenuInflater().inflate(R.menu.activity_main, menu);
        super.onCreateOptionsMenu(menu);
         MenuInflater mi = getMenuInflater();
            mi.inflate(R.menu.activity_main, menu); 
        return true;

    }

     @Override
        public boolean onMenuItemSelected(int featureId, MenuItem item) {

                createProject();

            return super.onMenuItemSelected(featureId, item);
     }

     private void createProject() {
            Intent i = new Intent(this, ProjectEditActivity.class);
            startActivityForResult(i, ACTIVITY_CREATE);   
        }

     @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
            super.onActivityResult(requestCode, resultCode, intent);
            initView();
        }

    @Override
    public void onFetchComplete(List<Application> data)
    {
        // dismiss the progress dialog
        if ( dialog != null )
            dialog.dismiss();
        // create new adapter
        ApplicationAdapter adapter = new ApplicationAdapter(this, data);
        // set the adapter to list
        lv.setAdapter(adapter);
    }

    @Override
    public void onFetchFailure(String msg)
    {
        // dismiss the progress dialog
        if ( dialog != null )
            dialog.dismiss();
        // show failure message
        Toast.makeText(this, msg, Toast.LENGTH_LONG).show();
    }
}

我只想显示列表视图数据,不需要 htmltags

我想知道如果(titleText!= null)那么哪一行是正确的, titleText.setText(Html.fromHtml(app.getContent()).toString());或者我想这样写holder.text1.setText(Html.fromHtml(app.getContent()).toString());

4

1 回答 1

1

你可以通过改变来做到

holder.text1.setText(items.get(position).getContent());

holder.text1.setText(Html.fromHtml(items.get(position).getContent()));

于 2013-06-04T07:53:38.780 回答