Android - Listview personalizado solo muestra 1 objeto

 
Vista:
sin imagen de perfil
Val: 1
Ha aumentado su posición en 102 puestos en Android (en relación al último mes)
Gráfica de Android

Listview personalizado solo muestra 1 objeto

Publicado por Santiago (1 intervención) el 21/07/2018 03:48:46
Intento hacer una lista de tareas, cuando tengo que imprimir las tareas en la lista solo imprime una tarea

MainActivity.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package com.example.hermanos.basededatos;
 
 
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ListView;
 
import java.lang.reflect.Array;
import java.util.ArrayList;
 
public class MainActivity extends AppCompatActivity {
 
    ListView lv;
    ArrayList<String> lista;
 
    public int i;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        Datos datos[];
 
        datos=llenar_lista();
        lv= (ListView)findViewById(R.id.lista);
        DatosAdapter adapter = new DatosAdapter(this, R.layout.listview_item_row,datos);
        View header = (View)getLayoutInflater().inflate(R.layout.list_header_row,null);
        lv.addHeaderView(header);
        lv.setAdapter(adapter);
        //adaptador= new ArrayAdapter(this, android.R.layout.simple_list_item_1,lista);
        //lv.setAdapter(adaptador);
 
    }
 
    public void n_user(View view){
        Intent i = new Intent(this, Main2Activity.class);
        startActivity(i);
    }
 
    public Datos[] llenar_lista() {
        Datos datos[]=new Datos[]{};
        Basededatos n_user = new Basededatos(this, "Registro", null, 1);
        SQLiteDatabase bd = n_user.getWritableDatabase();
 
        String sql = "SELECT * FROM tareas";
        Cursor registros = bd.rawQuery(sql, null);
        i = 0;
        if (registros.moveToFirst()) {
            do {
                datos[i]= new Datos(registros.getString(1));
                i++;
            } while (registros.moveToNext());
        }
        return datos;
    }
}

Datos.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
package com.example.hermanos.basededatos;
 
public class Datos {
 
        public String tarea;
 
        public Datos(){
            super();
        }
 
        public Datos(String tarea){
            super();
            this.tarea = tarea;
        }
}

DatosAdapter.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package com.example.hermanos.basededatos;
 
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
 
public class DatosAdapter extends ArrayAdapter<Datos> {
 
    Context mycontext;
    int mylayout_resourseID;
    Datos mydata[] = null;
 
    public DatosAdapter(Context context,int layout_resourseID, Datos[] data){
 
        super(context,layout_resourseID,data);
 
        this.mycontext = context;
        this.mylayout_resourseID = layout_resourseID;
        this.mydata = data;
 
        }
 
    public View getView(int position, View convertView, ViewGroup parent){
        View row = convertView;
        Datosholder holder = null;
 
        if(row==null){
            LayoutInflater inflater = ((Activity)mycontext).getLayoutInflater();
            row = inflater.inflate(mylayout_resourseID,parent,false);
            holder = new Datosholder();
            holder.textview = (TextView)row.findViewById(R.id.txt);
            row.setTag(holder);
        }else{
            holder = (Datosholder)row.getTag();
        }
 
        Datos datos = mydata[position];
        holder.textview.setText(datos.tarea);
 
        return row;
    }
 
    static class Datosholder{
        TextView textview;
    }
 
}

Basededatos.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
package com.example.hermanos.basededatos;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
 
public class Basededatos extends SQLiteOpenHelper{
 
    public Basededatos(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
        super(context, name, factory, version);
    }
 
    @Override
    public void onCreate(SQLiteDatabase Registro) {
        Registro.execSQL("create table tareas(codigo integer primary key autoincrement, tarea text)");
    }
 
    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
 
    }
}

listview_item_row.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<?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">
 
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal">
 
        <CheckBox
            android:id="@+id/elr"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.1" />
 
        <TextView
            android:id="@+id/txt"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="TextView" />
    </LinearLayout>
</LinearLayout>

activity_main.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
 
 
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
 
        <ListView
            android:id="@+id/lista"
            android:layout_width="match_parent"
            android:layout_height="284dp"
            android:clickable="false"
            android:footerDividersEnabled="false"
            android:headerDividersEnabled="false" />
 
        <Button
            android:id="@+id/button5"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:onClick="n_user"
            android:text="@string/txt_n" />
    </LinearLayout>
 
</android.support.constraint.ConstraintLayout>

list_header_row.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?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="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/encabezado"
        android:background="#396640"
        android:textColor="#ffffff"
        android:gravity="center_horizontal"
        />
 
</LinearLayout>

Es mi primera vez haciendo esto, si me podrian ayudar lo agradeceria
Muchas gracias
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder