[英]How can I delete and edit a specific (sqlite db) row from an attached Linear Layout (Android)
[英]How can I get all columns for a specific row from the database to another layout?
我正在開發一個android項目,我想在其中獲取一行的所有列(特定條件)並將其拆分為TextViews。
我有一個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布局
除此之外,我還有一個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----------------------------------------------------------------------------------------------------------------//
}
希望你們能幫助我弄清楚
您應該在適配器中保留數據列表(如果是電影,則為電影)。 然后將對象的ID而不是標題傳遞給另一個活動,以便能夠通過ID從數據庫查詢該對象。
您可以將整個對象傳遞給一個Activity
並在那里檢索它。
通話活動:
final Movie details = listView.getAdapter().getItem(position);
Intent i = new Intent(getApplicationContext(), MovieDetailsActivity.class);
i.putExtra("key", details);
稱為Activity(這里是MovieDetailsActivity
):
Intent intent = getIntent();
Movie details = (Movie)(intent.getExtras().getSerializable("key"));
注意,您必須使類Movie
Serializable。
public class Movie implements Serializable
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.