0

我试图通过覆盖视图来设置 CustomAdapter 类中的文本颜色。代码似乎可以运行,但手机屏幕什么也没有显示。有什么建议么?

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

    prefs = PreferenceManager.getDefaultSharedPreferences(LogCat.this);

    String logLevel = prefs.getString("dirLogCat", "");

    setContentView(R.layout.log_cat);
    ListView lv1 = (ListView) findViewById(R.id.listView1);


    ArrayAdapter<String> arrayAdapter = 
                      new ArrayAdapter<String>(this, R.layout.text_list, log);

    try {
        Process process = Runtime.getRuntime().exec(new String[] {"logcat", "-d"});
        BufferedReader bufferedReader = new BufferedReader(
                          new InputStreamReader(process.getInputStream()));

        while ((line = bufferedReader.readLine()) != null){
            if (line.contains(" " + logLevel + " ")){
                    //log.add(line);
                log.add(line.substring(6, 14) + " " + line.substring(31, line.length()));
            }
        }
    CustomAdapter adapter = new CustomAdapter(log, this, "V");
    lv1.setAdapter(arrayAdapter);
    adapter.notifyDataSetChanged();
    }
    catch (IOException e) {}
}


 public class CustomAdapter extends BaseAdapter {

     private Context ctx;

     CustomAdapter (ArrayList<String> data, Context context, String log) { 
         this.ctx = context;

         this.getView(0, new View(context), null);
     }

     @Override
     public View getView(int position, View convertView, ViewGroup parent){ 
          LayoutInflater inflator = (LayoutInflater)ctx.getSystemService(LAYOUT_INFLATER_SERVICE);
          View v = inflator.inflate(R.layout.text_list, null);

          TextView textView = (TextView) v.findViewById(R.id.logText);
          textView.setTextColor(Color.CYAN);

          return textView;
     }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public Object getItem(int arg0) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public long getItemId(int arg0) {
        // TODO Auto-generated method stub
        return 0;
    } 
 }

}

编辑:

这做到了:

 public class CustomAdapter extends BaseAdapter {

     private Context ctx;
     private ArrayList<String> children;

     CustomAdapter (ArrayList<String> data, Context context, String log) { 
         this.ctx = context;
         System.out.println("Cyan1");
         this.children = data;

         this.getView(0, new View(context), null);
     }

     @Override
     public View getView(int position, View convertView, ViewGroup parent){ 
          LayoutInflater inflator = (LayoutInflater)ctx.getSystemService(LAYOUT_INFLATER_SERVICE);
          View v = inflator.inflate(R.layout.text_list, null);

          TextView textView = (TextView) v.findViewById(R.id.logText);
          System.out.println("Cyan");
          textView.setTextColor(Color.CYAN);
          for(int i = 0; i < log.size(); i++){
              textView.setText(children.get(i));
          }
          return textView;
     }
4

1 回答 1

1

您在列表中设置了错误的适配器?

CustomAdapter adapter = new CustomAdapter(log, this, "V");
lv1.setAdapter(arrayAdapter);  //<---- arrayAdapter instead of adapter?
adapter.notifyDataSetChanged();
于 2013-06-07T13:45:31.180 回答