0

我正在创建一个带有进度条和一些文本的列表视图。我遇到了关于如何将数据库值从 sqlite 获取到的问题.onProgressUpdate(value)

这是我的问题类 ProfileProgressArrayAdapter 的代码

public class ProfileProgressArrayAdapter extends ArrayAdapter<ProfileProgress> {
     Context context;
     String year;
     int dbmark;
     private static class ViewHolder {
            TextView textView1, textView2;
            ProgressBar progressBar;
            ProfileProgress info;

           }

      private static final String TAG = ProfileProgressArrayAdapter.class.getSimpleName();

      public ProfileProgressArrayAdapter(Context context, int textViewResourceId,
            List<ProfileProgress> objects) {
            super(context, textViewResourceId, objects);

          }

      @Override
      public View getView(int position, View convertView, ViewGroup parent) {
        View row = convertView;
        final ProfileProgress info = getItem(position);

        ViewHolder holder = null;

        if(null == row) {
          LayoutInflater inflater = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
          row = inflater.inflate(R.layout.profile_pbrow, parent, false);


          holder = new ViewHolder();
          holder.textView1 = (TextView) row.findViewById(R.id.pbrow_tv1);
          holder.textView2 = (TextView) row.findViewById(R.id.pbrow_tv2);
          holder.progressBar = (ProgressBar) row.findViewById(R.id.pbrow_pbar);
          holder.info = info;


          row.setTag(holder);
        } else {
          holder = (ViewHolder) row.getTag();

          holder.info.setProgressBar(null);
          holder.info = info;
          holder.info.setProgressBar(holder.progressBar);

        }

        year = info.getYear();
        //dbmark=value from database need help here !!!!
            holder.textView1.setText(info.getYear());
            holder.textView2.setText("YOUR SCORE IS "+info.getProgress()+" /40");
            holder.progressBar.setProgress(info.getProgress());
            holder.progressBar.setMax(info.getMarkSize());
            info.setProgressBar(holder.progressBar);

            task.onProgressUpdate(dbmark);
                return row;
     }
}

我的 ProfileProgress 课程

public class ProfileProgress {
    private final static String TAG = ProfileProgress.class.getSimpleName();
     public enum ProgressState {
            NOT_STARTED,
            IN_PROGRESS,
            COMPLETE
          }
      private volatile ProgressState pProgress = ProgressState.NOT_STARTED;
      private final String pyear;
      private volatile Integer pprogress;
      private final Integer pmark;
      private volatile ProgressBar mProgressBar;

      public ProfileProgress (String year, Integer mark){
          pyear=year;
          pprogress=0;
          pmark=mark;
          mProgressBar=null;
      }

      public ProgressBar getProgressBar() {
            return mProgressBar;
          }
      public void setProgressBar(ProgressBar progressBar) {
            Log.d(TAG, "setProgressBar " + pyear + " to " + progressBar);
            mProgressBar = progressBar;
          }

      public void setProgressState(ProgressState state) {
            pProgress = state;
          }
      public ProgressState getProgressState() {
            return pProgress;
          }
      public Integer getProgress() {
            return pprogress;
          }
      public void setProgress(Integer progress) {
            this.pprogress = progress;
          }
      public Integer getMarkSize() {

          return pmark;
          }

          public String getYear() {
            return pyear;
          }
}

我的活动课 ProfileActivity

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_profile);

    ListView listView = (ListView) findViewById(R.id.profile_ylistView);
    List<ProfileProgress> progressinfo = new ArrayList<ProfileProgress>();
    for(int i = 2005; i < 2013; ++i) {

        progressinfo.add(new ProfileProgress(""+ i, 40));

    }

    listView.setAdapter(new ProfileProgressArrayAdapter(getApplicationContext(), R.id.profile_ylistView, progressinfo));

}

ProfileProgressArrayAdapter 类的问题,我似乎无法在 ProfileProgressArrayAdapter 上进行任何数据库声明,因为它说我的数据库是 Database(Context context),所以任何建议都将不胜感激。

4

1 回答 1

0

在做了一些研究后,我终于回答了我自己的问题

public class ProfileProgressArrayAdapter extends ArrayAdapter<ProfileProgress> {
     Context context;
     String year,mark;
     int dbmark;

   //declare database (context)
     DatabaseAdapter db = new DatabaseAdapter (getContext());
     private static class ViewHolder {
            TextView textView1, textView2;
            ProgressBar progressBar;
            ProfileProgress info;

           }

      private static final String TAG = ProfileProgressArrayAdapter.class.getSimpleName();

      public ProfileProgressArrayAdapter(Context context, int textViewResourceId,
            List<ProfileProgress> objects) {
            super(context, textViewResourceId, objects);

          }

      @Override
      public View getView(int position, View convertView, ViewGroup parent) {
        View row = convertView;
        final ProfileProgress info = getItem(position);

        ViewHolder holder = null;

        if(null == row) {
          LayoutInflater inflater = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
          row = inflater.inflate(R.layout.profile_pbrow, parent, false);


          holder = new ViewHolder();
          holder.textView1 = (TextView) row.findViewById(R.id.pbrow_tv1);
          holder.textView2 = (TextView) row.findViewById(R.id.pbrow_tv2);
          holder.progressBar = (ProgressBar) row.findViewById(R.id.pbrow_pbar);
          holder.info = info;

          row.setTag(holder);
        } else {
          holder = (ViewHolder) row.getTag();

          holder.info.setProgressBar(null);
          holder.info = info;
          holder.info.setProgressBar(holder.progressBar);

        }

        year = info.getYear();
        //From my database class method 
        HashMap<String, String> a= db.getMark(year);
        mark = a.get("MARK");

        dbmark=Integer.parseInt(mark);

        //Toast.makeText(getContext(), "mark ="+mark+" for year "+year, Toast.LENGTH_LONG).show();
        holder.textView1.setText(info.getYear());
        holder.textView2.setText("YOUR SCORE IS "+info.getProgress()+" /40");
        holder.progressBar.setProgress(info.getProgress());
        holder.progressBar.setMax(info.getMarkSize());
        info.setProgressBar(holder.progressBar);

        ProfileProgressTask task = new ProfileProgressTask(info);
        task.onProgressUpdate(dbmark);
        return row;



      }
}

示例列表视图进度条可以参考这里 https://github.com/mdkess/ProgressBarListView

于 2014-06-03T06:37:41.207 回答