1
Я попытался создать собственное Listview с пользовательскими представлениями.Listview с пользовательским CursorAdapter и
Медленный список, некоторые повторяющиеся строки, не работают хорошо.
Я не знаю, правилен ли код или нет.
Это мой код:
public class ActDictionary extends Activity
{
private TextView searchBox;
private ListView wordsList;
private Cursor c;
private ActDictionary.CustomA wordListAdapter;
private boolean defaultLanguage;
private String TableIs;
@Override
protected void onCreate(Bundle savedInstanceState)
{
// TODO: Implement this method
super.onCreate(savedInstanceState);
Wind.in(this);
setContentView(R.layout.act_dictionary);
searchBox=(TextView)findViewById(R.id.actdictionaryEditText1);
wordsList=(ListView)findViewById(R.id.act_dictionary_list);
defaultLanguage=true;
((TextView)findViewById(R.id.actdictionaryTextView1)).setOnClickListener(new OnClickListener(){
@Override
public void onClick(View p1)
{
defaultLanguage=!defaultLanguage;
setListData();
}
});
setListData();
}
public void setListData(){
if(defaultLanguage){
((TextView)findViewById(R.id.actdictionaryTextView1)).setText("Franch >> English");
c=Dictionary.in(this).database("dictionary.db").codes().__get_all_rows("fr2en");
c.moveToFirst();
TableIs="fr2en";
loaddata();
}else{
((TextView)findViewById(R.id.actdictionaryTextView1)).setText("English >> Franch");
c=Dictionary.in(this).database("dictionary.db").codes().__get_all_rows("en2fr");
c.moveToFirst();
TableIs="en2fr";
loaddata();
}
//String from[] ={"a","b"};
//int to[]={R.id.dictionaryrawTextView1,R.id.dictionaryrawTextView2};
}
public void loaddata(){
wordListAdapter=new CustomA(this,c,TableIs);//R.layout.dictionary_raw,c,from,to);
wordsList.setAdapter(wordListAdapter);
searchBox.addTextChangedListener(new TextWatcher(){
@Override
public void beforeTextChanged(CharSequence p1, int p2, int p3, int p4)
{
// TODO: Implement this method
}
@Override
public void onTextChanged(CharSequence p1, int p2, int p3, int p4)
{
wordListAdapter.notifyDataSetChanged();
wordListAdapter.getFilter().filter(p1);
}
@Override
public void afterTextChanged(Editable p1)
{
}
});
wordListAdapter.setFilterQueryProvider(new FilterQueryProvider(){
@Override
public Cursor runQuery(CharSequence p1)
{
String str=p1.toString();
if(defaultLanguage){
return Dictionary.in(getBaseContext()).database("dictionary.db").codes().__get_all_rows("fr2en",str+"%");
}
return Dictionary.in(getBaseContext()).database("dictionary.db").codes().__get_all_rows("en2fr",str+"%");
}
});
}
public class CustomA extends CursorAdapter
{
private LayoutInflater inflater;
private Context mctx;
private Cursor mcu;
private boolean starIs;
private String table;
@Override
public View newView(Context p1, Cursor p2, ViewGroup p3)
{
// TODO: Implement this method
return null;
}
@Override
public void bindView(View p1, Context p2, Cursor p3)
{
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
convertView=null;
View v=inflater.inflate(R.layout.dictionary_raw,parent,false);
//LinearLayout l=(LinearLayout)v.findViewById(R.id.dictionaryrawLinearLayout1);
WrapLayout l=(WrapLayout)v.findViewById(R.id.flow_layout);
TextView tv=(TextView)v.findViewById(R.id.dictionaryrawTextView1);
final ImageView img=(ImageView)v.findViewById(R.id.star);
tv.setText(getCursor().getString(getCursor().getColumnIndex("value1")));
String temp=getCursor().getString(getCursor().getColumnIndex("value2"));
final String star=Dictionary.in(getBaseContext()).database("dictionary.db").codes().___getStar(table,getCursor().getString(getCursor().getColumnIndex("_id")));//mcu.getString(getCursor().getColumnIndex("value4"));
String values[]=temp.split(",|\\;");
if(star.equals("0")){
img.setImageResource(R.drawable.ic_star_off);
}else{
img.setImageResource(R.drawable.ic_star_on);
}
String color[]={"#123232","#544344","#774499","#330088","#546789","#223322"};
TextView[] textView=new TextView[values.length];
for (int i=0;i<values.length;i++){
textView[i]=new TextView(getBaseContext());
textView[i].setText(values[i]);
textView[i].setTextColor(Color.WHITE);
textView[i].setBackgroundResource(R.drawable.button);
l.addView(textView[i]);
}
//final ImageView button01 = (ImageView) findViewById(R.id.button01);
final int cc=getCursor().getInt(getCursor().getColumnIndex("_id"));
img.setOnClickListener(new OnClickListener() {
int button01pos = Integer.parseInt(star);
public void onClick(View v) {
if (button01pos == 0) {
img.setImageResource(R.drawable.ic_star_on);
button01pos = 1;
Dictionary.in(getBaseContext()).database("dictionary.db").codes().___upadteValue(table,"1",cc);
Toast.makeText(getBaseContext(),""+cc,1).show();
} else if (button01pos == 1) {
img.setImageResource(R.drawable.ic_star_off);
button01pos = 0;
Dictionary.in(getBaseContext()).database("dictionary.db").codes().___upadteValue(table,"0",cc);
}
}
});
return v;
}
public CustomA(Context ctx,Cursor cu,String c){
super(ctx,cu);
mctx=ctx;
mcu=cu;
table=c;
inflater=LayoutInflater.from(ctx);
}
}
}
И база данных
public class Codes
{
private SQLiteDatabase mDb;
public Codes(SQLiteDatabase db)
{
this.mDb = db;
}
public String ___getStar(String table, String string)
{
Cursor c= mDb.rawQuery("SELECT value4 from "+table+" WHERE _id = ? ",new String[]{string});
c.moveToFirst();
return c.getString(c.getColumnIndex("value4"));
}
public Cursor __get_all_rows_star(String table){
return mDb.rawQuery("select * from "+table+" where value4 ='1'",null);
}
public Cursor __get_all_rows_star(String tableName,String args){
return mDb.rawQuery("select * from "+tableName+" where value1 like ? ORDER BY value1 COLLATE NOCASE LIMIT 50",new String[]{args});
}
public void ___upadteValue(String table, String value,int id){
mDb.execSQL("UPDATE "+table+" SET value4 = '"+value+"' WHERE _id = "+id);
System.err.println("UPDATE "+table+" SET value4 = "+value+" WHERE _id = "+id);
}
public Cursor __get_all_rows(String tableName){
return mDb.rawQuery("select * from "+tableName+" ORDER BY RANDOM() LIMIT 100",null);
}
public Cursor __get_all_rows(String tableName,String args){
return mDb.rawQuery("select * from "+tableName+" where value1 like ? ORDER BY value1 COLLATE NOCASE LIMIT 50",new String[]{args});
}
public Cursor test(){
return mDb.rawQuery("select * from fr2en where value4= ?",new String[]{"1"});
}
}
просто использовать 'SimpleCursorAdapter' (или' ResourceCursorAdapter'), нет необходимости в обычае 'CursorAdapter', если вы не знаете, как' вид reusing' работает – pskink
Я попытался использовать SimpleCursorAdapter, но проблема при использовании этой части TextView [] textView = новый TextView [values.length]; for (int i = 0; i
user2402497