在我使用SherlockActivity
与我不同的方法之前,我将所有治疗都包括在内并且走路正确SherlockFragmentActivity
。DrawerMenu
但我决定改变SherlockActivity
以SherlockFragment
从DrawerMenu
所有观点中受益。我尝试了第一次Activity
,Fragment
它出现了,但包含数据库数据的列表没有出现。请帮帮我。
我的适配器类
public class ListAdapterAnSco extends BaseAdapter {
// Declare Variables
Context mContext;
LayoutInflater inflater;
private List<AnSco> listAnsco = null;
private ArrayList<AnSco> arraylist;
ListAnSco anscoList;
public ListAdapterAnSco(Context context, List<AnSco> listAnsco) {
mContext = context;
this.listAnsco = listAnsco;
inflater = LayoutInflater.from(mContext);
this.arraylist = new ArrayList<AnSco>();
this.arraylist.addAll(listAnsco);
}
public class ViewHolder {
TextView an;
}
@Override
public int getCount() {
return listAnsco.size();
}
@Override
public AnSco getItem(int position) {
return listAnsco.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View view, ViewGroup parent) {
final ViewHolder holder;
if (view == null) {
holder = new ViewHolder();
view = inflater.inflate(R.layout.annee_sco_item, null);
// Locate the TextViews in listview_item.xml
holder.an = (TextView) view.findViewById(R.id.an_item);
view.setTag(holder);
} else {
holder = (ViewHolder) view.getTag();
}
// Set the results into TextViews
holder.an.setText(listAnsco.get(position).getAnsco());
view.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// Send single item click data to SingleItemView Class
Intent intent = new Intent(mContext, ListPeriode.class);
// Pass all data rank
intent.putExtra("ANSCO_ID",(listAnsco.get(position).getIdan()));
// Pass all data country
intent.putExtra("ANSCO_AN",(listAnsco.get(position).getAnsco()));
// Start SingleItemView Class
mContext.startActivity(intent);
}
});
view.setOnLongClickListener(new OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
int idAn = (listAnsco.get(position).getIdan());
String annee = (listAnsco.get(position).getAnsco());
AnSco ansco=new AnSco(idAn,annee);
AlertDialog diag= Alerts.ShowEditDialogAnSco(mContext,ansco);
diag.setOnDismissListener(new OnDismissListener() {
@Override
public void onDismiss(DialogInterface dialog) {
// TODO Auto-generated method stub
// Intent intent = new Intent(mContext, ListAnSco.class);
// mContext.startActivity(intent);
notifyDataSetChanged();
}
});
diag.show();
return false;
}
});
return view;
}
// Filter Class
public void filter(String charText) {
charText = charText.toLowerCase(Locale.getDefault());
listAnsco.clear();
if (charText.length() == 0) {
listAnsco.addAll(arraylist);
}
else
{
for (AnSco wp : arraylist)
{
if (wp.getAnsco().toLowerCase(Locale.getDefault()).contains(charText))
{
listAnsco.add(wp);
}
}
}
notifyDataSetChanged();
}
}
我的新片段
public class ListAnSco extends SherlockFragment {
protected EditText searchText;
DatabaseHelper dbhelper;
protected Cursor cursor;
ListAdapterAnSco adapter;
private static ListView liste;
TextView ansco;
String[] from;
int to[];
Context mcont;
ImageButton btnSearch, btnGo, btnAdd;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.annee_sco_list, container, false);
// setContentView(R.layout.annee_sco_list);
// setTitle(R.string.titre_list_ansco);
searchText = (EditText) v.findViewById(R.id.searchTextAn);
liste = (ListView) v.findViewById(R.id.list_ansco);
ansco = (TextView) v.findViewById(R.id.label_ansco);
btnSearch = (ImageButton) v.findViewById(R.id.btnSearchAn);
btnGo = (ImageButton) v.findViewById(R.id.btnGoAn);
btnAdd = (ImageButton) v.findViewById(R.id.btnAddAn);
mcont = getActivity();
setInvisible();
try {
LoadListAnsco();
} catch (Exception e) {
Toast.makeText(mcont, "Impossible d'afficher \n" + e.toString(),
Toast.LENGTH_SHORT).show();
}
btnAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(mcont, AddAnSco.class);
startActivity(intent);
}
});
btnGo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
try {
LoadListAnsco();
} catch (Exception e) {
Toast.makeText(mcont, "Impossible d'afficher \n" + e.toString(),
Toast.LENGTH_SHORT).show();
}
}
});
// Locate the EditText in menu.xml
// Capture Text in EditText
searchText.addTextChangedListener(textWatcher);
btnSearch.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
setVisible();
}
});
return v;
}
// EditText TextWatcher
private TextWatcher textWatcher = new TextWatcher() {
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
String text = searchText.getText().toString()
.toLowerCase(Locale.getDefault());
adapter.filter(text);
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub
}
@Override
public void onTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub
}
};
public void LoadListAnsco() {
dbhelper = new DatabaseHelper(mcont);
boolean ok = true;
try {
adapter = new ListAdapterAnSco(mcont, dbhelper.getLabelsAnSco());
liste.setAdapter(adapter);
liste.setTextFilterEnabled(true);
Log.i("ListAnsco 3", "OK");
} catch (Exception ex) {
ok = false;
AlertDialog.Builder b = new AlertDialog.Builder(mcont);
b.setMessage(ex.toString());
b.show();
} finally {
if (ok) {
dbhelper.close();
}
}
}
private void setVisible() {
// TODO Auto-generated method stub
searchText.setVisibility(View.VISIBLE);
// Focus on EditText
searchText.requestFocus();
// Force the keyboard to show on EditText focus
InputMethodManager imm = (InputMethodManager) mcont
.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
}
private void setInvisible() {
// TODO Auto-generated method stub
searchText.setText("");
searchText.clearFocus();
searchText.setVisibility(View.GONE);
}
}
我的数据库助手的一部分
public List < AnSco> getLabelsAnSco(){
List < AnSco > lansco = new ArrayList < AnSco > ();
// Select All Query
String selectQuery = "SELECT * FROM "+ TABLE_ANSCO+ " order by " + ANSCO;// tAnsco is your table name?
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if ( cursor.moveToFirst () ) {
do {
lansco.add ( new AnSco ( cursor.getInt(0) , cursor.getString(1) ) );
} while (cursor.moveToNext());
}
// closing connection
cursor.close();
db.close();
// returning labels
return lansco;
}
anne_sco_item.wml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:padding="20px" >
<TextView
android:id="@+id/label_ansco"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/ansco"
android:textColor="@color/font_color"
android:textStyle="bold" />
<TextView
android:id="@+id/an_item"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/label_ansco"
android:layout_marginLeft="35dp"
android:textColor="@color/font_color" />
</RelativeLayout>
annee_sco_list
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:paddingTop="4px"
android:background="@drawable/background1" >
<include
android:id="@+id/menuAn"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
layout="@layout/menu_an" />
<ListView
android:id="@+id/list_ansco"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:drawSelectorOnTop="false"
android:dividerHeight="4dip"
android:divider="@drawable/list_divider_bl"
android:footerDividersEnabled="false"
android:layout_below="@id/menuAn"
/>
</RelativeLayout>