Android開発ListViewに関する基礎的な使い方(主観的理解)
5145 ワード
とても簡単に見えますが、実は泥棒ギルは面倒です.
ListViewで行うべき3つのポイント:
第一点は省略して、この自主設計でいいです.
第2第3点を述べる.
まずアダプタ類を書き直さなければなりません.萌えの新しい私をやめてください.
public class MyAdapter extends BaseAdapter {
public List mList;//
public Context mContext;//
public LayoutInflater mLayoutInflater;// listView
public MyAdapter(Context context){//
mList = new ArrayList<>();
getDatabase();
mContext = context;
mLayoutInflater = LayoutInflater.from(context);
}
@Override
public int getCount() {//
return mList.size();
}
@Override
public Object getItem(int position) {//
return mList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
if (convertView==null) {
viewHolder = new ViewHolder();
convertView = mLayoutInflater.inflate(R.layout.count_item_bigger, null);
viewHolder.date = convertView.findViewById(R.id.date);
viewHolder.totalMoney = convertView.findViewById(R.id.total_money);
viewHolder.name = convertView.findViewById(R.id.name);
viewHolder.type = convertView.findViewById(R.id.type);
viewHolder.money = convertView.findViewById(R.id.money);
viewHolder.time = convertView.findViewById(R.id.time);
viewHolder.date_bar = convertView.findViewById(R.id.date_bar);
convertView.setTag(viewHolder);// ,
}else{
viewHolder= (ViewHolder) convertView.getTag();//
}
// position
Data data = mList.get(position);
// if else ,
if(data.getDate().equals("") && data.getTotal_money().equals("")){
viewHolder.date_bar.setVisibility(View.GONE);
}
else {
viewHolder.date.setText(data.getDate());
viewHolder.totalMoney.setText(data.getTotal_money());
}
//
if(!data.getDate().equals("")){// , , orz
viewHolder.date_bar.setVisibility(View.VISIBLE);
}
//
viewHolder.name.setText(data.getName());
viewHolder.type.setText(data.getType());
viewHolder.money.setText(data.getMoney());
viewHolder.time.setText(data.getTime());
return convertView;
}
//
private static class ViewHolder{
public TextView date;
public TextView totalMoney;
public TextView name;
public TextView type;
public TextView money;
public TextView time;
public LinearLayout date_bar;
}
// , , ,
private void getDatabase(){
Data data = new Data(" ", "+10000.00", " ", " ", "+1000.00", "15:00");
mList.add(data);
}
}
アダプタの作成
listBigger = findViewById(R.id.count_list_bigger);// listView
MyAdapter adapter = new MyAdapter(this);//
listBigger.setAdapter(adapter);//
そしてあなたのデータにクラスを分けなければなりません
public class Data {
private String date;
private String total_money;
private String name;
private String type;
private String money;
private String time;
public Data(String data, String total_money, String name, String type, String money, String time){
this.date = data;
this.total_money = total_money;
this.name = name;
this.type = type;
this.money = money;
this.time = time;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public String getTotal_money() {
return total_money;
}
public void setTotal_money(String total_money) {
this.total_money = total_money;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getMoney() {
return money;
}
public void setMoney(String money) {
this.money = money;
}
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
}
そしてそれでいい!
面倒くさいなorz