AndroidのBmobから取得したデータをListViewに表示

3469 ワード

一、Bmob 1、Bmobの公式アドレス2を熟知し、androidに関するBmobのクイックエントリードキュメントを表示する:リンク二、ListView 1の理解、汎クラス2の構築、アダプタ3の構築、アダプタの構築のlayout 4、イベントでListView 3の起動、ソース汎クラスPerson
public class Person extends BmobObject {
    private String name;
    private String address;



    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }
}


アダプタpersonAdapte
public class PersonAdapte extends ArrayAdapter{
    private int resourceId;
    public PersonAdapte(Context context, int resource, List objects) {
        super(context, resource,objects);
        resourceId =resource;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Person person = getItem(position);
        View view = LayoutInflater.from(getContext()).inflate(resourceId,parent,false);
        TextView textView1 = view.findViewById(R.id.textView1);
        TextView textView2 = view.findViewById(R.id.textView2);
        textView1.setText(person.getName());
        textView2.setText(person.getAddress());
        return view;
    }
}

アダプタLayout




    

    


アクティブソース
public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
//             
        Bmob.initialize(this, "   Bmob  ID");
   //      Bmob    , ListView  
          BmobQuery bmobQuery = new BmobQuery();
        bmobQuery.findObjects(new FindListener() {
            @Override
            public void done(List list, BmobException e) {
              if(e==null){
                  Log.d("path","    ");
                  PersonAdapte personAdapte = new PersonAdapte(MainActivity.this,R.layout.personlayout,list);
                  ListView listView = findViewById(R.id.listView);
                  listView.setAdapter(personAdapte);
              }
              else{
                  Log.d("path","     ");
              }
            }
        });
        }
}

イベント