文本未出现在列表视图中

时间:2011-06-08 07:06:19

标签: android android-listview simpleadapter

我有一个列表视图,其中我想要自定义字体,例如Arial字体。为此,我将SimpleAdapter子类化,并在列表中实现了textview的字体。但在执行此操作后,列表显示为空白,但onClick仍然有效,单击空白列表项会根据需要导航到下一个活动。

代码

public class TopNewsActivity extends ListActivity {

public static final String LOG_TAG = "Infra";
private ProgressDialog progressDialog;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.listplaceholder);
    new BackgroundAsyncTask().execute();
}

public class BackgroundAsyncTask extends AsyncTask<String, Integer, ArrayList<HashMap<String, String>>> {

    @Override
    protected void onPreExecute() {
        progressDialog = new ProgressDialog(TopNewsGroup.group);
        progressDialog.setCancelable(true);
        progressDialog.setMessage("Loading...");
        progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        progressDialog.setProgress(0);
        progressDialog.show();
    }

    @Override
    protected ArrayList<HashMap<String, String>> doInBackground(String... paths) {

        ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();

        String xml = XMLfunctions.getTopNewsXML();
        Document doc = XMLfunctions.XMLfromString(xml);

        int numResults = XMLfunctions.numResults(doc);
        Log.d(LOG_TAG, "Number of Results: " + numResults);
        if ((numResults <= 0)) {
            Toast.makeText(TopNewsActivity.this, "No Result Found",Toast.LENGTH_LONG).show();
            return null;
        }

        NodeList nodes = doc.getElementsByTagName("result");

        for (int i = 0; i < nodes.getLength(); i++) {
            HashMap<String, String> map = new HashMap<String, String>();

            Element e = (Element) nodes.item(i);
            map.put("id", XMLfunctions.getValue(e, "id"));
            map.put("title", XMLfunctions.getValue(e, "title"));
            map.put("date", XMLfunctions.getValue(e, "date"));
            map.put("recordDate", XMLfunctions.getValue(e, "recordDate"));
            mylist.add(map);
        }
        return mylist;
    }

    @Override
    protected void onProgressUpdate(Integer... values) {
        super.onProgressUpdate(values);
    }

    protected void onPostExecute(ArrayList<HashMap<String, String>> result) {



        ListAdapter adapter = new MySimpleAdapter(TopNewsActivity.this, result, R.layout.list_item, new String[] { "title", "date" }, new int[] { R.id.item_title, R.id.item_subtitle });
        setListAdapter(adapter);
        progressDialog.dismiss();

        final ListView lv = getListView();

        lv.setTextFilterEnabled(true);  
        lv.setOnItemClickListener(new OnItemClickListener() {
            @SuppressWarnings("unchecked")
            @Override
            public void onItemClick(AdapterView<?> a, View view, final int position, long id) {

                HashMap<String, String> o = (HashMap<String, String>) lv.getItemAtPosition(position);

                Intent i = new Intent(TopNewsActivity.this, NewsDetails.class);
                i.putExtra("content_id", o.get("id"));
                i.putExtra("title", o.get("title"));
                i.putExtra("date", o.get("recordDate"));
                i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

                View v = TopNewsGroup.group.getLocalActivityManager().startActivity("ShowNews", i).getDecorView();

                // Again, replace the view
                TopNewsGroup.group.replaceView(v);
            }
        });
    }
}

public class MySimpleAdapter extends SimpleAdapter {
    public MySimpleAdapter(Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to) {
        super(context, data, resource, from, to);
    }

    public View getView(int position, View view, ViewGroup parent){

        Typeface localTypeface1 = Typeface.createFromAsset(getAssets(), "fonts/arial.ttf");
        View v = view;
        if (v == null) {
            LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = vi.inflate(R.layout.list_item, null);
        }
        TextView tt = (TextView) v.findViewById(R.id.item_title);
        tt.setBackgroundColor(R.color.white);
        tt.setTypeface(localTypeface1);
        tt.setTextColor(android.R.color.black);
        TextView bt = (TextView) v.findViewById(R.id.item_subtitle);
        bt.setBackgroundColor(R.color.white);
        bt.setTypeface(localTypeface1);
        bt.setTextColor(R.color.grey);
        return v;
        /*Typeface localTypeface1 = Typeface.createFromAsset(getAssets(), "fonts/arial.ttf");
        View v = super.getView(position, view, parent);
        ((TextView) v).setTypeface(localTypeface1);
        return v;*/
    }
}

 }

2 个答案:

答案 0 :(得分:2)

在简单适配器声明中使用此代码

public MySimpleAdapter(Context context, ArrayList<? extends Map<String, String>> data, int resource, String[] from, int[] to) {
    super(context, data, resource, from, to);
}

或者在其他地方,将ArrayList更改为List。我觉得那里有冲突。

Rahul, Use this link to understand a bit more on using SimpleAdapter

答案 1 :(得分:1)

答案非常简单,因为这是我的错误,没有提供要查看getView方法的ItemText:

public class MySimpleAdapter extends SimpleAdapter {

    private ArrayList<HashMap<String, String>> results;

    public MySimpleAdapter(Context context, ArrayList<HashMap<String, String>> data, int resource, String[] from, int[] to) {
        super(context, data, resource, from, to);
        this.results = data;
    }

    public View getView(int position, View view, ViewGroup parent){

        Typeface localTypeface1 = Typeface.createFromAsset(getAssets(), "fonts/arial.ttf");
        View v = view;
        if (v == null) {
            LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = vi.inflate(R.layout.list_item, null);
        }
        TextView tt = (TextView) v.findViewById(R.id.item_title);
        tt.setText(results.get(position).get("title"));
        tt.setTypeface(localTypeface1);
        TextView bt = (TextView) v.findViewById(R.id.item_subtitle);
        bt.setText(results.get(position).get("date"));
        bt.setTypeface(localTypeface1);
        return v;
    }
}

:)