显示特定列中的数据

时间:2015-10-09 18:14:03

标签: java android sqlite

请帮助我...如何将数据显示到特定列?我不希望数据显示为Toast,但我希望数据显示为行...如何实现此目的?我真的不知道。任何建议都将不胜感激。

WorkDetailsTable.Java

 Button btn1=(Button)findViewById(R.id.button2);
        btn1.setOnClickListener(new View.OnClickListener() {
            public void onClick(View arg0) {
                AlertDialog.Builder builder=new AlertDialog.Builder(WorkDetailsTable.this);
                builder.setTitle("Data Saved");
                builder.setMessage("Are you sure you want to save?");
                builder.setIcon(android.R.drawable.ic_dialog_alert);
                builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {


                     public void onClick(DialogInterface dialog, int ii) {
                            long ab = ts.insertTimeSheet(name, weather, date, status);
                            Toast.makeText(context, "Data Saved", Toast.LENGTH_SHORT).show();
                            Intent intent=new Intent(WorkDetailsTable.this,DisplayData.class);

                                intent.putExtra("name",name); // pass name to DisplayData
                                startActivity(intent);
 });

DisplayData.java

public class DisplayData extends AppCompatActivity {
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.displaydata);
        MyDatabaseHelper db=new MyDatabaseHelper(this);
        InfoAPI I1=new InfoAPI(this);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        final String name1 = getIntent().getExtras().getString("name");
        Toast.makeText(getApplicationContext(), name1, Toast.LENGTH_SHORT).show();
        if (name1.equals("X Y Z")) ;

        {
            Log.d("Reading: ", "Reading all contacts..");
            List<Info> info=I1.getAllContacts(name1); //refer to InfoAPI
            for(Info in:info)
            {
                String 

log="Weather:"+in.getWeather()+",Date:"+in.getDate()+"Status:"+in.getStatus();
                    Log.d("Name: ", log);
                   // Toast.makeText(getApplicationContext(), log, Toast.LENGTH_SHORT).show();
// what should write so data can be displayed in certain manner?
                }

            }


        }
    }

InfoAPI.java

 public List<Info> getAllContacts(String name) {

        List<Info> contactList = new ArrayList<Info>();
        String selectQuery = ("SELECT Weather,Date,Status FROM "+MyDatabaseHelper.TABLE_INFO);
        database=dbHelper.getWritableDatabase();
        Cursor cursor = database.query(MyDatabaseHelper.TABLE_INFO,new String[]{MyDatabaseHelper.ID,MyDatabaseHelper.Name,MyDatabaseHelper.Weather,MyDatabaseHelper.Date,MyDatabaseHelper.Status},MyDatabaseHelper.Name+"=?", new String[] { String.valueOf(name)}, null, null, null, null);
        // looping through all rows and adding to list
        if (cursor.moveToFirst()) {
            do {
                Info contact = new Info();  //refer Info
                contact.setWeather((cursor.getString(0)));
                contact.setDate(cursor.getString(1));
                contact.setStatus(cursor.getString(2));
                contactList.add(contact);
            } while (cursor.moveToNext());
        }
        // return contact list
        return contactList;
    }

Info.java

public class Info {

    private int id;
    private String name;
    private String weather;
    private String date;
   private String status;


    public Info() {
    }

    public void setStatus(String  status)
    {
        this.status=status;
    }

    public String getStatus()
    {
        return this.status;
    }

    public void setID(int id)
    {
        this.id=id;
    }

    public int getID()
    {
        return this.id;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getName() {
        return this.name;
    }

    public void setWeather(String weather)
    {
        this.weather=weather;
    }

    public String getWeather()
    {
        return this.weather;
    }

    public void setDate(String date)
    {
        this.date=date;
    }

    public String getDate()
    {
        return this.date;
    }


}

enter image description here

数据应显示在第二行

2 个答案:

答案 0 :(得分:0)

我无法强调:http://developer.android.com/reference/packages.html

我认为您正在寻找的是TableLayout。

你的layout.xml应该是

<LinearLayout ...>
    <TableLayout ... />
</LinearLayout>

您可以在活动findViewById(R.id.table)

中获得对它的引用

从那里循环访问您的数据。

for (Info contact : contactList) {
    TableRow mRow = new TableRow();
    LinearLayout cell = new LinearLayout(...); // honestly you can just appendt he text view, but it's nice to have more control, sometimes
    TextView cellText = new TextView(...);
    cellText.setText(contact.x);
    cell.addView(cellText);
    mRow.addView(cell);
}

在您的情况下,您每行添加三个单元格:日期,天气,状态。

编辑:不要忘记在表格中添加行。 table.addView(mRow)

答案 1 :(得分:0)

如果我正确理解您的要求,您需要显示存储在数组列表contactList中的Info对象列表中的数据。

这通常最好使用带有自定义布局的ListView和扩展ArrayAdapter的类。这是一个简单的ArrayAdapter示例,显示股票报价信息:

  class StockArrayAdapter extends ArrayAdapter
{
    Context myContext;
    ArrayList<Stock> stocks;
    int myLayout;
    LayoutInflater inflator;

    public StockArrayAdapter(Context context, int resource) {
        super(context, resource);
        ArrayList<Stock> stocks = new ArrayList<Stock>();
        initialize(context, resource, stocks);
    }

    public StockArrayAdapter(Context context, int resource, ArrayList<Stock> stocks) {
        super(context, resource, stocks);

        initialize(context, resource, stocks);

    }

    private void initialize(Context context, int resource, ArrayList<Stock> objects) {
        myContext = context;
        myLayout = resource;
        stocks = objects;
        inflator = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View rowView = inflator.inflate(myLayout, parent, false);
        TextView nameTextView = (TextView) rowView.findViewById(R.id.nameTextView);
        TextView lastTextView = (TextView) rowView.findViewById(R.id.lastTextView);
        TextView changeTextView = (TextView) rowView.findViewById(R.id.changeTextView);
        nameTextView.setText(stocks.get(position).CompanyName);
        lastTextView.setText(stocks.get(position).LastTrade);
        changeTextView.setText((stocks.get(position).Change));

        return rowView;
    }
}

此代码使用布局文件:

&#13;
&#13;
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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="fill_parent"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="Medium Text"
        android:id="@+id/nameTextView"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:text="@string/lastString"
        android:id="@+id/textView2"
        android:layout_below="@+id/nameTextView"
        android:layout_alignParentStart="true"
        android:layout_marginLeft="10dp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:text="@string/zerosString"
        android:id="@+id/lastTextView"
        android:layout_below="@+id/nameTextView"
        android:layout_toEndOf="@+id/textView2"
        android:layout_marginLeft="10dp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:text="@string/changeString"
        android:id="@+id/textView"
        android:layout_alignTop="@+id/lastTextView"
        android:layout_toEndOf="@+id/lastTextView"
        android:layout_marginLeft="10dp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:text="@string/zerosString"
        android:id="@+id/changeTextView"
        android:layout_marginLeft="10dp"
        android:layout_alignTop="@+id/textView"
        android:layout_toEndOf="@+id/textView"
        android:layout_marginStart="33dp" />
</RelativeLayout>
&#13;
&#13;
&#13;

然后通常在OnCreate中,您将创建适配器的实例,并使用它的setAdapter方法将其分配给ListView。就我而言,我有几个领域    ArrayList myStock;     StockArrayAdapter myAdapter;

在我的OnCreate中有这个代码。

        myStock = new ArrayList<Stock>();
    myAdapter = new StockArrayAdapter(this, R.layout.stock_row_layout, myStock);
    ListView stockListView = (ListView) findViewById(R.id.stocksListView);
    stockListView.setAdapter(myAdapter);

当然,您需要创建一个显示Info对象的对象,但这应该让您知道如何做到这一点。