在ListView中列出信息而不是从数据库中的TextView - Android

时间:2017-04-28 15:29:12

标签: java android listview textview

我的应用程序中有一个活动,它将我数据库中的所有信息列入TextView,但我想在ListView中打印出来。我不确定如何做到这一点,所以这是我到目前为止。

#include <stdio.h>
#include <string.h>
#include <stddef.h>
#include <stdlib.h>
#include "mpi.h"
main(int argc, char **argv ) {
    char message[20];
    int i,j, ressize, out, numprocs, myid, proc, res[10];
    MPI_Status status;
    MPI_Init(&argc, &argv);
    MPI_Comm_size(MPI_COMM_WORLD, &numprocs);
    MPI_Comm_rank(MPI_COMM_WORLD, &myid);

    for(i=0;i<2;i++)
        {
            if(myid == 0)
            {
                for (proc = 0; proc < numprocs; proc++)
                {
                    MPI_Send(&i, 1, MPI_INT, proc, 99, MPI_COMM_WORLD);
                }

                for (proc = 1; proc < numprocs; proc++)
                {
                    MPI_Recv(res, sizeof(res), MPI_INT, MPI_ANY_SOURCE, 99, MPI_COMM_WORLD, &status);
                    for(j=0;j<10;j++){
                        printf("result = %i\n",res[j]);
                    }

                }
            }
            else {
                MPI_Recv(&i, 1 , MPI_INT, 0, 99, MPI_COMM_WORLD, &status);
                for(j=0;j<10;j++){
                    res[j]=i+10;
                    printf("j= %i, res= %i\n",j,i+10);
                }
                ressize = sizeof(res);
                MPI_Send(res, ressize, MPI_INT, 0, 99, MPI_COMM_WORLD);
            }
        }
    MPI_Finalize();
    }

2 个答案:

答案 0 :(得分:0)

将ListView添加到您的活动布局中。然后在您的打印功能中,将您的会议列表传递给ArrayAdapter,然后将其设置为ListView适配器,如下所示:

ArrayAdapter adapter = new ArrayAdapter < String > (this, R.layout.LIST_ID, list);

ListView listView = (ListView)findViewById(R.id.LIST_ID);

listView.setAdapter(adapter);

<强>更新 您的代码将类似于:

public class ViewMeeting extends Activity implements OnClickListener {

    EditText deleteText;
    Button deleteButton;
    TextView tv;
    List < MeetingModel > list = new ArrayList <  > ();
    DatabaseHelper db;
    ArrayList meetings = new ArrayList < String > ();
    ListView listView;
     @ Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.view_meeting);
        listView = (ListView)findViewById(R.id.LIST_ID);
        db = new DatabaseHelper(getApplicationContext());
        deleteText = (EditText)findViewById(R.id.editText6);
        deleteButton = (Button)findViewById(R.id.delete1);
        tv = (TextView)findViewById(textView6);
        deleteButton.setOnClickListener(this);
        list = db.getAllMeetingsList();
        print(list);
    }
    //prints details from database table into a textview
    private void print(List < MeetingModel > list) {

        String value = "";
        for (MeetingModel sm: list) {
            value = value + " ID: " + sm.id + "\n" + " Title: " + sm.title + "\n" + "
                        Date: " + sm.date + "\n" + " Time: " + sm.time + "\n" + " Location:
                        " + sm.location + "\n" + "\n";
            meetings.add(value);
        }
        //tv.setText(value);
        ArrayAdapter adapter = new ArrayAdapter < String > (this, R.layout.LIST_ID, meetings);
        listView.setAdapter(adapter);
    }
}

答案 1 :(得分:0)