2016-11-19 2 views
0

Meine ListedActivity ist leer und löst keine Fehler oder Ausnahmen aus. Es sitzt einfach da und macht gar nichts.Aktivität der leeren Liste, ListView-Objekt ist nicht null

Meine Datenbank sollte nicht leer sein, es gibt einige Einträge.

public class ListedActivity extends Activity { 

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

     LayoutInflater inflater = (LayoutInflater) getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

     LinearLayout mContainer = (LinearLayout) inflater.inflate(R.layout.activity_list, null); 
     ListView listView = (ListView) mContainer.findViewById(R.id.listedactivity_list_view); 
     DBHelper dbHelper = new DBHelper(getApplicationContext()); 
     ArrayList<MyLayoutObject> b = dbHelper.getAllResults(); 

     MyAdapter a = new MyAdapter(this, b); 

     listView.setAdapter(a); 

     listView.setOnItemClickListener(
      new AdapterView.OnItemClickListener(){ 
       @Override 
       public void onItemClick(AdapterView<?> arg0, View view, int position, long id) { 
        Intent intent = new Intent(ListedActivity.this, ResultsActivity.class); 
        intent.putExtra("position", position); 
        intent.putExtra("id", id); 
        startActivity(intent); 
       } 
      } 
     ); 
    } 
} 

Aktivität Liste sollte alle Ergebnisse aus meiner Datenbank schreiben. Hier ist es .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/activity_list" 
    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="smart.tuke.sk.makac.ListedActivity"> 

    <ListView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:background="#eeeeee" 
     android:id="@+id/listedactivity_list_view" /> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@android:id/empty" 
     android:text="@string/no_data" 
     android:visibility="gone" /> 
</LinearLayout> 

Dies ist mein Zeilenlayout. Jede Zeile sollte so aussehen. Seine .xml ist unten.

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

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="wrap_content" 
     android:layout_height="61dp" 
     android:orientation="vertical"> 

    <TextView 
     android:id="@+id/date_recorded" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:padding="10dp" 
     android:textSize="18sp" 
     android:textStyle="bold" 
     android:textColor="#111111" /> 

    </LinearLayout> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="vertical"> 

     <TextView 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:id="@+id/duration_recorded" /> 

     <TextView 
      android:layout_width="345dp" 
      android:layout_height="wrap_content" 
      android:id="@+id/distance_recorded" /> 

     <TextView 
      android:layout_width="345dp" 
      android:layout_height="wrap_content" 
      android:id="@+id/pace_recorded" /> 

     <TextView 
      android:layout_width="345dp" 
      android:layout_height="wrap_content" 
      android:id="@+id/calories_recorded" /> 
    </LinearLayout> 

</LinearLayout> 

MyLayoutObject 5 Saiten mit Getter, nichts mehr

Helfen Sie mir, Überlauf Kenobi, du bist meine einzige Hoffnung!

edit: Adaptercode

class MyAdapter extends BaseAdapter{ 

    private LayoutInflater inflater; 
    private ArrayList<MyLayoutObject> objects; 

    private class ViewHolder { 
     TextView date; 
     TextView duration; 
     TextView distance; 
     TextView pace; 
     TextView calories; 
    } 

    MyAdapter(Context context, ArrayList<MyLayoutObject> objects) { 
     inflater = LayoutInflater.from(context); 
     this.objects = objects; 
    } 

    public int getCount() { 
     return objects.size(); 
    } 

    public MyLayoutObject getItem(int position) { 
     return objects.get(position); 
    } 

    public long getItemId(int position) { 
     return position; 
    } 

    public View getView(int position, View convertView, ViewGroup parent) { 
     ViewHolder holder; 
     if(convertView == null) { 
      holder = new ViewHolder(); 
      convertView = inflater.inflate(R.layout.row_layout, null); 
      holder.date = (TextView) convertView.findViewById(R.id.date_recorded); 
      holder.distance = (TextView) convertView.findViewById(R.id.distance_recorded); 
      holder.duration = (TextView) convertView.findViewById(R.id.duration_recorded); 
      holder.pace = (TextView) convertView.findViewById(R.id.pace_recorded); 
      holder.calories = (TextView) convertView.findViewById(R.id.calories_recorded); 
      convertView.setTag(holder); 
     } else { 
      holder = (ViewHolder) convertView.getTag(); 
     } 
     holder.date.setText(objects.get(position).getDate()); 
     holder.duration.setText(objects.get(position).getDuration()); 
     holder.distance.setText(objects.get(position).getDistance()); 
     holder.pace.setText(objects.get(position).getPace()); 
     holder.calories.setText(objects.get(position).getCalories()); 
     return convertView; 
    } 
} 

Und .xml des Adapters

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

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="wrap_content" 
     android:layout_height="61dp" 
     android:orientation="vertical"> 

    <TextView 
     android:id="@+id/date_recorded" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:padding="10dp" 
     android:textSize="18sp" 
     android:textStyle="bold" 
     android:textColor="#111111" /> 

    </LinearLayout> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="vertical"> 

     <TextView 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:id="@+id/duration_recorded" /> 

     <TextView 
      android:layout_width="345dp" 
      android:layout_height="wrap_content" 
      android:id="@+id/distance_recorded" /> 

     <TextView 
      android:layout_width="345dp" 
      android:layout_height="wrap_content" 
      android:id="@+id/pace_recorded" /> 

     <TextView 
      android:layout_width="345dp" 
      android:layout_height="wrap_content" 
      android:id="@+id/calories_recorded" /> 
    </LinearLayout> 

</LinearLayout> 
+0

Versuchen listview.setAdapter Verschiebung nach setContentView (R.layout.activity_list) – Swr7der

+0

ich sie verschoben wird, ist die Aktivität immer noch leer –

Antwort

0

Sie versuchen, eine android.R Ansicht in Ihrer aufgeblasenen benutzerdefinierte Ansicht zu "finden". Das Layout Sie Aufpumpen nicht enthält diese Ansicht (solche Ansichten erscheinen nur innerhalb vordefinierten Layouts, wie android.R.layout.simple_list_item)

Auch betrachten Sie diese Zeile in XML Ihre Aktivität ist: android:id="@android:id/list"

Sie sind nicht wirklich eine ID zuweist das Element. Mach es so: android:id="@+id/listedactivity_list_view" (das wichtige Bit ist die @ + ID).

nun in Ihrer Tätigkeit, dies zu tun: ListView listView = (ListView) mContainer.findViewById(R.id.listedactivity_list_view);

Durch es so tun, Sie verweisen, tatsächlich ein tatsächliches Element innerhalb Ihrer Ansicht statt Referenzierung etwas „Geister Elements“, die nicht in Ihrem Layout nicht vorhanden .

EDIT: Nach dem erneuten Lesen mehr Ihren Code sorgfältig habe, fand ich endlich das Problem ... bei diesen Zeilen ein:

setContentView(R.layout.activity_list); 

LayoutInflater inflater = (LayoutInflater) getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
LinearLayout mContainer = (LinearLayout) inflater.inflate(R.layout.activity_list, null); 
ListView listView = (ListView) mContainer.findViewById(R.id.listedactivity_list_view); 

In Zeile 1 (des Snippets), Sie setzen die Inhaltsansicht einer bestimmten Layout-Ressource. In Zeile 3 (mContainer) füllen Sie dieses Layout jedoch WIEDER - also wenn Sie die findViewById in Zeile 4 ausführen, suchen Sie tatsächlich etwas in der aufgeblähten Ansicht WEITER als die tatsächlich angezeigte Ansicht. So ist die Lösung für Ihr Problem ist es, die inflater und mContainer Variable zu entfernen:

setContentView(R.layout.activity_list); 

ListView listView = (ListView) findViewById(R.id.listedactivity_list_view); 
+0

Es ist immer noch leer, Auch nachdem ich die Änderungen gemacht habe, die du erwähnt hast. –

+0

Sind Sie sicher, dass Ihr Adapter wie vorgesehen funktioniert? Könnten Sie uns bitte auch den Adaptercode geben? – Aenadon

+0

Adapter Code wurde in. –

Verwandte Themen