gpt4 book ai didi

java - 在特定列中显示数据

转载 作者:太空宇宙 更新时间:2023-11-04 13:21:12 25 4
gpt4 key购买 nike

请帮帮我...如何才能将数据显示到特定的列???我不希望数据显示为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

数据应显示在第二行

最佳答案

如果我正确理解您的要求,您希望显示存储在数组列表 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;
}
}

此代码使用布局文件:

<?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>

然后,通常在 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 对象,但这应该让您知道如何做到这一点。

关于java - 在特定列中显示数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33044739/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com