我正在开发一个Android项目,我希望获取一行的所有列(特定条件)并将它们拆分为TextViews。
我有一个包含项目的ListView:
当我点击listview中的一个项目时,它将导航到另一个布局并将listview项目作为字符串传递:
public void Select()
{
final ListView listView = (ListView) findViewById(R.id.ListViewMovie);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
final String details = listView.getAdapter().getItem(position).toString();
Intent i = new Intent(getApplicationContext(), MovieDetailsActivity.class);
i.putExtra("key", details);
startActivity(i);
}
});
}
这是导航到的其他布局: MovieDetails Layout
除此之外我还有MovieRepository
:
public class MoviesRepository
{
private SQLiteDatabase db;
private MyDBHelper myDBHelper;
Movie movie;
private String [] MovieAllColumns ={Movie.COlUMN_ID,
Movie.COlUMN_NAME};
private String [] MovieColumns ={Movie.COlUMN_ID,
Movie.COlUMN_NAME,
Movie.COlUMN_GENRE,
Movie.COlUMN_YEAR};
public MoviesRepository(Context context)
{
myDBHelper = new MyDBHelper(context);
}
public void open() throws SQLException
{
//Open connection to write data
db = myDBHelper.getWritableDatabase();
}
public void close()
{
//Close connection to database
db.close();
}
private Movie cursorToMovie (Cursor cursor)
{
Movie movie = new Movie();
movie.setId(cursor.getInt(0));
movie.setName(cursor.getString(1));
return movie;
}
public List<Movie> getAllMovies()
{
open();
List<Movie> movieList = new ArrayList<>();
Cursor cursor = db.query(Movie.TABLE_NAME, MovieAllColumns, null, null, null, null, null);
cursor.moveToFirst();
while (!cursor.isAfterLast())
{
Movie movie = cursorToMovie(cursor);
movieList.add(movie);
cursor.moveToNext();
}
cursor.close();
close();
return movieList;
}
public void Create(Movie movie)
{
open();
//helps you insert values to the table
ContentValues values = new ContentValues();
//Put method - first column; what column do you want to be storing this ind. Second; what is the value you want to put ind
values.put(Movie.COlUMN_NAME, movie.getName());
values.put(Movie.COlUMN_GENRE, movie.getGenre());
values.put(Movie.COlUMN_YEAR, movie.getYear());
db.insert(Movie.TABLE_NAME, null, values);
close();
}
public void Delete(Movie movie)
{
open();
//Deletes Movie by id
db.delete(Movie.TABLE_NAME, Movie.COlUMN_ID + " = " + movie.getId(), null);
db.close();
}
}
DBHelper
电影模特:
public class Movie
{
// property to help us handle the data
private int id;
private String name;
private String genre;
private int year;
//Getters and Setters of the properties
public long 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 String getGenre()
{
return genre;
}
public void setGenre(String genre)
{
this.genre = genre;
}
public int getYear()
{
return year;
}
public void setYear(int year)
{
this.year = year;
}
public String toString()
{
return name;
}
//----------Start--------- Here we have defined the table contents (basically a blueprint of the table Movie) -------------------------------------
public static final String TABLE_NAME = "movie";
public static final String COlUMN_ID = "id";
public static final String COlUMN_NAME = "make";
public static final String COlUMN_GENRE = "model";
public static final String COlUMN_YEAR = "year";
//----------------------END----------------------------------------------------------------------------------------------------------------//
}
希望你们能帮助我弄清楚
答案 0 :(得分:0)
您应该在适配器中保留数据列表(在您的案例中为电影)。然后传递不是标题,而是将对象的id传递给另一个活动,以便能够通过id从数据库中查询该对象。
答案 1 :(得分:0)
您可以将整个对象传递给Activity
并在那里检索它。
致电活动:
final Movie details = listView.getAdapter().getItem(position);
Intent i = new Intent(getApplicationContext(), MovieDetailsActivity.class);
i.putExtra("key", details);
被叫活动(此处为MovieDetailsActivity
):
Intent intent = getIntent();
Movie details = (Movie)(intent.getExtras().getSerializable("key"));
请注意,您必须使您的班级Movie
可序列化。
public class Movie implements Serializable