2015/8/25问题集
pager页面加载顺序问题
ViewPager页面加载时要先把Pager加载进来,然后才能加载里面的控件
pager先加载进来的layout
用pager加载的页面,只预先加载了一个页面。
而其他页面的控件并没有加载进来,这就导致了null指针异常。
解决办法:在前面先加上一个view名称。
ll= (LinearLayout) myView.findViewById(R.id.ll);
listview绑定adapter
这回竟然是因为忘记吧List NEW出来了
package com.zsz.develop.wisdomcommunity.my;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import com.zsz.develop.wisdomcommunity.R;
import java.util.ArrayList;
import java.util.List;
public class MyHomeActivity extends AppCompatActivity {
private ListView lvMyHome;
private MyHomeAdapter adapter;
private List<MyHomeData> myHomeLists;
private MyHomeData myHomeData;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_home);
lvMyHome= (ListView) findViewById(R.id.lvMyHome);
lvMyHome=new ListView(this);
myHomeLists=new ArrayList<>();
for (int i=0;i<5;i++){
myHomeData=new MyHomeData(i+" 父子","天大地大","审核中");
myHomeLists.add(myHomeData);
}
adapter=new MyHomeAdapter(MyHomeActivity.this,myHomeLists);
lvMyHome.setAdapter(adapter);
setContentView(lvMyHome);
}
}
listview绑定adapter之感悟
首先在这里面的设置:
- 1.首先需要准备的数据listview承载数据,Adapter适配数据,data是数据。还需要一个装data的list集合
- 找到listview并实例化。(并不需要实例化)
- 实例化list和data,并把data装进list中
- 把list装进adapter中
- 绑定adapter
- setContentView(lvMyHome);(错误:这也重新覆盖掉了布局)
-
更改后的代码
package com.zsz.develop.wisdomcommunity.myOneLine;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Window;
import android.widget.ListView;
import com.zsz.develop.wisdomcommunity.R;
import java.util.ArrayList;
import java.util.List;
public class MyBillctivity extends AppCompatActivity {
private List<MyBillData> myBillDataList;
private MyBillAdapter adapter;
private ListView lvMyBill;
private MyBillData myBillData;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_bill);
lvMyBill= (ListView) findViewById(R.id.lvMyBill);
//不要 lvMyBill=new ListView(this);
myBillDataList=new ArrayList<>();
for (int i=1;i<5;i++){
myBillData=new MyBillData("2015-1-1至2015-1-31","未交","35"+i);
myBillDataList.add(myBillData);
}
adapter=new MyBillAdapter(this,myBillDataList);
lvMyBill.setAdapter(adapter);
//不要 setContentView(lvMyBill);
}
}
Adapter的操作
错误之:setTag()这里是传入整个类。而不是字符串
package com.zsz.develop.wisdomcommunity.myOneLine;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import com.zsz.develop.wisdomcommunity.R;
import java.util.List;
/**
* Created by shengzhong on 2015/8/25.
*/
public class MyBillAdapter extends BaseAdapter {
private Context context;
private List<MyBillData> myBillDataList;
public MyBillAdapter(Context context,List<MyBillData> myBillDataList){
this.context=context;
this.myBillDataList=myBillDataList;
}
@Override
public int getCount() {
return myBillDataList.size();
}
@Override
public Object getItem(int i) {
return myBillDataList.get(i);
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
ViewHolder holder;
if (view==null) {
view= LayoutInflater.from(context).inflate(R.layout.my_bill_layout,null);
holder=new ViewHolder();
holder.tv_my_bill_layout_Time= (TextView) view.findViewById(R.id.tv_my_bill_layout_Time);
holder.tv_my_bill_layout_CostState= (TextView) view.findViewById(R.id.tv_my_bill_layout_CostState);
holder.tv_my_bill_layout_Cost= (TextView) view.findViewById(R.id.tv_my_bill_layout_Cost);
holder.tv_my_bill_layout_Time.setText(myBillDataList.get(i).getTime());
holder.tv_my_bill_layout_CostState.setText(myBillDataList.get(i).getCoatState());
holder.tv_my_bill_layout_Cost.setText(myBillDataList.get(i).getCoat());
view.setTag(holder);
}else {
holder= (ViewHolder) view.getTag();
holder.tv_my_bill_layout_Time.setText(myBillDataList.get(i).getTime());
holder.tv_my_bill_layout_CostState.setText(myBillDataList.get(i).getCoatState());
holder.tv_my_bill_layout_Cost.setText(myBillDataList.get(i).getCoat());
}
return view;
}
private static class ViewHolder{
TextView tv_my_bill_layout_Time;
TextView tv_my_bill_layout_CostState;
TextView tv_my_bill_layout_Cost;
}
}
如果要对listview的每个item添加一个点击事件操作
lvMyInterflowMain.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Intent intent=new Intent(MyInterflowMainActivity.this,MyInterflowActivity.class);
startActivity(intent);
}