我的密码

public class Products extends AppCompatActivity {
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.addproducts);
        FillList fillList = new FillList();
        fillList.execute("");

    }
    public class FillList extends AsyncTask<String, String, String> {

        List<Map<String, String>> prolist = new ArrayList<Map<String, String>>();
        @Override
        protected void onPreExecute() {

        }

        @Override
        protected void onPostExecute(String r) {


            String[] from = { "E","D", "C", "B","A"};
            int[] views = { R.id.lblMondeh,R.id.lblBes,R.id.lblBed,R.id.lblSharh,R.id.lblDate};
            final SimpleAdapter ADA = new SimpleAdapter(Products.this,
                    prolist, R.layout.lsttemplate, from,
                    views);
            lstSoratHesab.setAdapter(ADA);
            lstSoratHesab.setVisibility(View.VISIBLE);

        if(lstSoratHesab.getChildAt(0-lstSoratHesab.getFirstVisiblePosition())!=null)//all time this line is null
                lstSoratHesab.getChildAt(0).setBackgroundColor(Color.parseColor("#ff00aa63"));//i want change color first row

        }

        @Override
        protected String doInBackground(String... params) {

            Map<String, String> datanum = new HashMap<String, String>();

            datanum.put("E", "e");
            datanum.put("D", "d");
            datanum.put("C", "c");
            datanum.put("B", "b");
            datanum.put("A", "a");
            prolist.add(datanum);

        }
    }
}


我想为第一行设置背景颜色。

为什么lstSoratHesab.getChildAt(0-lstSoratHesab.getFirstVisiblePosition())lstSoratHesab.getChildAt(0)一直为空?

但是列表视图有20行

android - 列表 View getChildAt()为空-LMLPHP

最佳答案

getChildAt(0)将检索第一行元素,但是您需要将其放入线程中,以便有时间在屏幕上显示布局,可以将其设置为:

lstSoratHesab.post(new Runnable() {
    @Override
    public void run() {
        if (lstSoratHesab.getChildAt(0) != null) {
            lstSoratHesab.getChildAt(0)
                    .setBackgroundColor(Color.parseColor("#ff00aa63"));
        }
    }
});

07-27 15:04