我在片段中的tabhost中有自定义列表视图。 当我滚动自定义列表视图时,TextView内容会破坏,看起来似乎是空的。这是我的代码
public class myFragment extends Fragment {
private TabHost tabHost;
private View view;
private ArrayList<String> list = new ArrayList<String>();
private ArrayList<String> listMyDataIds = new ArrayList<String>();
private ListView lstViewMyData;
private SQL sql;
private MyDataAdapter adpMyData;
private int i = 0;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
view = inflater.inflate(R.layout.activity_content, container, false);
initialize();
sql = new SQL(view.getContext());
sql.openToRead();
listMyDataIds = sql.get(list);
sql.close();
adpMyData = new MyDataAdapter(view.getContext());
return view;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
sql = new SQL(view.getContext());
sql.openToRead();
listMyDataIds = sql.get(list);
String[] stockArr = new String[listMyDataIds.size()];
stockArr = listMyDataIds.toArray(stockArr);
lstViewMyData.setAdapter(adpMyData);
sql.close();
}
public class MyDataAdapter extends ArrayAdapter<String> {
private Context c;
private SQL sql;
public MyDataAdapter(Context context) {
super(context, R.layout.mydata_listview_row, list);
this.c = context;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final LayoutInflater inflater = LayoutInflater.from(c);
View v = inflater.inflate(R.layout.mydata_listview_row, parent, false);
TextView txt1 = (TextView) v.findViewById(R.id.txt1);
TextView txt2 = (TextView) v.findViewById(R.id.txt2);
TextView txt3 = (TextView) v.findViewById(R.id.txt3);
sql = new SQL(c);
sql.openToRead();
listMyDataIds = sql.get(list);
if (i < listMyDataIds.size()){
txt1.setText((sql.get(list);
txt2.setText((sql.get(list));
txt3.setText((sql.get(list);
i++;
}
sql.close();
return v;
}
}
private void initialize() {
tabHost=(TabHost) view.findViewById(R.id.tabHost);
tabHost.setup();
TabSpec spec1=tabHost.newTabSpec("TAB 1");
spec1.setContent(R.id.tab1);
TabSpec spec2=tabHost.newTabSpec("TAB 2");
spec2.setContent(R.id.tab2);
TabSpec spec3=tabHost.newTabSpec("TAB 3");
spec3.setContent(R.id.tab3);
tabHost.addTab(spec1);
tabHost.addTab(spec2);
tabHost.addTab(spec3);
// tabHost.setCurrentTab(0);
lstViewMyData = (ListView) view.findViewById(R.id.lstMyData);
}
我该如何解决这种情况?