我正在尝试将DbHandler.java
中的数据添加到textView
中的MainActivity.java.
我已经完成了一个类似的项目,使用listView
向List<String>
添加数据
现在我想为textView
我的MainActivity.java代码填充列表视图:
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
String loc = parent.getItemAtPosition(position).toString();
DbHandler db = new DbHandler(getApplicationContext());
List<String> BusNoListUsingLoc = db.getBusNoUsingLoc(loc);
ArrayAdapter<String> lvAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, BusNoListUsingLoc);
listView.setAdapter(lvAdapter);
}
我的DbHandler.java:
public List<String> getBusNoUsingLoc(String loc)
{
List<String> BusRouteList = new ArrayList<String>();
try
{
SQLiteDatabase db = this.getWritableDatabase();
String[] cols={"From_Loc","To_Loc","Route_1","Route_2","Route_3","Route_4","Route_5","Route_6","Route_7","Route_8","Route_9","Route_10","Route_11","Route_12","Route_13","Route_14","Route_15"};
String query="SELECT Bus_No_Type FROM MDUBusRoutes WHERE";
for (int i=0;i<cols.length;i++)
{
if(i!=cols.length-1)
query+=" UPPER(" + cols[i] + ")= UPPER(?) OR";
else
query+=" UPPER(" + cols[i] + ")= UPPER(?) ORDER BY Total_Stops DESC";
}
Cursor CursorLocationRoute = db.rawQuery(query, new String[]{loc,loc,loc,loc,loc,loc,loc,loc,loc,loc,loc,loc,loc,loc,loc,loc,loc});
if (CursorLocationRoute.moveToFirst())
{
do
{
BusRouteList.add(CursorLocationRoute.getString(0));
}
while (CursorLocationRoute.moveToNext());
}
CursorLocationRoute.close();
db.close();
} catch (Exception ignored)
{
}
return BusRouteList;
}
答案 0 :(得分:1)
在我看来,ListView
会更好地为你服务,因为它会在需要时滚动,它会使你的行保持良好和一致,它基本上会处理你拥有的任何可变长度条目,但如果你真的想要使用TextView
执行此操作然后尝试:
(如果您需要滚动,因为条目数量太大而您的TextView
不再适合屏幕,那么您需要将LinearLayout
放入{{1} }}。)
在您的活动布局文件中放置ScrollView
并定义ID。
LinearLayout
您可以根据需要更改LinearLayout linearLayout = (LinearLayout) findViewById(R.id.linear_layout_id);
for(String s : BusRouteList){
TextView tv = new TextView(this);
tv.setText(s);
tv.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT));
linearLayout.addView(tv);
}
。