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

时间:2014-10-03 12:25:36

标签: android sqlite listview

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

但是在他的应用程序中,这个人经历了一个中间步骤来列出sql记录。

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

MyActivity

按钮“添加播放器”,一旦点击,将带来DisplayMessageActivity,我可以在其中保存数据库中的名称

DisplayMessageActivity

名称的EditText,三个按钮(删除,更新,关闭) (点击删除时,我被带回MyActivity) (点击更新时,我会继续使用DisplayMessageActivity) (当点击关闭时,我被带回MyActivity))

我设法构建它,在两个作品之间导航,数据库插入效果很好。

保存名称时,我将返回第一个活动,但显示的列表未使用新添加的名称进行更新。

这是我的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); } }

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

请接受我对我的误解的道歉。

非常感谢!

蝙蝠

3 个答案:

答案 0 :(得分:1)

在添加OnResume()方法时找到了解决方案。这是我最后的活动课程:

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

答案 1 :(得分:0)

包含LinearLayout的{​​{1}}的方向应为垂直

更改

ListView

 android:orientation="horizontal"

答案 2 :(得分:0)

只需在MainActivity中添加以下方法,当您输入save时,您将以垂直方式查看数据。

@Override
public void onResume(){
    super.onResume();
    StudentRepo repo = new StudentRepo(this);
    ArrayList<HashMap<String, String>> studentsList = repo.getStudentList();

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

        ListView lv = getListView();

        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                student_Id = (TextView) view.findViewById(R.id.student_Id);
                String valAnimalId = student_Id.getText().toString();
                Intent  objIndent = new Intent(getApplicationContext(),StudentDetail.class);
                objIndent.putExtra("animalId", valAnimalId);
                startActivity(objIndent);
            }
        });

        ListAdapter adapter = new SimpleAdapter( MainActivity.this,studentsList, R.layout.view_student_entry, new String[] { "id","name"}, new int[] {R.id.student_Id, R.id.student_name});
        setListAdapter(adapter);

    }
}