androidandroid-sqliteandroid-arrayadapterandroid-progressbargetview

Android get value from database by ArrayAdapter getview()


Im creating a listview with a progressbar and some text. i got a problem on how to take a database value from sqlite to .onProgressUpdate(value)

here are my code with problem class 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;
     }
}

my ProfileProgress class

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;
          }
}

my activity class 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));

}

problem at class ProfileProgressArrayAdapter, I cannot seem to make any database declaration on ProfileProgressArrayAdapter cause it say my database is Database(Context context), so any suggestion will be grateful thx.


Solution

  • I finally answer my own question after doing some research

    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;
    
    
    
          }
    }
    

    Examples listview progress bar can be refer here https://github.com/mdkess/ProgressBarListView