我从互联网上获取一些工作信息,并准备使用Listview进行显示。虽然我使用RxJava来处理多个线程,但列表项无法显示。List item cannot display getting data using RxJava 我使用了一些测试数据,它可以显示Use an array as data
public class MainActivity extends AppCompatActivity {
private Toolbar mToolbar;
private ActionBar mActionbar;
private ListView mSeminarList;
private SeminarListAdapter mSeminarListAdapter;
private ArrayList<SeminarListItem> mSeminarData;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initToolbar();
initSlideMenu();
initListView();
}
private void initToolbar() {
mToolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(mToolbar);
mActionbar = getSupportActionBar();
mActionbar.setTitle("爱实习 爱工作");
mToolbar.setNavigationIcon(R.mipmap.ic_menu_white_36dp);
}
private void initSlideMenu() {
PrimaryDrawerItem priItemHome = new PrimaryDrawerItem()
.withName("首页");
SecondaryDrawerItem subItemSeminar = new SecondaryDrawerItem()
.withName("宣讲信息");
SecondaryDrawerItem subItemIntern = new SecondaryDrawerItem()
.withName("实习信息");
SecondaryDrawerItem subItemJob = new SecondaryDrawerItem()
.withName("工作信息");
Drawer result = new DrawerBuilder()
.withActivity(this)
.withToolbar(mToolbar)
.addDrawerItems(
priItemHome, new DividerDrawerItem(),
subItemSeminar, subItemIntern, subItemJob
)
.build();
}
private void initListView() {
mSeminarList = (ListView) findViewById(R.id.listview_seminar);
mSeminarData = new ArrayList<SeminarListItem>();
// getListData();
getListDataTest();
}
private void getListData() {
Observable.create(new Observable.OnSubscribe<Elements>() {
public void call(Subscriber<? super Elements> subscriber) {
try {
String url = "http://xjh.haitou.cc/sh/uni-132";
Connection conn = Jsoup.connect(url);
conn.header("User-Agent", "Mozilla/5.0 (X11; Linux x86_64; rv:32.0) Gecko/20100101 Firefox/32.0");
Document doc = conn.get();
Elements elements = doc.select("tbody tr");
subscriber.onNext(elements);
subscriber.onCompleted();
} catch (Exception e) {
Log.d("Exception", e.toString());
}
}
}).subscribeOn(Schedulers.io())
.subscribe(new Observer<Elements>() {
@Override
public void onCompleted() {
Log.d("onCompleted", "test");
if (mSeminarListAdapter == null) {
mSeminarListAdapter = new SeminarListAdapter(getApplicationContext(), R.layout.list_item, mSeminarData);
}
mSeminarList.setAdapter(mSeminarListAdapter);
mSeminarListAdapter.notifyDataSetChanged();
}
@Override
public void onError(Throwable e) {
}
@Override
public synchronized void onNext(Elements elements) {
for (Element element : elements) {
String company = element.getElementsByClass("company").text();
String link = element.select("a").attr("abs:href").toString();
String time = element.getElementsByClass("hold-ymd").text();
String address = element.getElementsByClass("text-ellipsis").text();
SeminarListItem item = new SeminarListItem();
item.setCompanyName(company);
item.setTime("举办时间: " + time);
item.setPlace("举办地点: " + address);
item.setInfoUrl(link);
mSeminarData.add(item);
Log.d("test company name", company);
}
}
});
}
private void getListDataTest() {
for (int i=0; i<10; i++) {
SeminarListItem item = new SeminarListItem();
item.setCompanyName("company" + i);
item.setPlace("place" + i);
item.setTime("time" + i);
mSeminarData.add(item);
}
if (mSeminarListAdapter == null) {
mSeminarListAdapter = new SeminarListAdapter(MainActivity.this, R.layout.list_item, mSeminarData);
}
mSeminarList.setAdapter(mSeminarListAdapter);
mSeminarListAdapter.notifyDataSetChanged();
}
}
以下是我的自定义适配器。
public class SeminarListAdapter extends ArrayAdapter<SeminarListItem> {
Context mContext;
int mResId;
ArrayList<SeminarListItem> mSeminarListData;
public SeminarListAdapter(Context context, int resource, ArrayList<SeminarListItem> object) {
super(context, resource, object);
this.mContext = context;
this.mResId = resource;
this.mSeminarListData = object;
}
public void setSeminarListData(ArrayList<SeminarListItem> data) {
this.mSeminarListData = data;
notifyDataSetChanged();
}
@Override
public int getCount() {
if (mSeminarListData == null) {
return 0;
} else {
return mSeminarListData.size();
}
}
@Override
public SeminarListItem getItem(int position) {
if (mSeminarListData == null) {
return null;
} else {
return mSeminarListData.get(position);
}
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
LayoutInflater inflater = ((Activity)mContext).getLayoutInflater();
convertView = inflater.inflate(mResId, parent, false);
holder = new ViewHolder();
holder.companyName = (TextView) convertView.findViewById(R.id.textview_company_name);
holder.time = (TextView) convertView.findViewById(R.id.textview_time);
holder.place = (TextView) convertView.findViewById(R.id.textview_place);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
SeminarListItem item = mSeminarListData.get(position);
holder.companyName.setText(item.getCompanyName());
holder.time.setText(item.getTime());
holder.place.setText(item.getPlace());
return convertView;
}
class ViewHolder {
public TextView companyName;
public TextView time;
public TextView place;
}
}
当我调用功能getListDataTest()
时,它会成功显示,如果我调用功能getListData()
,它将失败。我已经调试过,发现适配器中的函数getView()
不会被调用。
我怀疑,如果我得到了论证&#34; context&#34;在新线程中使用MainActivity,它将与&#34; context&#34;来自UI主线程。
答案 0 :(得分:3)
您需要在mainthread上执行listview操作,即notifyDataSetChanged。最简单的方法是使用rxandroid并添加
.observeOn(AndroidSchedulers.mainThread())
在订阅observable之前。