ListView未显示在屏幕上

时间:2016-09-14 16:19:26

标签: android

我已经编写了用户输入“名称”和“密码”的代码,然后在下一个活动页面中将详细信息显示在列表视图中。 listview将通过自定义Cursor类填充,数据将从数据库中获取。

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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.hsports.sqlopenhelperclass.MainActivity"
    android:orientation="vertical"
    >



    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="name"
        android:id="@+id/name"
        />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="password"
        android:id="@+id/password"
        />

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Submit"
        android:id="@+id/submit"
        android:onClick="storeInDb"
        />


</LinearLayout>

MainActivity.java

    package com.example.hsports.sqlopenhelperclass;

import android.content.ContentValues;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import com.example.hsports.sqlopenhelperclass.DbHelper;

public class MainActivity extends AppCompatActivity {

    String name;
    String password;
    Button submit;



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



        submit = (Button) findViewById(R.id.submit);




    }


    public void storeInDb(View v)
    {
        name = ((EditText) findViewById(R.id.name)).getText().toString();
        password = ((EditText) findViewById(R.id.password)).getText().toString();


        DbHelper op=new DbHelper(getApplicationContext());
op.insertValue(name,password);


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


    }











}

activity_list_the_details_of_user.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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"        tools:context="com.example.hsports.sqlopenhelperclass.ListTheDetailsOfUser">

    <ListView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/listview"
        >

    </ListView>

</LinearLayout>

ListTheDetailsOfUser.java

package com.example.hsports.sqlopenhelperclass;

import android.database.Cursor;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ListView;
import com.example.hsports.sqlopenhelperclass.DbHelper;


public class ListTheDetailsOfUser extends AppCompatActivity {

    ListView lv;
    DbHelper db;

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

        lv=(ListView)findViewById(R.id.listview);
        DbHelper obj=new DbHelper(getApplicationContext());
        Cursor p=obj.getAllValues();

        CustomCursor obj1=new CustomCursor(getApplicationContext(),p,0);
        lv.setAdapter(obj1);
    }
}

CustomCursor.java

    package com.example.hsports.sqlopenhelperclass;

import android.content.Context;
import android.database.Cursor;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CursorAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;

import com.example.hsports.sqlopenhelperclass.DbHelper;
/**
 * Created by I324671 on 9/14/2016.
 */
public class CustomCursor extends CursorAdapter {

    LayoutInflater inflater;
    final DbHelper dbHelper;
    public CustomCursor(Context context, Cursor c, int flags) {
        super(context, c, flags);
        inflater= LayoutInflater.from(context);
         dbHelper=new DbHelper(context);

    }
    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {
        return inflater.inflate(R.layout.menu, parent, false);

    }

    @Override
    public void bindView(View view, final Context context, final Cursor cursor) {


        TextView gh=(TextView)view.findViewById(R.id.name);
       final ListView fg=(ListView)view.findViewById(R.id.listview);


        final String l=cursor.getString(cursor.getColumnIndex("name"));
        gh.setText(l);


        Button b=(Button)view.findViewById(R.id.remove);



        b.setOnClickListener(new View.OnClickListener(){


            @Override
            public void onClick(View v) {
                dbHelper.deleteValue(l);
                CustomCursor obj1=new CustomCursor(context,cursor,0);
                fg.setAdapter(obj1);





            }
        });




    }
}

我的DBhelper.java类如下

package com.example.hsports.sqlopenhelperclass;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.support.v4.content.ContextCompat;

/**
 * Created by I324671 on 9/14/2016.
 */
public class DbHelper extends SQLiteOpenHelper {

    DbHelper(Context context)
    {
        super(context,"NamePassword",null,1);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {

        db.execSQL("Create table if not exists NP ( _id INTEGER PRIMARY KEY AUTOINCREMENT , name varchar(20) , password varchar(20) );");

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }

    public boolean insertValue(String name,String password)
    {
        SQLiteDatabase db=this.getWritableDatabase();
        ContentValues cv=new ContentValues();
        cv.put("name",name);
        cv.put("password",password);
        db.insert("NP",null,cv);
        return true;
    }

    public int deleteValue(String name)
    {
        SQLiteDatabase db=this.getWritableDatabase();
        return db.delete("NP","name = ? ", new String[] {name});

    }

    public Cursor getAllValues()
    {
        SQLiteDatabase db=this.getReadableDatabase();
        Cursor df=db.rawQuery("Select * from NP",null);
        return df;
    }
}

我的menu.xml是:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

<TextView
    android:layout_width="65dp"
    android:layout_height="wrap_content"
    android:id="@+id/name"
    />

    <TextView
        android:layout_width="65dp"
        android:layout_height="wrap_content"
        android:id="@+id/password"
        />

    <Button
        android:layout_width="35dp"
        android:layout_height="wrap_content"
        android:id="@+id/remove"
        android:text="Remove"
        />
</LinearLayout>

现在我要填充列表,并且删除按钮即将到来。按下删除按钮后,该项目将被正确删除。

现在我想要的是,当我从列表中删除一个项目时,列表应该刷新然后在那里,不应该从列表中删除该项目,不应该在那里显示它。 目前我必须按回按钮,然后前进才能看到结果,但它应该发生,就像当我按下“删除”按钮时,它应该自动刷新那里的列表,然后删除特定的项目。

0 个答案:

没有答案