简体   繁体   中英

Passing data in from list to detail view in Android

I have a list view populated threw an SQlitedatabase but I need to pass to a detail activity from the list view. The problem is in passing the details from the listview activity to the detail activity because when I click the detail activity it gives me blank edit texts

Here is my listview activity:

public class consulter_note extends Activity implements AdapterView.OnItemClickListener{
    ListView list;
    SQLiteDatabase sqLiteDatabase;
    DataBaseOperationstwo dataBaseOperationstwo;
    Cursor cursor;
    ListDataAdapter listDataAdapter;
    String titre,objet;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_consulter_note);
        list = (ListView) findViewById(R.id.listView);
        listDataAdapter = new ListDataAdapter(getApplicationContext(),R.layout.notelist_row);
        list.setAdapter(listDataAdapter);
        list.setOnItemClickListener(this);
        dataBaseOperationstwo = new DataBaseOperationstwo(getApplicationContext());
        sqLiteDatabase = dataBaseOperationstwo.getReadableDatabase();
        cursor = dataBaseOperationstwo.getInformations(sqLiteDatabase);
        if (cursor.moveToFirst())
        {
            do
            {

                titre = cursor.getString(0);
                objet = cursor.getString(1);
                DataProvider dataProvider = new DataProvider(titre,objet);
                listDataAdapter.add(dataProvider);

            }while (cursor.moveToNext());
        }


    }




    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Intent intent = new Intent(this, note_details.class);
            startActivity(intent);
            intent.putExtra("titre", titre);
            intent.putExtra("objet", objet);
}
}

And here is my array adapter:

public class ListDataAdapter  extends ArrayAdapter{
    List list = new ArrayList();
    public ListDataAdapter(Context context, int resource) {
        super(context, resource);
    }
static class LayoutHandler
{
    TextView TITRE,OBJET;
}
    @Override
    public void add(Object object) {
        super.add(object);
        list.add(object);
    }

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

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View row = convertView;
        LayoutHandler layoutHandler;
        if (row == null)
        {
            LayoutInflater layoutInflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            row = layoutInflater.inflate(R.layout.notelist_row,parent,false);
            layoutHandler = new LayoutHandler();
            layoutHandler.TITRE = (TextView) row.findViewById(R.id.titredemo);
            layoutHandler.OBJET = (TextView) row.findViewById(R.id.objetdemo);
            row.setTag(layoutHandler);
        }
        else
        {
            layoutHandler = (LayoutHandler) row.getTag();

        }
        DataProvider dataProvider = (DataProvider) this.getItem(position);
        layoutHandler.TITRE.setText(dataProvider.getTitre());
        layoutHandler.OBJET.setText(dataProvider.getObjet());


        return row;
    }
}

The data provider class used in the array adapter:

public class DataProvider {
    private String titre,objet;
    public  DataProvider(String titre,String objet)
    {
        this.titre = titre;
        this.objet = objet;
    }

    public String getTitre() {
        return titre;
    }

    public void setTitre(String titre) {
        this.titre = titre;
    }

    public String getObjet() {
        return objet;
    }

    public void setObjet(String objet) {
        this.objet = objet;
    }
}

And finally my details activity. I'm only interested in the intent part; the rest has nothing to do with my problem:

public class note_details extends Activity {
    ImageButton Del;
    EditText PASSTITRE,USEROBJET;
    String Passtitre,Userobjet;
    DataBaseOperationstwo DOP;
    Context CTX = this;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Intent intent =getIntent();
        if(intent != null)
        {
            String objet = intent.getStringExtra("objet");
            String titre= intent.getStringExtra("titre");
            PASSTITRE.setText(objet);
            USEROBJET.setText(objet);

        }
        setContentView(R.layout.activity_note_details);
        Del = (ImageButton) findViewById(R.id.suppnote);
        PASSTITRE = (EditText) findViewById(R.id.titree);
        USEROBJET = (EditText) findViewById(R.id.objett);
        Del.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Passtitre = PASSTITRE.getText().toString();
                Userobjet = USEROBJET.getText().toString();
                DOP = new DataBaseOperationstwo(CTX);
                DOP.deleteNote(DOP,Passtitre,Userobjet);
                Toast.makeText(getBaseContext(),"note supprimé",Toast.LENGTH_LONG).show();
                finish();
            }
        });
   }
    public void liste(View v)
    {
        Intent i = new Intent(this, consulter_note.class);
        startActivity(i);
    }
    public void supprimer(View v)
    {

    }



}

My logcat doesn't show any errors but the details activity shows with empty edittexts.

You should first add extras to the Intent and then fire it:

Intent intent = new Intent(this, note_details.class);
intent.putExtra("titre", titre);
intent.putExtra("objet", objet);
startActivity(intent);

Another thing worth mentioning is that you should avoid doing DB queries on the main thread, as it will slow down your app. Use Loaders , or just run queries on worker threads.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM