我无法在列表视图中显示已解析的文本

时间:2019-04-18 23:13:36

标签: android listview jsoup

正如您所看到的主题,我无法在2天内解决此问题,我不知道为什么我找不到任何原因,而且我不是一个专业的初学者 谢谢aldready

MainActivity.java

这是我的MainActivity

public class MainActivity extends AppCompatActivity {
private ArrayList<Person> persons;
private ListView listView;
private CustomListViewAdapter listViewAdapter;
String Datas;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    initialize();
    fillArrayList(persons);
}
    private void initialize() {
        persons = new ArrayList<Person>();
        listView = (ListView) findViewById(R.id.person_list_view);
        listViewAdapter = new CustomListViewAdapter(MainActivity.this,persons);
        listView.setAdapter(listViewAdapter);
    }

    private void fillArrayList(ArrayList<Person>persons){
            String veriDizisi[] =new String[500];



            int sayi=0;
            int sayac=0;

            try{
                final StringBuilder builder = new StringBuilder();

                Document doc = Jsoup.connect("http://www.planecrashinfo.com/recent.htm").get();
                String title = doc.title();
                Elements links = doc.select("tr");
                // builder.append(title).append("\n");
                for (Element link : links) {
                    if (sayi>=1){
                        builder.append("\n").append("").append(link.text());
                        Datas= builder.toString();
                        sayi++;
                    }
                    else {

                        sayi++;
                    }

                }

            }catch (Exception e){

                e.printStackTrace();
            }
        for (int index = 0; index < 200; index++) {
            Person person = new Person(index+"",Datas,R.drawable.ic_launcher_background);
            persons.add(person);



        }

    }
 }

Person.java

这是我的Getter Setter类;

public class Person {
private String name;
private final String address;
private int photoId;

public Person(String name,String address,  int photoId) {
    this.name = name;
    this.address = address;
    this.photoId = photoId;

}

public int getPhotoId() {
    return photoId;
}

public String getName() {
    return name;
}

public String getAddress() {return address;}

}

CustomListViewAdapter.java

这是我的适配器类;

public class CustomListViewAdapter extends ArrayAdapter<Person> {

private final LayoutInflater inflater;
private final Context context;
private ViewHolder holder;
private final ArrayList<Person> persons;

public CustomListViewAdapter(Context context, ArrayList<Person> persons) {
    super(context,0, persons);
    this.context = context;
    this.persons = persons;
    inflater = LayoutInflater.from(context);
}

@Override
public int getCount() {
    return persons.size();
}

@Override
public Person getItem(int position) {
    return persons.get(position);
}

@Override
public long getItemId(int position) {
    return persons.get(position).hashCode();
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    if (convertView == null) {

        convertView = inflater.inflate(R.layout.list_view_item, null);

        holder = new ViewHolder();
        holder.personImage = (ImageView) convertView.findViewById(R.id.person_image);
        holder.personNameLabel = (TextView) convertView.findViewById(R.id.person_name_label);
        holder.personAddressLabel = (TextView) convertView.findViewById(R.id.person_address_label);
        convertView.setTag(holder);

    }
    else{
        //Get viewholder we already created
        holder = (ViewHolder)convertView.getTag();
    }

    Person person = persons.get(position);
    if(person != null){
        holder.personImage.setImageResource(person.getPhotoId());
        holder.personNameLabel.setText(person.getName());
        holder.personAddressLabel.setText(person.getAddress());

    }
    return convertView;
}

//View Holder Pattern for better performance
private static class ViewHolder {
    TextView personNameLabel;
    TextView personAddressLabel;
    ImageView personImage;

  }
}

2 个答案:

答案 0 :(得分:0)

尝试更改这两行的顺序:

从这个

initialize();
fillArrayList(persons);

对此:

fillArrayList(persons);
initialize();

答案 1 :(得分:0)

您正在初始化listviewadapter,然后再在人员ArrayList中添加人员项目,这意味着您没有要显示在列表视图中的项目

将代码中的fillArrayList(persons)移动到initialize()上方