如果未在onCreate()上创建适配器,则Android列表视图会更改颜色

时间:2015-11-28 17:28:29

标签: android listview

当我从服务器加载数据后创建适配器时,ListView有一个奇怪的行为,而不是我在onCreate函数中创建适配器时。 如果在onNext()函数而不是onCreate()上创建列表,则列表中文本的颜色将变为白色。 任何想法可能是什么?

public class PdfListActivity extends AppCompatActivity
{
private StableArrayAdapter adapter;
private PdfModelView modelView;
private ListView listview;
private Context instanceVar;
private ArrayList<PdfRetrive> dataObj;

@Bind(R.id.toolbar)
Toolbar toolbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pdf_list);

listview = (ListView) findViewById(R.id.listviewdoc);

instanceVar = getApplicationContext();

modelView = new PdfModelView();
modelView.getPdf().subscribe(pdfSubscriber);

listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {

    @Override
    public void onItemClick(AdapterView<?> parent, final View view,
                            int position, long id) {
        final String item = (String) parent.getItemAtPosition(position);

        ArrayList<VOS.PdfObject> pdfArr = (ArrayList<VOS.PdfObject>)      dataObj.get(position).pdfs;

        ArrayList<String> list = new ArrayList<String>();
        ArrayList<String> urlArr = new ArrayList<String>();
        int total = pdfArr.size();

        for (int i = 0; i < total; ++i) {
            list.add(pdfArr.get(i).title);
            urlArr.add(pdfArr.get(i).url);
        }

        Intent i = new Intent(getApplicationContext(), PdfSecondListActivity.class);
        i.putExtra("pdf", list);
        i.putExtra("url", urlArr);
        startActivity(i);
    }

});

}
private Subscriber pdfSubscriber = new     Subscriber<VOS.BackEndResponseObject<ArrayList<PdfRetrive>>>() {
@Override
public void onCompleted()
{
}

@Override
public void onError(Throwable e)
{
    Log.d("pdfs", "recived");
}

@Override
public void onNext(VOS.BackEndResponseObject<ArrayList<PdfRetrive>> arrayListBackEndResponseObject)
{
    dataObj = arrayListBackEndResponseObject.data;
    final ArrayList<String> list = new ArrayList<String>();
    int total = arrayListBackEndResponseObject.data.size();

    for (int i = 0; i < total; ++i) {
        list.add(arrayListBackEndResponseObject.data.get(i).name);
    }

    adapter  = new StableArrayAdapter(instanceVar, android.R.layout.simple_list_item_1, list);
    listview.setAdapter(adapter);
}
};

1 个答案:

答案 0 :(得分:0)

似乎这是我如何初始化适配器的问题: 我本应该去的:

adapter = new StableArrayAdapter(PdfListActivity.this, android.R.layout.simple_list_item_1, list);

这解决了我的问题。