当前位置: 动力学知识库 > 问答 > 编程问答 >

android - How to get String value of an object instead of object values?

问题描述:

I want to populate spinner from sqlite database. After I am retrieving the relevant data it showed junk values. Could you please help me to fix this. I will attach my codes here.

--java File--

public class AddNewMovieActivity extends AppCompatActivity {

private Spinner movieTypes;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.add_new_movie);

movieTypes = (Spinner) findViewById(R.id.spinMovieType);

loadSpinnerData();

}

@Override

public boolean onCreateOptionsMenu(Menu menu) {

// Inflate the menu; this adds items to the action bar if it is present.

getMenuInflater().inflate(R.menu.menu_add_new_movie, menu);

return true;

}

@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);

}

private void loadSpinnerData() {

// database handler

DBHelper db = new DBHelper(this);

// Spinner Drop down elements

List<MovieType> lables = db.getAllMovieTypes();

// Creating adapter for spinner

ArrayAdapter<MovieType> dataAdapter = new ArrayAdapter<MovieType>(this,android.R.layout.simple_spinner_item,lables);

// Drop down layout style - list view with radio button

dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

// attaching data adapter to spinner

movieTypes.setAdapter(dataAdapter);

}

}

-- DBHelper--

 public List<MovieType> getAllMovieTypes() {

SQLiteDatabase db = getReadableDatabase();

List<MovieType> lst = new ArrayList<MovieType>();

Cursor cursor = db.query(TABLE_MOVIE_TYPE, null, COLUMN_STATUS + "=?", new String[]{"A"}, null, null, null);

if (cursor != null) {

cursor.moveToFirst();

while (!cursor.isAfterLast()){

String type = cursor.getString(1).toString();

MovieType movieType = new MovieType(type);

movieType.setTypeId(cursor.getInt(0));

lst.add(movieType);

cursor.moveToNext();

}

}

db.close();

return lst;

}

I just want to show the movie types from the database. Please help me. this code returns the object values. but I need the string value of it. I have tried to convert my object to string. but I couldn't find a proper way to fix it. Actually I am very new to Android. so please help me.

网友答案:

What you can do is to Override toString() method in your MovieType class

    public class MovieType{

    //Whatever fields you have here

     //Override toString()
     @Override
        public String toString() {
            return your_field_name;  

            /***put the field name which you want to show in spinner
                            Or 
                You can append multiple fileds
                return field1 + field2+......;
           ***/
        }
    }

And your all set, you would then see the value of the field in your spinner, what you have set in toString() method.

网友答案:

You can make custom adapter or you can make your list of String type. As you have mentioned that you just want to show movies type. You can do this in your getAllMovieTypes() function.

public List<String> getAllMovieTypes() {
    SQLiteDatabase db = getReadableDatabase();
    List<String> lst = new ArrayList<String>();
    Cursor cursor = db.query(TABLE_MOVIE_TYPE, null, COLUMN_STATUS + "=?", new String[]{"A"}, null, null, null);
    if (cursor != null) {
        cursor.moveToFirst();
        while (!cursor.isAfterLast()){
            String type = cursor.getString(1).toString();
            lst.add(type);
        cursor.moveToNext();
        }
    }
    db.close();
    return lst;

}

Now, you can display only type.

分享给朋友:
您可能感兴趣的文章:
随机阅读: