2016-05-04 13 views
-5

ich zwei XML-Dateien erstellt haben, und meine benutzerdefinierte Ansicht von 3 Zeilen, 2 Premieren sind Texte, und die letzte eine Schaltfläche Daten löschen:Kann nicht populatemy Listview von SQLite

XML der Aktivität:

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

listpoids.xml:

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

    <TextView 
     android:id="@+id/textViewDate" 
     android:layout_gravity="center" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="TextView" /> 

    <TextView 
     android:layout_gravity="center" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="New Text" 
     android:id="@+id/textViewPoids" /> 

    <Button 
     android:layout_gravity="center" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Supprimer" 
     android:id="@+id/buttonSupprimer" /> 

</LinearLayout> 

ich habe meine adaper:

public class PoidsAdapter extends ArrayAdapter<String> { 
    private Context mContext; 
    private List<String> mListPoids; 

    public PoidsAdapter(Context context, int resource, List<String> objects) { 
     super(context, resource, objects); 
     this.mContext = context; 
     this.mListPoids = objects; 
    } 


    @Override 
    public int getCount() { 
     return mListPoids.size(); 
    } 

    @Override 
    public String getItem(int position) { 
     return mListPoids.get(position); 
    } 

    @Override 
    public View getView(final int position, View view, final ViewGroup parent) { 
     final holder holder; 
      LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      view = inflater.inflate(R.layout.listpoids, null); 
      holder = new holder(); 
      holder.mTvTitle = (TextView) view.findViewById(R.id.textViewDate); 
      holder.mTvMediaName = (TextView) view.findViewById(R.id.textViewPoids); 
      holder.mImageUrl = (Button) view.findViewById(R.id.buttonSupprimer); 
    return view; 
    } 

    public class holder { 
     public Button mImageUrl; 
     public TextView mTvTitle; 
     public TextView mTvMediaName; 
    } 
} 

und die Datenbank-Handler alle Zeilen zu erhalten:

public List<Poids> getAllPoids() { 


     SQLiteDatabase db = this.getReadableDatabase(); 
     ArrayList<Poids> poidsList = new ArrayList<Poids>(); 

     Cursor cursor = db.rawQuery("SELECT * from " + TABLE_POIDS, 
       new String[] {}); 
/* 
     List<Poids> poidsList = new ArrayList<Poids>(); 
     // Select All Query 
     String selectQuery = "SELECT * FROM " + TABLE_POIDS; 

     SQLiteDatabase db = this.getWritableDatabase(); 
     Cursor cursor = db.rawQuery(selectQuery, null); 
*/ 
     // looping through all rows and adding to list 
     if (cursor.moveToFirst()) { 
      do { 
       Poids poid = new Poids(); 
       poid.setPoids(Integer.parseInt(cursor.getString(1))); 
       poid.setDate_enr(cursor.getString(2)); 
       // Adding contact to list 
       poidsList.add(poid); 
      } while (cursor.moveToNext()); 
     } 

     // return contact list 
     return poidsList; 
    } 

zu beenden, wie ich es nennen:

db.getAllPoids(); 
    List<Poids> poids = db.getAllPoids(); 

    ListView listView=(ListView) view.findViewById(R.id.listView); 

    newData = new ArrayList<String>(); 

    for (Poids val : poids) { 

     newData.add(val.getDate_enr()); 
     newData.add(String.valueOf(val.getPoids())); 
     //String log = "Id: " + val.getId() + " ,Date: " + val.getDate_enr() + " ,Poids: " + val.getPoids() + " ,Evolution: " + val.getEvolution() ; 
     // Writing Contacts to log 
     //Log.d("Name: ", log); 
    } 

    PoidsAdapter mAdapter = new PoidsAdapter(getActivity(), R.layout.listpoids, newData); 
    listView.setAdapter(mAdapter); 

Aber es funktioniert nicht, ich bin in einem Fragment.

UPDATE:

@Override 
public View getView(final int position, View view, final ViewGroup parent) { 
    final ViewHolder holder; 
     LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     view = inflater.inflate(R.layout.listpoids, null); 
     holder = new ViewHolder(); 
     holder.mTvTitle = (TextView) view.findViewById(R.id.textViewDate); 
     holder.mTvMediaName = (TextView) view.findViewById(R.id.textViewPoids); 
     holder.mImageUrl = (Button) view.findViewById(R.id.buttonSupprimer); 
    final Poids poids = mListPoids.get(position); 
     holder.mTvTitle.setText(poids.getDate_enr().toString()); 
     holder.mTvMediaName.setText(String.valueOf(poids.getPoids()).toString()); 

    return view; 
} 

Ich denke, der Fehler ist hier beceuse ich die beiden Zeilen zu sehen, aber bei setAdapter oder so etwas, hier fehlt etwas denke ich:

public class MonPoidsFragment extends Fragment { 

    DatabaseHandler db = new DatabaseHandler(getActivity()); 

    public MonPoidsFragment(){} 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     //View rootView = inflater.inflate(R.layout.accueil_activity, container, false); 
     View view = inflater.inflate(R.layout.monpoids_activity, container, false); 

     db = new DatabaseHandler(getActivity()); 

     db.getAllPoids(); 
     List<Poids> poids = db.getAllPoids(); 

     ListView listView=(ListView) view.findViewById(R.id.listView); 

     PoidsAdapter mAdapter = new PoidsAdapter(getActivity(), R.layout.listpoids, poids); 
     listView.setAdapter(mAdapter); 
     return view; 
    } 

    public Cursor getDetails() { 
     return db.getReadableDatabase().query("Poids ", null, null, null, null, null, null); 
     //This will return all data in your table. 
    } 
} 

Database Helper :

// Getting All Contacts 
public List<Poids> getAllPoids() { 
    List<Poids> poidsList = new ArrayList<Poids>(); 
    // Select All Query 
    String selectQuery = "SELECT * FROM " + TABLE_POIDS; 

    SQLiteDatabase db = this.getWritableDatabase(); 
    Cursor cursor = db.rawQuery(selectQuery, null); 

    // looping through all rows and adding to list 
    if (cursor.moveToFirst()) { 
     do { 
      Poids poid = new Poids(); 
      poid.setPoids(Integer.parseInt(cursor.getString(1))); 
      poid.setDate_enr(cursor.getString(2)); 
      //poid.setPoids(Integer.parseInt(cursor.getString(0))); 
      //poid.setDate_enr(cursor.getString(1)); 
      // Adding contact to list 
      poidsList.add(poid); 
     } while (cursor.moveToNext()); 
    } 

    // return contact list 
    return poidsList; 
} 
+0

könnten Sie beschreiben mehr, was nicht funktioniert hat? keine Ergebnisse angezeigt? oder ... ? –

+0

@ccsnoopy Ich habe listpoids.xml angezeigt, also 2 textview mit "TextView" als Wert und die Schaltfläche, als ob der lstview nur die xml-Datei zeigt – BJM

+0

Die untergeordneten Elemente des listview werden von Ihrem Adapter ausgefüllt, warum haben Sie 2 Textansichten und Button als Kinder von listview? – drulabs

Antwort

1

Sie haben dem TextView eigentlich nichts zugewiesen, außer Esel Wenn Sie den Halter anweisen, Komponenten in getView() anzuzeigen, müssen Sie ihnen auch Werte zuweisen.

Beispiel:

@Override 
public View getView(final int position, View view, final ViewGroup parent) { 
    final holder holder; 
     LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     view = inflater.inflate(R.layout.listpoids, null); 
     holder = new holder(); 
     holder.mTvTitle = (TextView) view.findViewById(R.id.textViewDate); 
     holder.mTvMediaName = (TextView) view.findViewById(R.id.textViewPoids); 
     holder.mImageUrl = (Button) view.findViewById(R.id.buttonSupprimer); 
     holder.mTvTitle.setText("something you need to assign from some data"); 
     holder.mTvMediaName.setText("this as well.."); 
return view; 
} 

Auch nicht mit dem Problem, da Ansicht Aufblasen ein teurer Prozess ist, sollten Sie zunächst prüfen, ob die View aufgeblasen wurde, bevor Sie aufzublasen.

Man könnte dies überprüfen: http://lucasr.org/2012/04/05/performance-tips-for-androids-listview/

+0

Arbeit, aber ich bekomme nur einen Wert :( – BJM

+0

könnte u bitte Ihren Code aktualisieren? –

+0

bearbeiten Blick auf bitte – BJM

Verwandte Themen