我是Android编程新手。我一直坚持这个问题,我想找到解决方案,通过其他问题和代码示例,似乎没有什么似乎与我创建这个应用程序的方法一致,作为一个初学者我不得不承认我完全失去了。我的词汇中没有很多技巧,所以我很难找到我想要的东西,希望有人可以帮我指出正确的方向。我正在尝试更新teams
表格中的一个单独行,其中包含id
,team_name
和image
,其中包含位于用户设备上的图片的文件路径。
id | team_name | image
-------------------------
| |
以下是我用来更新行的方法
public int updateTeam(Team team) {
//get reference to writable DB
SQLiteDatabase db = this.getWritableDatabase();
//create ContentValues to add key "column"/value
ContentValues values = new ContentValues();
values.put("team_name", team.getTeamName()); // get team_name
values.put("path", team.getPath()); // get image
//updating row
int i = db.update(TEAMS_TABLE, //table
values, // column/value
ID + " = ?", // selections
new String[] {
String.valueOf(team.getId())
}); //selection args
//close
db.close();
//return
return i;
}
public class Team {
String team_name, path, league_name;
int id, league_id, points;
public Team(int keyId, String team_name, String path,
int points, String league_name) {
this.id = keyId;
this.team_name = team_name;
this.path = path;
this.points = points;
this.league_name = league_name;
}
public Team(int keyId, String team_name, String path) {
this.id = keyId;
this.team_name = team_name;
this.path = path;
}
public Team() {}
public Team(int keyId) {
this.id = keyId;
}
public int getId() {
return id;
}
public void setId(int keyId) {
this.id = keyId;
}
public int getLeagueId() {
return league_id;
}
public String getTeamName() {
return team_name;
}
public String getLeagueName() {
return league_name;
}
public void setLeagueName(String league_name) {
this.league_name = league_name;
}
public int getPoints() {
return points;
}
public void setPoints(int points) {
this.points = points;
}
public void setTeamName(String team_name) {
this.team_name = team_name;
}
public void setLeague_id(int league_id) {
this.league_id = league_id;
}
public void setPath(String path) {
this.path = path;
}
public String getPath() {
return path;
}
}
public class TeamImageAdapter extends ArrayAdapter < Team > {
Context context;
int layoutResourceId;
ArrayList < Team > data = new ArrayList < Team > ();
public TeamImageAdapter(Context context, int layoutResourceId, ArrayList < Team > data) {
super(context, layoutResourceId, data);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.data = data;
}
private final int THUMBSIZE = 200;
/**
* applying ViewHolder pattern to speed up ListView, smoother and faster
* item loading by caching view in A ViewHolder object
*/
private static class ViewHolder {
ImageView imgIcon;
TextView teamId, teamName, leagueName, points;
}
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
ViewHolder holder = null;
if (row == null) {
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new ViewHolder();
holder.teamId = (TextView) row.findViewById(R.id.txtTeamId);
holder.teamName = (TextView) row.findViewById(R.id.txtTeamName);
holder.leagueName = (TextView) row.findViewById(R.id.txtLeagueName);
holder.points = (TextView) row.findViewById(R.id.txtPoints);
holder.imgIcon = (ImageView) row.findViewById(R.id.item_img_icon);
row.setTag(holder);
} else {
holder = (ViewHolder) row.getTag();
}
Team team = data.get(position);
holder.teamId.setText(String.valueOf(team.id));
holder.teamName.setText(team.team_name);
holder.leagueName.setText(team.league_name);
holder.points.setText("Points: " + String.valueOf(team.points));
//convert string to bitmap and set to thumbsize
holder.imgIcon.setImageBitmap(ThumbnailUtils
.extractThumbnail(BitmapFactory.decodeFile(team.getPath()),
THUMBSIZE, THUMBSIZE));
return row;
}
}
public class DisplayTeam extends AppCompatActivity {
ArrayList < Team > imageArry = new ArrayList < Team > ();
TeamImageAdapter adapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.display_team);
DatabaseHelper myDb = new DatabaseHelper(this);
// Reading all teams from database
List < Team > team = myDb.getAllTeams();
for (Team tm: team) {
String log = "ID:" + tm.getId() + " Team Name: " + tm.getTeamName() + " ,Image: " + tm.getPath() + " ,Points: " + tm.getPoints() + " ,League Name: " + tm.getLeagueName();
// Writing teams to log
Log.d("Result: ", log);
//add teams data in arrayList
imageArry.add(tm);
}
adapter = new TeamImageAdapter(this, R.layout.teams_list,
imageArry);
final ListView dataList = (ListView) findViewById(R.id.main_list_view);
dataList.setAdapter(adapter);
dataList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView < ? > parent, View view, int position, long id) {
TextView tid = (TextView) view.findViewById(R.id.txtTeamId);
Long teamId = Long.valueOf(tid.getText().toString());
}
});
}
}
我想要它,以便当我点击ListView时会出现一个弹出窗口,其中包含一个显示当前团队team_name
的EditText。我只是不确定它的名称或我如何执行此操作。有点像
以及更新代码可能会是什么样子。我并不是真的期待一个完整的代码答案,而只是一个正确的方向,一步一步的教程将是非常有帮助的。非常感谢任何有用的人。
答案 0 :(得分:0)
因为当您将适配器设置为列表时,它会从数据库中获取记录。但是在更新行之后,您不会改变适配器值。
所以在List < Team > team;
中声明Activity
为全局变量,并在onCreate
方法中对其进行初始化。
将新值设置为适配器并通知OnItemClickListener
方法
dataList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView < ? > parent, View view, int position, long id) {
Team teamToUpdate = team.get(position);
int status = updateTeam(teamToUpdate);
if(status > 0) {
// update success
// change the adapter values here
imageArry.clear();
team = myDb.getAllTeams();
for (Team tm: team) {
String log = "ID:" + tm.getId() + " Team Name: " + tm.getTeamName() +
" ,Image: " + tm.getPath() + " ,Points: " + tm.getPoints() +
" ,League Name: " + tm.getLeagueName();
// Writing teams to log
Log.d("Result: ", log);
//add teams data in arrayList
imageArry.add(tm);
}
adapter.notifyDataSetChanged();
} else {
// failed to update
// check the reason
}
}
});
希望它会有所帮助。
答案 1 :(得分:0)
/*public int getId() {
return id;
}
*/
db.update(TEAMS_TABLE, values,
ID + " = ?", new String[] {
Integer.toString(team.getId())
});