I need to create a android app to list and insert data using sqlite. I just researched and got the basics for lists, adapters, communicating and handling trough activities. My trouble now is how can I correctly separate the classes so my app can do things nicely.
I'll try to explain it here.
The app will have 4 main objects nested. [Project] can have one or more [Uc], which can have one or more [Environment], which can have one or more [Section]. each of these objects should let the user insert, delete, update it's data like, Project name, or Section data and the Object itself destroying all it's child's if deleted.
In php I just put these methods inside each of it's class. But I'm in serious troubles to achieve that on android studio.
I know it's a little vague topic, because I'm new on it. But if you guys have any tutorial or example that can help with it, I would love it.
Bellow is a example of what I want to achieve in each class.
public class projeto {
String projetoNome;
String projeotDescricao;
public void deleteProject () {}
public int countProjects () {}
public void updateProject () {}
public void newProject () {}
}
for explanation purposes follows the code I'm working on, it list items that will be the most part of the app job. This is the same code used in this tutorial: http://ift.tt/1t2yiPA
package test.test2;
import android.content.Context;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends ActionBarActivity {
private DbAdapter Db;
public class codeLeanChapter {
String chapterName;
String chapterDescription;
}
CodeLearnAdapter chapterListAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_view_with_simple_adapter);
chapterListAdapter = new CodeLearnAdapter();
ListView codeLearnLessons = (ListView)findViewById(R.id.listView1);
codeLearnLessons.setAdapter(chapterListAdapter);
codeLearnLessons.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
codeLeanChapter chapter = chapterListAdapter.getCodeLearnChapter(arg2);
Toast.makeText(MainActivity.this, chapter.chapterName, Toast.LENGTH_LONG).show();
}
});
}
public class CodeLearnAdapter extends BaseAdapter {
List<codeLeanChapter> codeLeanChapterList = getDataForListView();
@Override
public int getCount() {
// TODO Auto-generated method stub
return codeLeanChapterList.size();
}
@Override
public codeLeanChapter getItem(int arg0) {
// TODO Auto-generated method stub
return codeLeanChapterList.get(arg0);
}
@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return arg0;
}
@Override
public View getView(int arg0, View arg1, ViewGroup arg2) {
if(arg1==null)
{
LayoutInflater inflater = (LayoutInflater) MainActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
arg1 = inflater.inflate(R.layout.listitem, arg2,false);
}
TextView chapterName = (TextView)arg1.findViewById(R.id.textView1);
TextView chapterDesc = (TextView)arg1.findViewById(R.id.textView2);
codeLeanChapter chapter = codeLeanChapterList.get(arg0);
chapterName.setText(chapter.chapterName);
chapterDesc.setText(chapter.chapterDescription);
return arg1;
}
public codeLeanChapter getCodeLearnChapter(int position)
{
return codeLeanChapterList.get(position);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.list_view_with_simple_adapter, menu);
return true;
}
public List<codeLeanChapter> getDataForListView()
{
List<codeLeanChapter> codeLeanChaptersList = new ArrayList<codeLeanChapter>();
Db = new DbAdapter(this);
Db.openToWrite();
Db.deleteAll();
Db.insert("ABCDE");
Db.insert("FGHIJK");
Db.insert("1234567");
Db.insert("890");
Db.insert("Testing");
Db.insert("Testing");
Db.insert("Testing");
Db.insert("Testing");
Db.insert("1234567");
Db.insert("890");
Db.close();
/*
* Open the same SQLite database
* and read all it's content.
*/
Db = new DbAdapter(this);
Db.openToRead();
String contentRead = Db.queueAll();
String array[] = new String [10];
array = contentRead.split("\n");
Db.close();
for(int i=0;i<10;i++)
{
codeLeanChapter chapter = new codeLeanChapter();
chapter.chapterName = array[i];
chapter.chapterDescription = "This is description for chapter "+i;
codeLeanChaptersList.add(chapter);
}
return codeLeanChaptersList;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Aucun commentaire:
Enregistrer un commentaire