2016-11-08 42 views
-1

我設計了簡單的提交和獲取詳細信息表單。數據存儲在數據庫中,並獲取數據庫中的數據並顯示在列表視圖中。爲什麼不顯示列表視圖數據點擊詳細按鈕

但所有的東西都正常工作,但是當我點擊詳細的按鈕時,他沒有顯示數據庫的細節。

請幫我...

DBAdapter.java

package com.example.anew.demo; 

import android.content.ContentValues; 
import android.content.Context; 
import android.database.Cursor; 
import android.database.SQLException; 
import android.database.sqlite.SQLiteDatabase; 
import android.database.sqlite.SQLiteOpenHelper; 


public class DBAdapter { 
//COLUMNS 
static final String NAME = "name"; 
static final String E_Mail = "email"; 
static final String PHONE = "phone"; 

//DB PROPERTIES 
static final String DBNAME = "employee_DB"; 
static final String TBNAME = "employee_TB"; 
static final int DBVERSION = '1'; 

static final String CREATE_TB = ("CREATE TABLE IF NOT EXISTS employee_TB(name VARCHAR, phone VARCHAR, email VARCHAR);"); 
final Context context; 
SQLiteDatabase sqLiteDatabase; 
DBHelper helper; 

public DBAdapter(Context ctx) { 
    this.context = ctx; 
    helper = new DBHelper(context); 
} 

//INNER HELPER DB CLASS 
private static class DBHelper extends SQLiteOpenHelper { 
    public DBHelper(Context context) { 
     super(context, DBNAME, null, DBVERSION); 
    } 

    @Override 
    public void onCreate(SQLiteDatabase db) { 
      db.execSQL(CREATE_TB); 
    } 

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

    } 
} 
    // OPEN THE DB 
    public DBAdapter openDB() { 
     try { 
      sqLiteDatabase = helper.getWritableDatabase(); 
     } catch (SQLException e) { 
      e.printStackTrace(); 
     } 
     return this; 
    } 
//CLOSE THE DB 
public void close() 
{ 
    helper.close(); 
} 

//INSERT INTO TABLE 
public long add(String name,String email, String phone){ 
    try{ 
     ContentValues contentValues = new ContentValues(); 
     contentValues.put(NAME,name); 
     contentValues.put(E_Mail,email); 
     contentValues.put(PHONE,phone); 
     return sqLiteDatabase.insert(TBNAME,null,contentValues); 
    }catch (SQLException e){ 
     e.printStackTrace(); 
    } 
    return 0; 
} 

//GET ALL VALUES 
public Cursor getAllNames(){ 
    String[] columns={NAME,E_Mail,PHONE}; 
    String query="select * from "+TBNAME; 
    return sqLiteDatabase.rawQuery(query,null); 
} 

}

MainActivity.java

package com.example.anew.demo; 

import android.database.Cursor; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.AdapterView; 
import android.widget.ArrayAdapter; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.ListView; 
import android.widget.Toast; 

import java.util.ArrayList; 

public class MainActivity extends AppCompatActivity { 

ListView lv; 
EditText nametext,emailtext,phonetext; 
Button submitbtn,detailbtn; 
ArrayList<String> details = new ArrayList<String>(); 
ArrayAdapter<String> adapter; 

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

    nametext = (EditText)findViewById(R.id.nametext); 
    emailtext = (EditText)findViewById(R.id.emailtext); 
    phonetext = (EditText)findViewById(R.id.phonetext); 
    submitbtn = (Button)findViewById(R.id.submitbtn); 
    detailbtn = (Button)findViewById(R.id.detailbtn); 
    lv = (ListView)findViewById(R.id.listview1); 

    adapter = new ArrayAdapter<String>(this,android.R.layout.simple_selectable_list_item); 

    final DBAdapter db = new DBAdapter(this); 

    //EVEMTS 
    submitbtn.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      //OPEN 
      db.openDB(); 

      //INSERT 
      long result=db.add(nametext.getText().toString(),emailtext.getText().toString(),phonetext.getText().toString()); 
      if(result>0){ 
       nametext.setText(""); 
       emailtext.setText(""); 
       phonetext.setText(""); 
      }else{ 
       Toast.makeText(MainActivity.this, "Failure", Toast.LENGTH_SHORT).show(); 
      } 
      db.close();; 
     } 
    }); 
    //Retrieve 
    detailbtn.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      details.clear(); 
      //OPEN 
      db.openDB(); 

      //RETRIEVE 
      Cursor cursor = db.getAllNames(); 
      while (cursor.moveToNext()) { 
       String name = cursor.getString(0); 
       details.add(name); 
      } 
      lv.setAdapter(adapter); 
      db.close(); 
     } 
    }); 

} 

}

Activity_main

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 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.anew.demo.MainActivity"> 


<LinearLayout 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_alignParentTop="true" 
    android:layout_centerHorizontal="true"> 


    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textAppearance="?android:attr/textAppearanceLarge" 
     android:text="Details" 
     android:id="@+id/textView" 
     android:layout_gravity="center_horizontal" 
     android:textSize="35sp" /> 

    <LinearLayout 
     android:orientation="horizontal" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginLeft="10dp" 
     android:layout_marginRight="10dp" 
     android:layout_marginTop="40dp"> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Name" 
      android:id="@+id/nameView" 
      android:textSize="30sp" /> 

     <EditText 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:inputType="textPersonName" 
      android:ems="10" 
      android:id="@+id/nametext" 
      android:layout_weight="1" 
      android:layout_marginLeft="50dp"/> 
    </LinearLayout> 

    <LinearLayout 
     android:orientation="horizontal" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginLeft="10dp" 
     android:layout_marginRight="10dp" 
     android:layout_marginTop="20dp"> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="E-Mail" 
      android:id="@+id/emailView" 
      android:textSize="30sp" /> 

     <EditText 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:inputType="textEmailAddress" 
      android:ems="10" 
      android:id="@+id/emailtext" 
      android:layout_weight="1" 
      android:layout_marginLeft="45dp"/> 
    </LinearLayout> 

    <LinearLayout 
     android:orientation="horizontal" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginLeft="10dp" 
     android:layout_marginRight="10dp" 
     android:layout_marginTop="20dp"> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Phone" 
      android:id="@+id/phoneView" 
      android:textSize="30sp" /> 

     <EditText 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:inputType="phone" 
      android:ems="10" 
      android:id="@+id/phonetext" 
      android:layout_weight="1" 
      android:layout_marginLeft="45dp"/> 
    </LinearLayout> 

    <LinearLayout 
     android:id="@+id/linearlayput1" 
     android:orientation="horizontal" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginLeft="10dp" 
     android:layout_marginRight="10dp" 
     android:layout_marginTop="10dp"> 

     <Button 
      android:layout_width="match_parent" 
      android:layout_height="20dp" 
      android:text="Submit" 
      android:id="@+id/submitbtn" 
      android:layout_weight="1"/> 

     <Button 
      android:layout_width="match_parent" 
      android:layout_height="20dp" 
      android:text="Details" 
      android:id="@+id/detailbtn" 
      android:layout_weight="1"/> 

    </LinearLayout> 

    <LinearLayout 
     android:orientation="horizontal" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/linearlayput1" 
     android:layout_marginLeft="10dp"> 

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

+1

你能添加一些更多的信息,即你​​已經嘗試了什麼,有什麼做什麼,等等? – DForck42

+0

當你說它「沒有顯示」細節時,你是什麼意思?你會得到一個空白的對話框嗎?程序崩潰了嗎?你是否收到特定的錯誤信息? –

回答

0

嘗試在DB類這種方法,

public Cursor getAllNames(){ 

    sqLiteDatabase = helper.getReadableDatabase(); 
    return sqLiteDatabase.query(TBNAME,new String[] {NAME,E_Mail,PHONE},null,null,null,null,null); 
} 
+0

ohh yes ...它的工作謝謝你這麼多 –

+0

ohh yes ... srry for actlly我是新用戶..我不知道 –

相關問題