我正在使用SimpleCursorAdapter,由于某种原因,有时findViewById
方法会向某些TextView返回null。 id对应于XML Layout小部件。关于为什么的任何想法?
这是OrderList.java,我有SimpleCursorAdapter:
package com.orderdelivery;
import java.util.ArrayList;
import android.app.Activity;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.LayoutInflater;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.View.OnLongClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.CursorAdapter;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
import com.orderdelivery.DataAdapter;
public class OrderList extends ListActivity {
private ListView list;
ArrayList<Order> orders;
OrderListAdapter oAdapter;
SimpleOrderAdapter oCursorAdapter;
private Runnable viewOrders;
private ProgressDialog progress=null;
private DataAdapter dbAdapter;
private int[] views;
private String[] columns;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
list = getListView();
// list.setAdapter(oCursorAdapter);
list.setOnItemClickListener(new OnItemClickListener(){
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3)
{
Cursor cursor = (Cursor) list.getItemAtPosition(arg2);
Intent intent = new Intent("com.orderdelivery.GROCERYITEMLIST");
Bundle bundle = new Bundle();
intent.putExtra("order_id", cursor.getString(cursor.getColumnIndex("order_id")));
startActivity(intent);
// TODO Auto-generated method stub
}
}
);
registerForContextMenu(getListView());
list.setOnLongClickListener(new OnLongClickListener(){
public boolean onLongClick(View v) {
// TODO Auto-generated method stub
return false;
}});
// orders = new ArrayList<Order>();
//
// for(int i =0; i<5;i++){
// orders.add(new Order("19995","Pueblo, Maya",i,"Pending", null, null, null)); //Customer, items, confirmation...
// }
//
// oAdapter = new OrderListAdapter(this,R.layout.row,orders);
// list.setAdapter(oAdapter);
//
dbAdapter = new DataAdapter(this);
dbAdapter.open();
dbAdapter.deleteAll();
columns = new String[] {"order_id","retailer_name","qty","status"};
views = new int[]{R.id.orderNumber,R.id.location,R.id.quantity,R.id.status};
oCursorAdapter = new SimpleOrderAdapter(this,R.layout.row,dbAdapter.getAllOrders(),columns,views);
list.setAdapter(oCursorAdapter);
// Cursor oCursor = dbAdapter.getAllOrders();
viewOrders = new Runnable(){
public void run() {
// TODO Auto-generated method stub
updateData();
runOnUiThread(returnRes);
}
};
Thread thread = new Thread(viewOrders,"getOrderThread");
progress = ProgressDialog.show(this,"Please wait...","Retrieving assigned orders...", true);
thread.start();
}
private Runnable returnRes = new Runnable(){
public void run() {
// TODO Auto-generated method stub
oCursorAdapter.changeCursorAndColumns(dbAdapter.getAllOrders(), columns, views);
oCursorAdapter.notifyDataSetChanged();
progress.dismiss();
}
};
private void updateData(){
dbAdapter.addOrder("1995", "Pending", "100101", 12, 31, "Pueblo,Xtra", "Mayaguez" , 1);
dbAdapter.addOrder("1991", "Pending", "100102", 12, 31, "Pueblo,Xtra", "Mayaguez" , 1);
dbAdapter.addOrder("1993", "Pending", "100103", 12, 31, "Pueblo,Xtra", "Mayaguez" , 1);
dbAdapter.addCustomer("1995", "Robert Soler", "Urb. Villa Mofongo II3", "PR", "Mayaguez", "00617", "787-391-5555", "787-391-5555");
dbAdapter.addCustomer("1991", "Robert Soler", "Urb. Villa Mofongo II3", "PR", "Mayaguez", "00617", "787-391-5555", "787-391-5555");
dbAdapter.addCustomer("1993", "Robert Soler", "Urb. Villa Mofongo II3", "PR", "Mayaguez", "00617", "787-391-5555", "787-391-5555");
dbAdapter.addItem("0101", "Habichuelas Blancas", "1995", "Si es GOYA tiene que ser bueno", 1, "Goya", 0, 2.75, "1001019");
dbAdapter.addItem("0102", "Honey Nut Cheerios", "1995", "Cereal Cheerios", 5, "General Mills", 0, 3.99, "10019876");
dbAdapter.addItem("0111", "Habichuelas Rosadas", "1995", "Si es GOYA tiene que ser bueno", 1, "Goya", 0, 2.75, "1001019");
dbAdapter.addItem("0112", "Arroz Rico", "1995", "Rico", 5, "Bien, Bien Rico", 0, 3.99, "10019876");
dbAdapter.addItem("0121", "Habichuelas Blancas", "1991", "Si es GOYA tiene que ser bueno", 1, "Goya", 0, 2.75, "1001019");
dbAdapter.addItem("0122", "Honey Nut Cheerios", "1991", "Cereal Cheerios", 5, "General Mills", 0, 3.99, "10019876");
dbAdapter.addItem("0131", "Habichuelas Blancas", "1993", "Si es GOYA tiene que ser bueno", 1, "Goya", 0, 2.75, "1001019");
dbAdapter.addItem("0132", "Honey Nut Cheerios", "1993", "Cereal Cheerios", 5, "General Mills", 0, 3.99, "10019876");
}
private class OrderListAdapter extends ArrayAdapter<Order> {
private ArrayList<Order> orders;
private OrderListAdapter(Context context, int textViewResourceId, ArrayList<Order> orders)
{
super(context, textViewResourceId, orders);
this.orders = orders;
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
View v = convertView;
if (v==null)
{
LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.row, null);
}
Order o = orders.get(position);
if(o!=null){
TextView orderNum = (TextView) v.findViewById(R.id.orderNumber);
TextView location = (TextView) v.findViewById(R.id.location);
TextView quantity = (TextView) v.findViewById(R.id.quantity);
TextView status = (TextView) v.findViewById(R.id.status);
if(orderNum!=null){
orderNum.setText("Order " + o.getOrderNumber());
}
if(location!=null)
{
location.setText("Location: "+o.getOrderRetailer());
}
if(quantity!=null){
quantity.setText("Qty: "+o.getTotalQuantity());
}
if(status!=null){
status.setText("Status: "+ o.getStatus());
}
}
return v;
}
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
super.onListItemClick(l, v, position, id);
Intent intent = new Intent("com.orderdelivery.GROCERYITEMLIST");
Bundle bundle = new Bundle();
bundle.putInt("position", position);
}
//Might be needed
public static Order getOrderAtPosition(int pos){
return null;
}
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
// TODO Auto-generated method stub
super.onCreateContextMenu(menu, v, menuInfo);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.item_menu, menu);
}
@Override
public boolean onContextItemSelected(MenuItem item) {
// TODO Auto-generated method stub
//Como obtener el item de la lista a desplegar!!!
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
//test
switch (item.getItemId()){
case R.id.itemMenu_details:
Intent in = new Intent("com.orderdelivery.GROCERYORDERDETAILS");
Bundle bund = new Bundle();
/*
this.listCursorAdapter.getCursor().moveToFirst();
int position = this.listCursorAdapter.getCursor().getColumnIndex(DataAdapter.ITEM_KEY_ORDER_ID);
String orderId = this.listCursorAdapter.getCursor().getString(position);
*/
String orderId = "1995";//this.listCursorAdapter.getCursor().getString(2);
bund.putString("order_id", orderId);
in.putExtras(bund);
startActivity(in);
return super.onContextItemSelected(item);
}
return super.onContextItemSelected(item);
}
private class SimpleOrderAdapter extends SimpleCursorAdapter {
private LayoutInflater inflater;
public SimpleOrderAdapter(Context context, int layout, Cursor c,
String[] from, int[] to) {
super(context, layout, c, from, to);
// TODO Auto-generated constructor stub
}
@Override
public void bindView(View v, Context context, Cursor c) {
// TODO Auto-generated method stub
super.bindView(v, context, c);
TextView orderNum = (TextView) v.findViewById(R.id.orderNumber);
TextView location = (TextView) v.findViewById(R.id.location);
TextView quantity = (TextView) v.findViewById(R.id.quantity);
TextView status = (TextView) v.findViewById(R.id.status);
String orderNumString="Order "+ c.getString(c.getColumnIndex(DataAdapter.ORDER_KEY_ORDER_ID));
String locationString=c.getString(c.getColumnIndex(DataAdapter.ORDER_KEY_RETAILER));
int qty = c.getInt(c.getColumnIndex(DataAdapter.ORDER_KEY_QTY));
String statusString = c.getString(c.getColumnIndex(DataAdapter.ORDER_KEY_STATUS));
orderNum.setText(orderNumString);
location.setText(locationString);
quantity.setText(""+qty); //FIXXX
status.setText(statusString);
}
@Override
public View newView(Context context, Cursor c, ViewGroup parent) {
// TODO Auto-generated method stub
final LayoutInflater inflater = LayoutInflater.from(context);
View v = inflater.inflate(R.layout.row, parent, false);
TextView orderNum = (TextView) v.findViewById(R.id.orderNumber);
TextView location = (TextView) v.findViewById(R.id.location);
TextView quantity = (TextView) v.findViewById(R.id.quantity);
TextView status = (TextView) v.findViewById(R.id.status);
String orderNumString="Order "+ c.getString(c.getColumnIndex(DataAdapter.ORDER_KEY_ORDER_ID));
String locationString="Location: "+c.getString(c.getColumnIndex(DataAdapter.ORDER_KEY_RETAILER));
String qtyString ="Qty: "+ c.getString(c.getColumnIndex(DataAdapter.ORDER_KEY_QTY));
String statusString = c.getString(c.getColumnIndex(DataAdapter.ORDER_KEY_STATUS));
orderNum.setText(orderNumString);
location.setText(locationString);
quantity.setText(qtyString);
status.setText(statusString);
return v;
}
}
}