我第二次打开这个话题是因为我找不到问题,没有人无法回复 我在片段中的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);
}
我该如何解决这种情况?
答案 0 :(得分:2)
试试这个......
public class MyDataAdapter extends ArrayAdapter<String> {
private Context c;
private SQL sql;
private LayoutInflater inflater;
public MyDataAdapter(Context context) {
super(context, R.layout.mydata_listview_row, list);
inflater = LayoutInflater.from(context);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
convertView =inflater.inflate(R.layout.mydata_listview_row,null);
try {
holder = new ViewHolder();
holder.txt1 = (TextView)convertView.findViewById(R.id.subject);
holder.txt2= (TextView) convertView.findViewById(R.id.user);
holder.txt3= (TextView)convertView.findViewById(R.id.startDate);
sql = new SQL(c);
sql.openToRead();
listMyDataIds = sql.get(list);
if (i < listMyDataIds.size()){
holder.txt1.setText((sql.get(list);
holder.txt2.setText((sql.get(list));
holder.txt3.setText((sql.get(list);
i++;
}
sql.close();
} catch (Exception e) {
}
return convertView;
}
public static class ViewHolder
{
public TextView txt1;
public TextView txt2;
public TextView txt3;
}
当你初始化Adpter写
adpt = new MyDataAdapter (getApplicationContext(),
R.layout.mydata_listview_row, list);
workList.setAdapter(adpt);