在Android中将数据从列表传递到详细信息视图

时间:2015-05-22 16:45:26

标签: android listview android-intent android-sqlite android-arrayadapter

我有一个列表视图填充了一个SQlitedatabase但我需要从列表视图传递给详细活动。问题在于将详细信息从listview活动传递到详细信息活动,因为当我单击详细信息活动时,它会为我提供空白的编辑文本

以下是我的listview活动:

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);
}
}

这是我的数组适配器:

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;
    }
}

数组适配器中使用的数据提供程序类:

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;
    }
}

最后我的细节活动。我只对意图部分感兴趣;其余的与我的问题无关:

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)
    {

    }



}

我的logcat没有显示任何错误,但详细信息活动显示为空edittexts。

1 个答案:

答案 0 :(得分:0)

您应首先向SELECT Name, DENSE_RANK() OVER(ORDER BY Name) [Count] FROM dbo.YourTable; 添加额外内容,然后触发它:

Intent

值得一提的另一件事是你应该避免在主线程上进行数据库查询,因为它会降低你的应用程序速度。使用Loaders,或者只在工作线程上运行查询。