• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Data wont show on second page of Android app after heading selection

Pooveshin

Newbie
Hi All

In my Android App I am currently developing I have a page to show Historic/Archived data, The list view gets populated from the database with only the Name of school and Date. When a user clicks on a particular meeting a Dialog box displays and the option to only view is shown, when a user clicks view a second page opens and the data relating to that meeting shows.

Currently the app shows the meeting and name of school, when a user clicks the item the dialog opens up and a user can click view, My problem is when it goes to the next page to actually view the information no information comes up to be viewed.

If anyone can assist I have attached my code below.

Setting up the database table
Code:
public class UserMeetings extends Table {

public String table_name = "usermeetings";
public String primary_key = "id";
public String [] fillable_fields = new String [] {"name_of_school", "date", "venue", "meeting_notes"};
public Map<String, String> fields = new HashMap<>();
public String[] protected_fields = new String []{};

public UserMeetings()
{
   super ("usermeetings", "id", new String []{"name_of_school", "date", "venue", "meeting_notes"}, new String []{}, new HashMap<String, String>());
}
public String getPrimary_key()
{
   return primary_key;
}
public String getTable_name()
{
   return table_name;
}
public String[] getFillable_fields()
{
   return fillable_fields;
}

@Override
public Map<String, String> getFields()
{
   return fields;
}

@Override
public void setFields(Map<String, String> fields)
{
   this.fields = fields;
}

public String get_name()
{
   if(!fields.get("name_of_school").isEmpty())
       return fields.get("name_of_school");

   return null;
}

public String get_field(String key)
{
   if(fields.containsKey(key) && !fields.get(key).isEmpty())
       return fields.get(key);

   return "";
 }
}

CustomAdapter
Code:
public class CustomUserMeetingsAdapter extends BaseAdapter {

public Context context;
private ArrayList<Map<String, String>> list;
private static LayoutInflater inflater = null;
private ListCallback callback;

public CustomUserMeetingsAdapter(Context mContext, ArrayList<Map<String, String>>list, ListCallback callback)
{
   this.context = mContext;
   this.list = list;
   this.callback = callback;

   inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

@Override
public int getCount()
{
   return list.size();
}

@Override
public Object getItem(int poisition)
{
   return list.get(poisition);
}

@Override
public long getItemId(int position)
{
   return position;
}

@Override
public View getView(final int position, View convertView, ViewGroup parent)
{
   View rowView;

   Map<String, String> d = (Map<String, String>) getItem(position);

   final int item = Integer.parseInt(d.get("id"));
   Log.d("item in adapter", item + "");
   final String name_of_school = d.get("name_of_school");
   final String date = d.get("date");

   rowView = inflater.inflate(R.layout.list_item, null);
   TextView tv = (TextView) rowView.findViewById(R.id.txt_label);
   tv.setText("Meeting : " + name_of_school + " "+ date);

   rowView.setOnClickListener(new View.OnClickListener()
   {
       @Override
       public void onClick(View v)
       {
           AlertDialog.Builder builder = new AlertDialog.Builder(context);
           builder.setNegativeButton("View", new DialogInterface.OnClickListener() {
           public void onClick(DialogInterface dialog, int id)
           {
               callback.edit_process(item);
           }
           });

           /*Map<String, String> where = new HashMap<>();
           where.put("id", item + "");

           DB db = GlobalHelper.getInstance().getDb();
           db.delete_record((new UserMeetings()).getTable_name(), where);

           callback.after_process();*/

           AlertDialog dialog = builder.create();
           dialog.show();
       }
   });
   return rowView;
 }
}

List View to Show Headings and click
Code:
public class UserArchivedData extends AppCompatActivity implements ListCallback{

GlobalHelper gh;
Context mContext;
TableHelper th;
DB db;

ListView lv_records;
ArrayList<Map<String, String>>data;

Button btn_create_meeting;

@Override
protected void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.activity_user_archived_data);

   gh = GlobalHelper.getInstance();

   mContext = this;
   lv_records = (ListView) findViewById(R.id.lv_records);
   th = new TableHelper();

   gh.setmContext(this);

   db = gh.getDb();

   data = new ArrayList<>();

   btn_create_meeting = (Button) findViewById(R.id.btn_create_meeting);
   btn_create_meeting.setOnClickListener(new View.OnClickListener()
   {
       @Override
       public void onClick(View view)
       {
           Intent i = new Intent(UserArchivedData.this, Capture_Meetings.class);
           i.putExtra("create_process", 1);

           startActivity(i);
       }
   }
   );

   Cursor cursor = db.get_data((new UserMeetings().getTable_name()));
   if (cursor.moveToFirst())
   {
       while (cursor.isAfterLast()==false)
       {
           int index_meeting_id = cursor.getColumnIndex("id");
           int index_name_of_school = cursor.getColumnIndex("name_of_school");

           int id = cursor.getInt(index_meeting_id);
           String name_of_school = cursor.getString(index_name_of_school);

           Map<String, String> d = new HashMap<>();
           d.put("id", id + "");
           d.put("name_of_school", name_of_school);

           data.add(d);

           cursor.moveToNext();
       }
   }

   cursor.close();

   process_data();
}

@Override
public void after_process()
{

}

@Override
public void edit_process(int id)
{

}

private void process_data()
{
   CustomUserMeetingsAdapter adapter = new CustomUserMeetingsAdapter(mContext, data, new ListCallback() {
       @Override
       public void after_process() {
           data = new ArrayList<>();

           Cursor cursor = db.get_data((new UserMeetings()).getTable_name());
           if(cursor.moveToFirst())
           {
               while (cursor.isAfterLast() == false)
               {
                   int index_meeting_id = cursor.getColumnIndex("id");
                   int index_name_of_school = cursor.getColumnIndex("name_of_school");

                   int id = cursor.getInt(index_meeting_id);
                   String name_of_school = cursor.getString(index_name_of_school);

                   Map<String, String> d = new HashMap<>();
                   d.put("id", id + "");
                   d.put("name_of_school", name_of_school);

                   data.add(d);

                   cursor.moveToNext();
               }
           }

           cursor.close();
           process_data();
       }

       @Override
       public void edit_process(int id) {
           Intent i = new Intent(UserArchivedData.this, ViewUserArchivedData.class);
           i.putExtra("view", id);
           startActivity(i);

       }
   });
   lv_records.setAdapter(adapter);
 }
}

View to see all Data for specific meeting
Code:
public class ViewUserArchivedData extends AppCompatActivity {

GlobalHelper gh;
DB db;
int id;
Cursor cursor;
UserMeetings name_of_school;

EditText editTextName;
EditText editDateTime;
EditText editVenue;
EditText editTextAddress;

@Override
protected void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.activity_view_user_archived_data);

   gh = GlobalHelper.getInstance();
   gh.setmContext(this);
   db = gh.getDb();

   Intent intent = getIntent();
   id = intent.getIntExtra("View", -1);
   Log.d("id","id" + id);

   editTextName = (EditText)findViewById(R.id.editTextName);
   editDateTime = (EditText) findViewById(R.id.editDateTime);
   editVenue = (EditText) findViewById(R.id.editVenue);
   editTextAddress = (EditText) findViewById(R.id.editTextAddress);

   Map<String, String> where_data = new HashMap<>();
   where_data.put("id",id + "");
   cursor = db.get_data((new UserMeetings()).getTable_name(), where_data);

   if (cursor.moveToFirst())
   {
       name_of_school = new UserMeetings();

       while (cursor.isAfterLast() == false)
       {
           int index_id = cursor.getColumnIndex("id");
           int index_name_of_school = cursor.getColumnIndex("name_of_school");
           int index_date_time = cursor.getColumnIndex("date");
           int index_venue = cursor.getColumnIndex("venue");
           int index_address = cursor.getColumnIndex("meeting_notes");

           int temp_id = cursor.getInt(index_id);
           String str_school = cursor.getString(index_name_of_school);
           String dateTime = cursor.getString(index_date_time);
           String venue = cursor.getString(index_venue);
           String meeting_notes = cursor.getString(index_address);

           Map<String, String> d = new HashMap<>();
           d.put("id", temp_id + "");
           d.put("name_of_school", str_school);
           d.put("date", dateTime);
           d.put("venue", venue);
           d.put("meeting_notes", meeting_notes);

           name_of_school.setFields(d);

           cursor.moveToNext();

       }
   }

   cursor.close();
   Log.d("school is","school is");

   if (name_of_school != null)
   {
       Log.d("user is not null", "user is not null");
       editTextName.setText(name_of_school.get_field("name_of_school"));
       editDateTime.setText(name_of_school.get_field("date"));
       editVenue.setText(name_of_school.get_field("venue"));
       editTextAddress.setText(name_of_school.get_field("meeting_notes"));
   }
   else
   {
       Log.d("user is null", "user is null");
   }
  }
}
 
First thing to check is that the DB query in ViewUserArchivedData returns the data you expect.

Does your cursor contain any records?

Set a breakpoint at line 35 and step into db.get_data(). You haven't included the code for this so we can't see what it's doing.

This looks a bit suspect because you're creating a new UserMeetings object, but don't initialise it.

Code:
db.get_data((new UserMeetings())
 
First thing to check is that the DB query in ViewUserArchivedData returns the data you expect.

Does your cursor contain any records?

Set a breakpoint at line 35 and step into db.get_data(). You haven't included the code for this so we can't see what it's doing.

This looks a bit suspect because you're creating a new UserMeetings object, but don't initialise it.

Code:
db.get_data((new UserMeetings())
Hi

In ViewUserArchivedData line 50 I have cursor = db.get_data((new UserMeetings())
 
What does your actual DB query look like?
Code:
public Cursor get_data(String table_name, Map<String, String> where) {
    SQLiteDatabase sqLiteDatabase = this.getWritableDatabase();
    String query = "SELECT * FROM " + table_name;

    try {
        int count = where.size();
        int loop = 1;

        if (count > 0) {
            query += " WHERE ";
        }

        for (Map.Entry<String, String> entry : where.entrySet()) {
            query += entry.getKey() + " = '" + entry.getValue() + "'";
            if (loop++ < count) {
                query += " AND ";
            }
        }
    } catch (Exception e) {

    }

    Cursor c = sqLiteDatabase.rawQuery(query, null);
    return c;
}
 
I just noticed one inconsistency in your code. You use the following to put a value into your Intent

Code:
i.putExtra("view", id);

and use the following code to extract it

Code:
id = intent.getIntExtra("View", -1);

I'm pretty sure that the case matters, so "view" is not the same as "View". Make them both exactly the same.
 
I just noticed one inconsistency in your code. You use the following to put a value into your Intent

Code:
i.putExtra("view", id);

and use the following code to extract it

Code:
id = intent.getIntExtra("View", -1);

I'm pretty sure that the case matters, so "view" is not the same as "View". Make them both exactly the same.
Thank you, that has solved the problem.
 
I don't often request a 'best answer', but I feel that one deserves it ;)
 
I don't often request a 'best answer', but I feel that one deserves it ;)
Hi There

Just a quick one, in the CustomAdapter between line 43 and 46 I am grabbing the school and date from the database to display in the selection so the user knows which school and date they selecting to view, the school name displays fine but the date shows null. What could be the problem?
 
Back
Top Bottom