I'm new to android java coding so bear with me. thanks ^^ So I'm building an app which shows some Life Hacks which I want to populate a ListView in one of the Activities from an existing sqlite database. Most tutorials I searched for included the creating of the database from within the app but what I want is to read from a database (example.sqlite) and not create or edit it. For now, I have created string arrays to store my values to be displayed but I want to populate it from a database as storing 400 values (my target) inside a string array is not as efficient as inside a database. Maybe there have been some tutorials with the purpose I seek but I did not understand so I apologize again but hope for someone to help me by building the pieces of code for my purpose and explaining it. Thanks again ^^
Here is my activity's .java code
public class HealthSub1 extends AppCompatActivity {
ListView list;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_health_sub1);
list= (ListView) findViewById(R.id.healthsub1list);
list.setAdapter(new HealthSub1Adapter(this));
}
}
class HealthSub1SingleHack {
String healthsub1_title;
String healthsub1_desc;
HealthSub1SingleHack(String healthsub1_title, String healthsub1_desc)
{
this.healthsub1_title=healthsub1_title;
this.healthsub1_desc=healthsub1_desc;
}
}
class HealthSub1Adapter extends BaseAdapter {
ArrayList<HealthSub1SingleHack> list;
Context context;
HealthSub1Adapter(Context c)
{
context=c;
list= new ArrayList<>();
Resources res=c.getResources();
String[] titles=res.getStringArray(R.array.healthsub1_title);
String[] desc=res.getStringArray(R.array.healthsub1_desc);
for(int i=0;i<5;i++)
{
list.add(new HealthSub1SingleHack(titles[i], desc[i]));
}
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int i) {
return list.get(i);
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(int i, View View, ViewGroup viewGroup) {
LayoutInflater inflater= (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View row=inflater.inflate(R.layout.healthhack,viewGroup,false);
TextView title= (TextView) row.findViewById(R.id.healthsub1hacktitle);
TextView desc= (TextView) row.findViewById(R.id.healthsub1hackdesc);
HealthSub1SingleHack temp=list.get(i);
title.setText(temp.healthsub1_title);
desc.setText(temp.healthsub1_desc);/
return row;
}
}
Aucun commentaire:
Enregistrer un commentaire