Android通过ArrayAdapter getview()从数据库获取值

时间:2014-05-30 08:32:00

标签: android android-sqlite android-arrayadapter android-progressbar getview

我正在使用进度条和一些文本创建列表视图。我遇到了如何将数据库值从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),所以任何建议都会感激不尽。

1 个答案:

答案 0 :(得分: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;



      }
}

示例listview进度条可以在这里参考 https://github.com/mdkess/ProgressBarListView