使用简单适配器填充时,listview的每个项目都有不同的文本颜色

时间:2015-07-02 03:17:09

标签: android listview android-listview

我正在尝试设置列表视图中每个项目的不同文本颜色,如下图所示填充项目,但我无法使其工作,

这个想法是,如果“Rata-Rata”的数量超过75,那么文本颜色将被设置为黑色,但如果低于它将被设置为红色。

enter image description here

这是我的代码,我重写了getview方法:

calonSiswa.add(map);
list = (ListView) findViewById(R.id.list);

ListAdapter adapter = new SimpleAdapter(SeleksiNilai.this,
  calonSiswa,
  R.layout.activity_seleksi_nilai_single_item_view,
  new String[] {
    TAG_NO_URUTAN, TAG_NO_PENDAFTARAN,
    TAG_NAMA_LENGKAP, TAG_JURUSAN,
    TAG_RATA_RATA_NILAI, TAG_CARA_SELEKSI
  },
  new int[] {
    R.id.nomorUrutan, R.id.noPendaftar,
      R.id.namaPendaftar, R.id.jurusanPendaftar,
      R.id.rataRataNilai, R.id.caraSeleksi
  }) {
  @Override
  public View getView(int position, View convertView,
    ViewGroup parent) {
    View view = super.getView(position, convertView,
      parent);
    float ratarata = Float.parseFloat(rata_rata);
    int posisi = position;
    int textColorId = R.color.black;
    TextView text;
    text = (TextView) view
      .findViewById(R.id.noPendaftar);

    if (ratarata <= 75) {
      textColorId = R.color.red;
    } else if (ratarata >= 75) {
      textColorId = R.color.black;
    }
    text.setTextColor(getResources().getColor(
      textColorId));
    return view;
  }
};

这是另一个图例,如果我将顺序从最低到最高更改,似乎问题是每当最后一个数字超过或低于75时,它将改变列表视图中的所有项目颜色,而不是具体位置 enter image description here

这是完整的代码:

private class GetData extends AsyncTask < String, String, JSONObject > {
  private ProgressDialog pDialog;
  String nomor, no_pendaftaran, nama_lengkap, jurusan, rata_rata, cara_seleksi;

  @
  Override
  protected void onPreExecute() {
    super.onPreExecute();
    noPendaftaran = (TextView) findViewById(R.id.noPendaftar);
    namaPendaftar = (TextView) findViewById(R.id.namaPendaftar);
    statusProses = (TextView) findViewById(R.id.rataRataNilai);
    pDialog = new ProgressDialog(SeleksiNilai.this);
    pDialog.setMessage("Getting Data ...");
    pDialog.setIndeterminate(false);
    pDialog.setCancelable(true);
    pDialog.show();

  }

  @
  Override
  protected JSONObject doInBackground(String...args) {

    // Getting JSON from URL
    JSONObject json = jParser.getJSONFromUrl(url);
    return json;
  }

  @
  Override
  protected void onPostExecute(JSONObject json) {
    pDialog.dismiss();
    try {
      // Getting JSON Array from URL
      pendaftar = json.getJSONArray(TAG_OS);

      for (int i = 0; i < pendaftar.length(); i++) {
        JSONObject c = pendaftar.getJSONObject(i);

        // Storing JSON item in a Variable
        nomor = String.valueOf(i + 1);
        no_pendaftaran = c.getString(TAG_NO_PENDAFTARAN);
        nama_lengkap = c.getString(TAG_NAMA_LENGKAP);
        jurusan = c.getString(TAG_JURUSAN);
        rata_rata = c.getString(TAG_RATA_RATA_NILAI);
        cara_seleksi = c.getString(TAG_CARA_SELEKSI);

        // Adding value HashMap key => value

        HashMap < String, String > map = new HashMap < String, String > ();

        map.put(TAG_NO_URUTAN, nomor);
        map.put(TAG_NO_PENDAFTARAN, no_pendaftaran);
        map.put(TAG_NAMA_LENGKAP, nama_lengkap);
        map.put(TAG_JURUSAN, jurusan);
        map.put(TAG_RATA_RATA_NILAI, rata_rata);
        map.put(TAG_CARA_SELEKSI, cara_seleksi);
        /*map.put(TAG_STATUS_PROSES, status_proses);*/

        calonSiswa.add(map);
        list = (ListView) findViewById(R.id.list);

        ListAdapter adapter = new SimpleAdapter(
          SeleksiNilai.this, calonSiswa,
          R.layout.activity_seleksi_nilai_single_item_view,
          new String[] {
            TAG_NO_URUTAN, TAG_NO_PENDAFTARAN, TAG_NAMA_LENGKAP, TAG_JURUSAN, TAG_RATA_RATA_NILAI, TAG_CARA_SELEKSI
          },
          new int[] {
            R.id.nomorUrutan, R.id.noPendaftar, R.id.namaPendaftar, R.id.jurusanPendaftar, R.id.rataRataNilai, R.id.caraSeleksi
          }) {@
          Override
          public View getView(int position, View convertView, ViewGroup parent) {
            View view = super.getView(position, convertView, parent);
            float ratarata = Float.parseFloat(rata_rata);
            int posisi = position;
            int textColorId = R.color.black;
            TextView text;
            text = (TextView) view.findViewById(R.id.noPendaftar);

            if (ratarata <= 75) {
              textColorId = R.color.red;
            } else if (ratarata >= 75) {
              textColorId = R.color.black;
            }
            text.setTextColor(getResources().getColor(textColorId));
            return view;
          }
        };

        list.setAdapter(adapter);

      }
    } catch (JSONException e) {
      e.printStackTrace();
    }

  }
}

5 个答案:

答案 0 :(得分:1)

在getView()中按位置获取“rata_rata”。

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; NSDictionary *parameters = @{ kSettingDashboard: @"compact", kSettingTheme: @"dark", kSettingShowSideBar: @"yes" }; [manager GET:kURLChangeSetting parameters:[NSDictionary dictionaryWithObject:parameters forKey:@"setting"] success:^(AFHTTPRequestOperation *operation, id responseObject) { // code } failure:^(AFHTTPRequestOperation *operation, NSError *error) { /// code }];

答案 1 :(得分:0)

 int textColorId;
   TextView text; //add this lines in global 

    @Override
      public View getView(int position, View convertView, ViewGroup parent) {
                View view = super.getView(position, convertView, parent);
                float ratarata = Float.parseFloat(rata_rata);
                int posisi = position;


                text = (TextView) view.findViewById(R.id.noPendaftar);

                if (ratarata <= 75)
                {
                  textColorId = R.color.red;
                } else {
                  textColorId = R.color.black;
                }
                text.setTextColor(getResources().getColor(textColorId));
                return view;
              }
            };

答案 2 :(得分:-1)

那很明显,你在循环中调用setAdapter方法,ratarata的值总是最后一个

答案 3 :(得分:-1)

尝试这种方式可以帮助你

 @Override
            public View getView(int position, View convertView,
            ViewGroup parent) {
            View view = super.getView(position, convertView,
              parent);

            text1 =(TextView)view.findViewById(R.id.noPendaftar);



             if (ratarata <= 75) {
                  textColorId = R.color.red;
                  text1.setTextColor(textColorId);
                } else if (ratarata >= 75) {
                  textColorId = R.color.black;
                  text1.setTextColor(textColorId);
                }

             //set the tag of position
            view.setTag(position);
            return view;
          }
        };

答案 4 :(得分:-1)

问题解决了,感谢situee,使用

获取rata_rata值

rata_rata = calonSiswa.get(position).get(TAG_RATA_RATA_NILAI);

所以这里是代码:

&#13;
&#13;
@
Override
protected void onPostExecute(JSONObject json) {
  pDialog.dismiss();
  try {
    // Getting JSON Array from URL
    pendaftar = json.getJSONArray(TAG_OS);

    for (int i = 0; i < pendaftar.length(); i++) {
      JSONObject c = pendaftar.getJSONObject(i);


      // Storing JSON item in a Variable
      nomor = String.valueOf(i + 1);
      no_pendaftaran = c.getString(TAG_NO_PENDAFTARAN);
      nama_lengkap = c.getString(TAG_NAMA_LENGKAP);
      jurusan = c.getString(TAG_JURUSAN);
      rata_rata = c.getString(TAG_RATA_RATA_NILAI);
      cara_seleksi = c.getString(TAG_CARA_SELEKSI);


      // Adding value HashMap key => value

      HashMap < String, String > map = new HashMap < String, String > ();

      map.put(TAG_NO_URUTAN, nomor);
      map.put(TAG_NO_PENDAFTARAN, no_pendaftaran);
      map.put(TAG_NAMA_LENGKAP, nama_lengkap);
      map.put(TAG_JURUSAN, jurusan);
      map.put(TAG_RATA_RATA_NILAI, rata_rata);
      map.put(TAG_CARA_SELEKSI, cara_seleksi);

      calonSiswa.add(map);
      ListView list = (ListView) findViewById(R.id.list);


      adapter = new SimpleAdapter(
        SeleksiNilai.this, calonSiswa,
        R.layout.activity_seleksi_nilai_single_item_view,
        new String[] {
          TAG_NO_URUTAN, TAG_NO_PENDAFTARAN, TAG_NAMA_LENGKAP, TAG_JURUSAN, TAG_RATA_RATA_NILAI, TAG_CARA_SELEKSI
        },
        new int[] {
          R.id.nomorUrutan, R.id.noPendaftar, R.id.namaPendaftar, R.id.jurusanPendaftar, R.id.rataRataNilai, R.id.caraSeleksi
        }) {
        
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
          View view = super.getView(position, convertView, parent);
          
          //HERE I ADD THE CODE FROM situee
          String rata_rata1 = calonSiswa.get(position).get(TAG_RATA_RATA_NILAI);
          float ratarata = Float.parseFloat(rata_rata1);
          //////////////////////////
          
          int textColorId;
          TextView text;
          text = (TextView) view.findViewById(R.id.noPendaftar);

          if (ratarata <= 75) {
            textColorId = R.color.red;
          } else {
            textColorId = R.color.black;
          }
          text.setTextColor(getResources().getColor(textColorId));
          return view;
        }

      };
      list.setAdapter(adapter);
      list.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @
        Override
        public void onItemClick(AdapterView <? > parent,
          View view, int position, long id) {
          // TODO Auto-generated method stub
          noPendaftaranTerpilih = calonSiswa.get(position).get("no_pendaftaran");
          Intent i = new Intent(SeleksiNilai.this, SeleksiNilaiLanjutan.class);
          startActivity(i);

          /*Toast.makeText(SeleksiNilai.this,"You Clicked at "+ calonSiswa.get(position).get("no_pendaftaran"),
											Toast.LENGTH_SHORT).show();*/

        }
      });
    }

  } catch (JSONException e) {
    e.printStackTrace();
  }

}
&#13;
&#13;
&#13;

它就像一个魅力,感谢所有人。

enter image description here