android - ListView getChildAt() 为空

标签 android listview

我的代码

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) 所有时间都为空?

但是 ListView 有 20 行

enter image description here

最佳答案

getChildAt(0) 将检索第一行元素,但您需要在线程中获取它,以便让布局在屏幕上显示的时间,可以设置为:

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

关于android - ListView getChildAt() 为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41268158/

相关文章:

wpf - 单击空白处时如何处理事件?

vb.net - 我如何在VB中的 ListView 控件中显示视频的图像缩略图?

android - 如何在android底部导航 View 中更改文本填充

android - 如何获取recyclerview Item位置的文档id?

javascript - 通过 JavaScript 检测和区分 Android 平板电脑和 Android 手机?

android - Jetpack Compose 预览未显示

android - Dart , flutter - ListView 不显示

android - 删除过滤器文本后,CustomArrayAdapter 不会刷新

android - 我可以在运行时在 ListView 中插入一列吗?

android - FacebookSDK 找不到资源(Intellij idea)