如何根据状态更改listview项目的颜色?

时间:2013-12-01 09:16:20

标签: android

我想根据数据库中的状态更改列表视图项的颜色.... 这是我的代码:

public class Main extends Activity {
  Button btnExit;
  LinearLayout Linear;
  private Intent intent;
  //public static String PersonelNo;
  private String PersonelNo;
  SQLiteDatabase mydb;
  ListView listViewSMS; 
  Context context;
  private static String DBNAME = "PDA"; 
  private static String TABLE = "TblRequest";
  //private static String TABLE2 = "TblSerial";
  ArrayAdapter <String> adapter;
  private  ArrayList<String> result= new ArrayList<String>();
  public static Integer Flag;

  @Override
  public void onCreate(Bundle savedInstanceState) {
      Uri uriSMSURI = Uri.parse("content://sms/inbox");
      //MsgCur.Close;
      Cursor MsgCur = getContentResolver().query(uriSMSURI, null, null, null,null);
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main);
              listViewSMS=(ListView)findViewById(R.id.List);


      Linear  = (LinearLayout) findViewById(R.id.Linear);


      createTable();
      //mydb = openOrCreateDatabase(DBNAME, Context.MODE_PRIVATE,null);
      NaraDatabaseHandler DH = new NaraDatabaseHandler(this);         
      mydb = DH.getReadableDatabase();
      TextView textViewListItems = (TextView) findViewById(R.id.list_content);
      if (MsgCur.getCount()>0){
            Cursor c = mydb.rawQuery("SELECT RequestNo,RequestStatus FROM " +
                    TABLE , null);
            if (c != null ) {             
                if  (c.moveToFirst()) {
                    do {
                        String ReqNo = c.getString(c.getColumnIndex("RequestNo"));
                        result.add(ReqNo);
                        int ReqStatus = c.getInt(c.getColumnIndex("RequestStatus"));

                        if (ReqStatus==1)
                            textViewListItems.setTextColor(Color.RED);
                    }while (c.moveToNext());
                } 
            }
            if (c != null && !c.isClosed()) {
                c.close();
            }
            //SimpleCursorAdapter adapter2 = new SimpleCursorAdapter(this, R.layout.listview_each_item, c, result, null);
            adapter=new ArrayAdapter<String>(this,R.layout.list_item,R.id.list_content,result);

            listViewSMS.setAdapter(adapter);


         }
      MsgCur.close();


      listViewSMS.setOnItemClickListener(new OnItemClickListener()
      {
      public void onItemClick(AdapterView<?> arg0, View v, int position, long id)
      {
          Intent ErrRep    = new Intent(Main.this,ErrorReport.class);
          String ItemReqNo = (String) listViewSMS.getItemAtPosition(position);
          ErrRep.putExtra("ItemReqNo", ItemReqNo);
          ErrRep.putExtra("PersonelNumber", PersonelNo);
          startActivityForResult(ErrRep, 0);                                 
      }
                                      });                 


      btnExit=(Button)findViewById(R.id.BtnExitMain);
      btnExit.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Intent intentExit = new Intent(Main.this, FirstPage.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                intent.putExtra("Exit", true);
                startActivity(intentExit);
                finish();
              }
           });
}

问题在于行:::

if (ReqStatus==1)

textViewListItems.setTextColor(Color.RED);

它给了我以下错误:::: 12-01 12:23:37.631: E/AndroidRuntime(6926): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.IranNara/com.example.IranNara.Main}: java.lang.NullPointerException 12-01 12:23:37.631: E/AndroidRuntime(6926): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663) 12-01 12:23:37.631: E/AndroidRuntime(6926): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679) 12-01 12:23:37.631: E/AndroidRuntime(6926): at android.app.ActivityThread.access$2300(ActivityThread.java:125) 12-01 12:23:37.631: E/AndroidRuntime(6926): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033) 12-01 12:23:37.631: E/AndroidRuntime(6926): at android.os.Handler.dispatchMessage(Handler.java:99) 12-01 12:23:37.631: E/AndroidRuntime(6926): at android.os.Looper.loop(Looper.java:123) 12-01 12:23:37.631: E/AndroidRuntime(6926): at android.app.ActivityThread.main(ActivityThread.java:4627) 12-01 12:23:37.631: E/AndroidRuntime(6926): at java.lang.reflect.Method.invokeNative(Native Method) 12-01 12:23:37.631: E/AndroidRuntime(6926): at java.lang.reflect.Method.invoke(Method.java:521) 12-01 12:23:37.631: E/AndroidRuntime(6926): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 12-01 12:23:37.631: E/AndroidRuntime(6926): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 12-01 12:23:37.631: E/AndroidRuntime(6926): at dalvik.system.NativeStart.main(Native Method)

和另一个问题,当我进入退出按钮时,它给了我错误....

1 个答案:

答案 0 :(得分:0)

您可以使用Custom Adapter来实现此目的,然后您可以在ListItem Color中更改getView method

对于Custom ListViewCustom Adapter,请参阅以下链接

http://www.vogella.com/articles/AndroidListView/article.html