Urgent: Need help with this, I have already makes changes from changing the string to float and my application still cannot start. I have tried debugging, it also return null. For the AddResult and UpdateResult in MainActivity.java it indicates it was never used. Please someone please help me see where is wrong.
Below are my codes
Scenario:
Name: (Allow user to enter name)
Rating design: (Rating bar)
Add (allow user to add their name and ratings)
Update (Allow user to update to make changes)
Listview (Display the name and the ratings)
activity_main.xml
<LinearLayout xmlns:android="http://ift.tt/nIICcg"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="left">
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_margin="10dp">
<ImageView
android:layout_width="150dp"
android:layout_height="50dp"
android:scaleType="fitXY"
android:src="@drawable/logo_jpg" />
</TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_margin="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000000"
android:text="Name:"
android:textStyle="bold"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/editText"
android:ems="10"/>
</TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_margin="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000000"
android:text="Rate Design:"
android:textStyle="bold"/>
<RatingBar
android:id="@+id/ratingBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:numStars="5"
android:stepSize="1.0"
android:rating="0.0" />
</TableRow>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="@id/ratingBar"
android:onClick="AddContact"
android:text="Add"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button2"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="@id/button"
android:onClick="UpdateContact"
android:text="Update"/>
<LinearLayout>
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/listView"
android:layout_alignParentLeft="true"
android:layout_below="@id/button2">
</ListView>
</LinearLayout>
</TableLayout>
</LinearLayout>
MainActivity.java:
public class MainActivity extends AppCompatActivity {
private RatingBar ratingBar;
private DatabaseHandler db;
ListView lvInfo;
EditText txtName;
ArrayList<String> ArrayofName = new ArrayList<String>();
List<Result> results;
static ArrayAdapter<String> dbAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lvInfo = (ListView) findViewById(R.id.listView);
txtName = (EditText) findViewById(R.id.editText);
ratingBar = (RatingBar) findViewById(R.id.ratingBar);
db = new DatabaseHandler(this);
dbAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, android.R.id.text1, ArrayofName);
lvInfo.setAdapter(dbAdapter);
lvInfo.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
txtName.setText(results.get(position).getName());
ratingBar.setRating(results.get(position).getRating());
}
});
DisplayAll();
}
public void DisplayAll() {
results = db.getAllResults();
ArrayofName.clear();
for (Result rs : results) {
ArrayofName.add(rs.getId() + ".\t" + rs.getName() + ".\t" + rs.getRating());
}
dbAdapter.notifyDataSetChanged();
txtName.setText("");
ratingBar.setRating(5);
}
public void AddResult(View view) {
if ((txtName.getText().length() == 0) || (ratingBar.getRating() == 0) )
Toast.makeText(this, "Please enter the name and rating to be added!", Toast.LENGTH_LONG).show();
else {
Result tmp = new Result(txtName.getText().toString(), (ratingBar.getRating()) );
db.addResult(tmp);
DisplayAll();
Toast.makeText(this, "Results Added!", Toast.LENGTH_LONG).show();
}
}
public void UpdateResult(View view) {
if ((txtName.getText().length() == 0) || (ratingBar.getRating() == 0) )
Toast.makeText(this, "Please select result to be updated!", Toast.LENGTH_LONG).show();
else {
Result tmp = new Result(txtName.getText().toString(),(ratingBar.getRating()) );
db.updateResult(tmp);
DisplayAll();
Toast.makeText(this, "Result Updated!", Toast.LENGTH_LONG).show();
}
}
}
DatabaseHandler.java
public class DatabaseHandler extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = Environment.getExternalStorageDirectory().toString() + "/result.db";
private static final String TABLE_RESULT = "Result";
private static final String KEY_ID = "id";
private static final String KEY_NAME = "name";
private static final String KEY_RATING = "rating";
public DatabaseHandler(Context context){
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
//creating Tables
@Override
public void onCreate(SQLiteDatabase db){
String CREATE_RESULTS_TABLE = "CREATE TABLE " + TABLE_RESULT + "(" + KEY_ID + " INTEGER PRIMARY KEY," + KEY_NAME +
" TEXT,"+ KEY_RATING + " TEXT)";
db.execSQL(CREATE_RESULTS_TABLE);
}
//upgrading database
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion){
//drop older table if existed
db.execSQL("DROP TABLE IF EXISTS " + TABLE_RESULT);
//create tables again
onCreate(db);
}
public Result getResult(int id) {
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.query(TABLE_RESULT, new String[] { KEY_ID,
KEY_NAME, KEY_RATING}, KEY_ID + "=?",
new String[] { String.valueOf(id) }, null, null, null, null);
if (cursor != null)
cursor.moveToFirst();
Result result;
result = new Result(Integer.parseInt(cursor.getString(0)),
cursor.getString(1), cursor.getFloat(2));
return result;
}
public List<Result> getAllResults() {
List<Result> resultList = new ArrayList<Result>();
String selectQuery = "SELECT * FROM " + TABLE_RESULT;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
//looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
Result result = new Result();
result.setId(Integer.parseInt(cursor.getString(0)));
result.setName(cursor.getString(1));
result.setRating(cursor.getFloat(2));
resultList.add(result);
} while (cursor.moveToNext());
}
return resultList;
}
public int getResultsCount(){
String countQuery = "SELECT * FROM " + TABLE_RESULT;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(countQuery, null);
cursor.close();
return cursor.getCount();
}
public void addResult(Result result){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_NAME, result.getName());
values.put(KEY_RATING, result.getRating());
db.insert(TABLE_RESULT, null, values);
db.close();
}
public int updateResult(Result result){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_RATING ,result.getRating());
//updating row
return db.update(TABLE_RESULT, values, KEY_NAME + " = ?", new String[]{ result.getName()});
}
}
Result.java
public class Result {
private int id;
private String name;
private Float rating;
public Result(){}
public Result(String name, Float rating){
this.name = name;
this.rating = rating;
}
public Result(int id, String name, Float rating){
this.id = id;
this.name = name;
this.rating = rating;
}
public int getId(){
return id;
}
public void setId(int id)
{
this.id = id;
}
public String getName(){
return name;
}
public void setName(String name){
this.name = name;
}
public Float getRating(){
return rating;
}
public void setRating(Float rating){
this.rating = rating;
}
}
Aucun commentaire:
Enregistrer un commentaire