0

我第二次打开这个主题是因为我找不到问题而且没有人无法响应我在片段的 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);
}

在此处输入图像描述 在此处输入图像描述 在此处输入图像描述

我该如何解决这种情况?

4

1 回答 1

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;


    }

当你初始化适配器时写

adpt = new MyDataAdapter (getApplicationContext(),
                    R.layout.mydata_listview_row, list);
            workList.setAdapter(adpt);
于 2013-08-29T08:57:13.083 回答