0

So I have been working on a project app that reads from a database and prints the contents of the DB into a list view. I now want to print more then just one column, I want to add two buttons and an edittext to the row that is being printed. I have created the new xml file that I want to use but when i try to set the new layout to be this xml file it doesnt work. Can anyone explain or show me how to fix my problem?

This is the XML layout I want to use for each row

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">

<TextView android:id="@+id/item1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="30sp"
    android:paddingBottom="5dp"
    android:hint="@string/hint"/>

<Button
    android:id="@+id/plusButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/plusButton"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true" />

<EditText
    android:id="@+id/edit1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:hint="@string/hint"
    android:layout_alignParentTop="true"
    android:layout_toLeftOf="@+id/minusButton"
    android:layout_toStartOf="@+id/minusButton"
    android:layout_marginRight="30dp" />

<Button
    android:id="@+id/minusButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/minusButton"
    android:layout_alignParentTop="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true" />

</RelativeLayout>

And this is the class where I am displaying the contents of the SQLite database, the error occurs on line 100 when i try to change the android.R.layout to the row.xml file

package com.example.rory.dbtest;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;

import android.app.Activity;
import android.app.ListActivity;
import android.content.Intent; 
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

import com.pinchtapzoom.R;

public class MyActivity extends Activity {



@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);


    setContentView(R.layout.activity_my);

    Button addBtn = (Button)findViewById(R.id.add);
    addBtn.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent i = new Intent(MyActivity.this, addassignment.class);
            startActivity(i);
        }
    });

    Button deleteBtn = (Button)findViewById(R.id.delete);
    deleteBtn.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent i = new Intent(MyActivity.this, Delete.class);
            startActivity(i);
        }
    });

    Button updateBtn = (Button)findViewById(R.id.update);
    updateBtn.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent i = new Intent(MyActivity.this, Update.class);
            startActivity(i);
        }
    });


    try {
        String destPath = "/data/data/" + getPackageName() + "/databases/AssignmentDB";
        File f = new File(destPath);
        if (!f.exists()) {
            CopyDB( getBaseContext().getAssets().open("mydb"),
                    new FileOutputStream(destPath));
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    com.example.rory.dbtest.DBAdapter db = new com.example.rory.dbtest.DBAdapter(this);
    db.open();



    ArrayList<String> data_list=new ArrayList<String>();
    ListView lv=(ListView)findViewById(R.id.listView1);
    Cursor c = db.getAllRecords();

    if (c.moveToFirst())
    {
        do {
            data_list.add(c.getString(2));
            //data_list.add(c.getString(1));
            //DisplayRecord(c);
        } while (c.moveToNext());
    }
    ArrayAdapter<String> aa=new ArrayAdapter<String>(getApplicationContext(), android.R.layout.row,  data_list);
    lv.setAdapter(aa);

}

private class DBAdapter extends BaseAdapter {
    private LayoutInflater mInflater;
    //private ArrayList<>

    @Override
    public int getCount() {

        return 0;
    }

    @Override
    public Object getItem(int arg0) {

        return null;
    }

    @Override
    public long getItemId(int arg0) {

        return 0;
    }

    @Override
    public View getView(int arg0, View arg1, ViewGroup arg2) {

        return null;
    }

}

public void CopyDB(InputStream inputStream, OutputStream outputStream)
        throws IOException {
    //---copy 1K bytes at a time---
    byte[] buffer = new byte[1024];
    int length;
    while ((length = inputStream.read(buffer)) > 0) {
        outputStream.write(buffer, 0, length);
    }
    inputStream.close();
    outputStream.close();
}



public void DisplayRecord(Cursor c)
{
   Toast.makeText(this,
            "id: " + c.getString(0) + "\n" +
                    "Item: " + c.getString(1) + "\n" +
                    "Litres:  " + c.getString(2),
            Toast.LENGTH_SHORT).show();
}

}

My new custom adapter class package com.example.rory.dbtest;

import android.content.Context;
import android.database.Cursor;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CursorAdapter;
import android.widget.TextView;

import com.pinchtapzoom.R;

/**
 * Created by Rory on 07/11/2014.
 */

public class MyCustomAdapter extends CursorAdapter {

DBAdapter db = new DBAdapter(this);

private LayoutInflater cursorInflater;

public MyCustomAdapter(Context context, Cursor c) {
    super(context, c);

    cursorInflater = (LayoutInflater) context.getSystemService(
            Context.LAYOUT_INFLATER_SERVICE);
}

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

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

    TextView textViewTitle = (TextView) view.findViewById(R.id.row);
    String title = cursor.getString( cursor.getColumnIndex( DATABASE_TABLE.KEY_ITEM ) )
    textViewTitle.setText(title);

}
}

1 Answers1

0

Your question has been asked many times before here. Check this link Instead of using refular ArrayAdapter, you need to create a custom adapter class and populate the view in your list items.

Community
  • 1
  • 1
Darpan
  • 5,623
  • 3
  • 48
  • 80
  • I have created what i think is the right adapter I need, but i amnt sure how to implement it as i am quite new to android and this concept, would you mind explaining or showing me how to do it correctly? –  Nov 07 '14 at 12:19
  • it has been asked many times. google 'create custom listadapter' and you will figure out how. Check this tutorial http://www.vogella.com/tutorials/AndroidListView/article.html – Darpan Nov 07 '14 at 12:42