我正在尝试过滤数据库中的数据,并使用listview
将过滤后的数据显示到cursor
。不幸的是,从查询返回的光标不是空的,但是项目没有显示在活动中。此外,logcat中没有显示错误。
我的search_results.java:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search__results);
lview = (ListView) findViewById(R.id.list);
Intent intent = getIntent();
String from = intent.getStringExtra("from");
String to = intent.getStringExtra("to");
// String date = intent.getStringExtra("date");
// String clas = intent.getStringExtra("class");
myrailway = new no.nordicsemi.android.nrftoolbox.myRailwayAdapter(this);
// Cursor cursor = myrailway.getTrainDetails(from, to);
String[] FROM = null;
String[] TO = null;
String[] TRAINNAME = null;
String[] TRAINNO = null;
String[] DEPART = null;
String[] ARRIVAL = null;
Cursor cursor = myrailway.getTrainDetails(from, to);
if(cursor != null) {
Log.e("ERROR","NON EMPTY CURSOR");
int count = 0;
if (cursor.moveToFirst()) {
Log.e("ERROR","ENTERED LOOP");
do {
String stnfrom = cursor.getString(cursor.getColumnIndex(no.nordicsemi.android.nrftoolbox.myRailwayAdapter.CONTACTS_COLUMN_STNFROM));
String stnto = cursor.getString(cursor.getColumnIndex(no.nordicsemi.android.nrftoolbox.myRailwayAdapter.CONTACTS_COLUMN_STNTO));
String trainname = cursor.getString(cursor.getColumnIndex(no.nordicsemi.android.nrftoolbox.myRailwayAdapter.CONTACTS_COLUMN_NAME));
String trainno = cursor.getString(cursor.getColumnIndex(no.nordicsemi.android.nrftoolbox.myRailwayAdapter.CONTACTS_COLUMN_TRAINNUM));
String depart = cursor.getString(cursor.getColumnIndex(no.nordicsemi.android.nrftoolbox.myRailwayAdapter.CONTACTS_COLUMN_DEPART));
String arrival = cursor.getString(cursor.getColumnIndex(no.nordicsemi.android.nrftoolbox.myRailwayAdapter.CONTACTS_COLUMN_ARRIVAL));
FROM[count] = stnfrom; Log.e("fr",stnfrom);
TO[count] = stnto; Log.e("too",stnto);
TRAINNAME[count] = trainname; Log.e("trainanme",trainname);
TRAINNO[count] = trainno; Log.e("trainno",trainno);
DEPART[count] = depart; Log.e("depart",depart);
ARRIVAL[count] = arrival; Log.e("arrival",arrival);
count = count + 1;
cursor.close();
} while (cursor.moveToNext());
lviewAdapter = new ListViewAdapter(this, FROM, TO, DEPART, ARRIVAL, TRAINNAME, TRAINNO);
lview.setAdapter(lviewAdapter);
}
}
else
Log.e("ERROR","EMPTY CURSOR");
}
我的ListViewAdapter.java:
public class ListViewAdapter extends BaseAdapter {
Activity context;
String from[];
String to[];
String depart[];
String arrival[];
String trainname[];
String trainno[];
private no.nordicsemi.android.nrftoolbox.myRailwayAdapter myrailway;
public ListViewAdapter(Activity context, String[] from, String[] to, String[] depart, String[] arrival, String[] trainname, String[] trainno) {
super();
this.context = context;
this.from = from;
this.to = to;
this.depart = depart;
this.arrival = arrival;
this.trainname = trainname;
this.trainno = trainno;
}
@Override
public int getCount() {
return depart.length;
}
@Override
public Object getItem(int i) {
return depart[i];
}
@Override
public long getItemId(int i) {
myrailway = new no.nordicsemi.android.nrftoolbox.myRailwayAdapter(this.context);
Long recc= Long.valueOf(0);
Cursor c= myrailway.getpass(trainname[i]);
if(c!=null)
{
c.moveToFirst();
recc=c.getLong(0);
}
return recc;
}
private class ViewHolder {
TextView txtfrom;
TextView txtto;
TextView txttrainno;
TextView txttrainname;
TextView txtdepart;
TextView txtarrival;
}
@Override
public View getView(int position, View view, ViewGroup viewGroup) {
ViewHolder holder;
LayoutInflater inflater = context.getLayoutInflater();
if (view == null)
{
view = inflater.inflate(R.layout.listview_items, null);
holder = new ViewHolder();
holder.txtfrom = (TextView) view.findViewById(R.id.from);
holder.txtto = (TextView) view.findViewById(R.id.to);
holder.txttrainno = (TextView) view.findViewById(R.id.trainno);
holder.txttrainname = (TextView) view.findViewById(R.id.trainname);
holder.txtdepart = (TextView) view.findViewById(R.id.depart);
holder.txtarrival = (TextView) view.findViewById(R.id.arrival);
view.setTag(holder);
}
else
{
holder = (ViewHolder) view.getTag();
}
holder.txtfrom.setText(from[position]);
holder.txtto.setText(to[position]);
holder.txttrainno.setText(trainno[position]);
holder.txttrainname.setText(trainname[position]);
holder.txtdepart.setText(depart[position]);
holder.txtarrival.setText(arrival[position]);
return view;
}
}
我的search_results.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="no.nordicsemi.android.nrftoolbox.Search_Results">
<ListView
android:id="@+id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"/>
</RelativeLayout>
我的listview_items.xml:
<TableLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TableRow>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingTop="0dip" android:layout_gravity="top"
>
<TableRow>
<TextView
android:id="@+id/from"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_weight="1" android:layout_gravity="left|center_vertical"
android:textSize="16sp"
android:layout_marginLeft="10dip"
android:layout_marginTop="4dip"
android:textColor="#000000"
android:layout_span="1"
/>
<TextView
android:id="@+id/to"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_weight="1" android:layout_gravity="left|center_vertical"
android:textSize="16sp"
android:layout_marginLeft="10dip"
android:layout_marginTop="4dip"
android:textColor="#000000"
android:layout_span="1"
/>
</TableRow>
<TableRow>
<TextView
android:text=""
android:id="@+id/trainno"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_weight="1"
android:layout_gravity="left|center_vertical"
android:textSize="16sp"
android:textColor="#000000"
android:layout_marginLeft="10dip"
android:layout_marginTop="4dip"
android:gravity="left"/>
<TextView
android:text=""
android:id="@+id/trainname"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_weight="1"
android:layout_gravity="left|center_vertical"
android:textSize="16sp"
android:textColor="#000000"
android:layout_marginLeft="10dip"
android:layout_marginTop="4dip"
android:gravity="left"/>
</TableRow>
<TableRow>
<TextView
android:text=""
android:id="@+id/depart"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_weight="1"
android:layout_gravity="left|center_vertical"
android:textSize="16sp"
android:textColor="#000000"
android:layout_marginLeft="10dip"
android:layout_marginTop="4dip"
android:gravity="left"/>
<TextView
android:text=""
android:id="@+id/arrival"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_weight="1"
android:layout_gravity="left|center_vertical"
android:textSize="16sp"
android:textColor="#000000"
android:layout_marginLeft="10dip"
android:layout_marginTop="4dip"
android:gravity="left"/>
</TableRow>
</TableLayout>
<Button
android:id="@+id/book"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:onClick="book"
android:text="BOOK">
</Button>
</TableRow>
</TableLayout>
现在,光标不为空,但listview适配器不显示列表视图。 有人能指出代码中的错误吗?
答案 0 :(得分:1)
请使用此。
@Override
public int getCount() {
//here too;
return depart.lenght;
}
@Override
public Object getItem(int i) {
//there is error: repalce return null; with
return depart[i];
}
@Override
public long getItemId(int i) {
//change this too
return depart[i].getId();
}
答案 1 :(得分:0)
我可以看到可能发生这种情况的两个原因:
检查:在使用getView方法设置数据之前,检查并向日志打印每行数组中的所有数据。
答案 2 :(得分:0)
getCount(), getItem()和 getItemId()。
getCount()应该返回arraylist中的项目数;
getItem()应该从arraylist返回给定位置的项目;
getItemId()应该返回项目的内部id(如果有),或者数组中项目的位置或项目的哈希编号。