gpt4 book ai didi

ANDROID STUDIO > Sqlite、Listview 和 Onclick 基本示例

转载 作者:行者123 更新时间:2023-11-29 01:38:45 25 4
gpt4 key购买 nike

在阅读了一些教程之后,我正在尝试构建我的第一个应用程序。我正在使用的是这个:http://instinctcoder.com/android-studio-sqlite-database-example/

但是在他的应用中,这个人通过一个中间步骤来列出 sql 记录。

我希望我的应用执行以下操作:

我的 Activity

“添加播放器”按钮,单击后会显示 DisplayMessageActivity,我可以在其中将名称保存在数据库中

显示消息 Activity

名称的 EditText,三个按钮(删除、更新、关闭)(当点击删除时,我被带回 MyActivity)(点击更新时,我停留在 DisplayMessageActivity 上)(当点击关闭时,我被带回 MyActivity))

我设法构建了它,两者之间的导航都有效,并且数据库插入效果很好。

保存名称时,我返回到第一个 Activity ,但出现的列表未使用新添加的名称进行更新。

这是我的 activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/swipe_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
tools:context="tab.sqltesting.com.myapplication.MyActivity">

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add Player"
android:id="@+id/addPlayer"
android:onClick="addPlayer" />

<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@android:id/list"
android:layout_centerHorizontal="true"
android:layout_alignParentTop="true"
android:layout_above="@+id/btnAdd" />

</LinearLayout>

这是我的 activity_main.java



public class MyActivity extends ListActivity implements android.view.View.OnClickListener{

Button addPlayer;
TextView players_Id;

@Override
protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);


PlayersRepo repo = new PlayersRepo(this);
ArrayList<HashMap<String, String>> playersList = repo.getPlayersList();

if (playersList.size() != 0) {

ListView lv = getListView();

lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
players_Id = (TextView) view.findViewById(R.id.players_Id);
String playersId = players_Id.getText().toString();
Intent objIndent = new Intent(getApplicationContext(), DisplayMessageActivity.class);
objIndent.putExtra("players_Id", Integer.parseInt(playersId));
startActivity(objIndent);
}

});

ListAdapter adapter = new SimpleAdapter(MyActivity.this, playersList, R.layout.list_players_view, new String[]{"id", "name"}, new int[]{R.id.players_Id, R.id.players_name});
setListAdapter(adapter);

} else {

Toast.makeText(this, "No Player!", Toast.LENGTH_SHORT).show();

}

addPlayer = (Button) findViewById(R.id.addPlayer);
addPlayer.setOnClickListener(this);

}


@Override
public void onClick(View view) {

if (view == findViewById(R.id.addPlayer)) {

Intent intent = new Intent(this, DisplayMessageActivity.class);
startActivity(intent);
}

}

我想请求你的帮助,帮助我理解我做错了什么。

请接受我对我的误解表示歉意。

非常感谢!

bat

最佳答案

在添加 OnResume() 方法中找到解决方案。这是我最后的 Activity 课:

package tab.sqltesting.com.myapplication;

public class MyActivity extends ListActivity implements android.view.View.OnClickListener{

Button addPlayer;
TextView players_Id;

@Override
protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);

addPlayer = (Button) findViewById(R.id.addPlayer);
addPlayer.setOnClickListener(this);

}

@Override
public void onClick(View view) {

if (view == findViewById(R.id.addPlayer)) {

Intent intent = new Intent(this, DisplayMessageActivity.class);
startActivity(intent);
}
}

@Override
public void onResume() {

super.onResume();
PlayersRepo repo = new PlayersRepo(this);
ArrayList<HashMap<String, String>> playersList = repo.getPlayersList();

if (playersList.size() != 0) {

ListView lv = getListView();

lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
players_Id = (TextView) view.findViewById(R.id.players_Id);
String playersId = players_Id.getText().toString();
Intent objIndent = new Intent(getApplicationContext(), DisplayMessageActivity.class);
objIndent.putExtra("players_Id", Integer.parseInt(playersId));
startActivity(objIndent);
}

});

ListAdapter adapter = new SimpleAdapter(MyActivity.this, playersList, R.layout.list_players_view, new String[]{"id", "name"}, new int[]{R.id.players_Id, R.id.players_name});
setListAdapter(adapter);

} else {

Toast.makeText(this, "No Player!", Toast.LENGTH_SHORT).show();

}
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.my, 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();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}

关于ANDROID STUDIO > Sqlite、Listview 和 Onclick 基本示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26178897/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com