CustomAdapter视图覆盖不设置文本

时间:2013-06-07 13:39:04

标签: android custom-adapter

我试图通过覆盖View来设置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;
     }

1 个答案:

答案 0 :(得分:1)

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

CustomAdapter adapter = new CustomAdapter(log, this, "V");
lv1.setAdapter(arrayAdapter);  //<---- arrayAdapter instead of adapter?
adapter.notifyDataSetChanged();