用于添加,删除和查看数据的应用程序

时间:2016-10-26 09:29:01

标签: java android sqlite android-sqlite

我创建了一个应用程序,它在第一个Activity(home)中有一个Button,进入下一个Activity 在下一个Activity中我添加了一些添加,删除和列出所有按钮,但它不起作用。

请帮我解决这个问题

删除数据时,我应该有一个复选框来选择另一个活动中的数据。

我正在使用Android Studio 2.1.3

<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" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="List All"
    android:id="@+id/btnGetAll"
    android:layout_marginBottom="94dp"
    android:layout_alignParentBottom="true"
    android:layout_alignParentStart="true" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Add"
    android:id="@+id/btnAdd"
    android:layout_alignTop="@+id/btnGetAll"
    android:layout_centerHorizontal="true" />


<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Delete"
    android:id="@+id/btnDel"
    android:layout_below="@android:id/list"
    android:layout_alignParentEnd="true" />

公共类List_View扩展ListActivity实现android.view.View.OnClickListener {

Button btnAdd,btnGetAll,btnDel;

TextView student_Id;


@Override
public void onClick(View view)
{
    if (view== findViewById(R.id.btnAdd))
    {

        Intent intent = new Intent(this,StudentDetails.class);
        intent.putExtra("student_Id",0);
        startActivity(intent);

    }
    else {

        StudentRepo repo = new StudentRepo(this);

        ArrayList<HashMap<String, String>> studentList = repo.getStudentList();
        if (studentList.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 studentId = student_Id.getText().toString();

                    Intent objIndent = new Intent(getApplicationContext(), StudentDetails.class);
                    objIndent.putExtra("student_Id", Integer.parseInt(studentId));
                    startActivity(objIndent);
                }
            });
           /* ListAdapter adapter = new SimpleAdapter( MainActivity.this,studentList, R.layout.activity_view_student_entry, new String[] { "id","name"}, new int[] {R.id.student_Id, R.id.student_name});
            setListAdapter(adapter);*/

            ListAdapter adapter = new SimpleAdapter(List_View.this, studentList, R.layout.activity_view__student__entry, new String[]{"id", "name", "email", "age", "place", "phnumber"}, new int[]{R.id.student_Id, R.id.student_name, R.id.student_email, R.id.student_age, R.id.student_place, R.id.student_phnumber});
            setListAdapter(adapter);
        } else {
                Toast.makeText(this, "No student!", Toast.LENGTH_SHORT).show();
            }
    }

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

        Intent i = new Intent(this,Delete.class);
        startActivity(i);

    }

}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_list__view);


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

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

    btnDel = (Button) findViewById(R.id.btnDel);
    btnDel.setOnClickListener(this);
}
}

1 个答案:

答案 0 :(得分:0)

试试这个。

android:descendantFocusability="blocksDescendants"

OR

在onItemClick上试试这个。

String item = ((TextView)view).getText().toString();