我想做一件非常简单的事情.我在我的应用程序中有一个listview,我动态添加文本.但是,在某一点之后,我想改变listview中文本的颜色.因此,我创建了一个定义自定义列表项的XML,并将ArrayAdapter子类化.但是,每当我在自定义ArrayAdapter上调用add()方法时,项目就会添加到列表视图中,但文本不会放入其中.
这是我的XML:`
<TextView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/list_content" android:textSize="8pt"
android:gravity="center" android:layout_margin="4dip"
android:layout_width="fill_parent" android:layout_height="wrap_content" android:textColor="#FF00FF00"/>
Run Code Online (Sandbox Code Playgroud)
而我的ArrayAdapter子类:
private class customAdapter extends ArrayAdapter<String> {
public View v;
public customAdapter(Context context){
super(context, R.layout.gamelistitem);
}
@Override
public View getView(int pos, View convertView, ViewGroup parent){
this.v = convertView;
if(v==null) {
LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v=vi.inflate(R.layout.gamelistitem, null);
}
if(timeLeft!=0) {
TextView tv = (TextView)v.findViewById(R.id.list_content);
//tv.setText(str[pos]);
tv.setTextColor(Color.GREEN);
}
else {
TextView tv = (TextView)v.findViewById(R.id.list_content);
//tv.setText(str[pos]);
tv.setTextColor(Color.RED);
}
return v;
}
}
Run Code Online (Sandbox Code Playgroud)
我确定我做的事情非常糟糕,但我对Android仍然有点新鲜.
谢谢!`
这是什么意思?我一直在与之斗争ListView,我使用过ArrayAdapter.我写了这个很好的对话框片段,我已经调用了一个updateUI列表器,因为我想要更新这个ListView我在我的片段DialogFragment中起初ArrayAdapter是一个复杂类型的我创建的:
ArrayAdapter<Location> theLocations;
...
//in oncreateview
theLocations = new ArrayAdapter<Location>(mContext, R.layout.location_row, locations);
//here locations is an ArrayList<Location>
Run Code Online (Sandbox Code Playgroud)
然后我有:
public void onUIUpdate(Location l) { //called from dialog fragment
locations.add(l);
theLocations.notifyDataSetChanged();
}
Run Code Online (Sandbox Code Playgroud)
然后,这给出了上述错误,所以我切换它只使用一个
String[] locationNames = new String[sizeofLocations];
theLocations=new ArrayAdapter<String>(mContext, R.layout.location_Row, locationNames );
public void onUIUpdate(Location l) {
locations.add(l);
locationNames = new String[locations.size()];
for(locations.size() etc...) {
locationNames [i] = new String(locations.get(i).getName());
}
theLocations.notifyDataSetChanged();
}
Run Code Online (Sandbox Code Playgroud)
这在我的更新方法中没有错误(更新了ui)但它没有更新任何东西.所以我迷失了如何更新这个ArrayAdapter,我认为notifyChange应该这样做,但它要么什么都不做,要么抛出上面的错误.
我的程序中的其他地方的SimpleCursorAdapters没有问题(只要我的游标保持打开状态,我就会对它们进行重新查询).
我对错误的看法有何见解?
根据要求,这里是R.layout.location_row的布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout …Run Code Online (Sandbox Code Playgroud) 开箱即用,AutoCompleteTextView小部件似乎无法匹配列表值中间的输入字符串 - 匹配始终在开头; 例如,输入" ar"匹配" argentina",但不输入" hungary".
如何搜索单词中间的文本?谁能给我一个想法?
提前致谢 !
我正在做一个项目.它只是显示任务列表并向其添加新任务.我有3堂课.一个用于添加,一个用于查看,一个用于保存所有信息(或者我认为).
我的列表中已经有2个任务,并且显示正确.
问题是,当我添加一个新任务时,它不会在视图中显示它们.我尝试了很多可能的解决方案:
只需将项目添加到列表中
创建一个包含旧旧项目和重建适配器的新列表;
使用notifyDataSetChanged();沿着与附加()命令;
等等
这是我的代码,它有点乱,但我希望你能弄明白.
AndroidListAdapterActivity类:
public class AndroidListAdapterActivity extends ListActivity {
/** Called when the activity is first created. */
Button b1;
Lista o;
ArrayAdapter aa;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
b1=(Button)findViewById(R.id.add);
Log.w("POC", "PA OVO SE ZOVE SVAKI PUT");
o=new Lista();
o.lis.add("S1");
o.lis.add("S2");
aa = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, o.lis);
setListAdapter(aa);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(AndroidListAdapterActivity.this, Dodavanje.class);
startActivity(i);
}
});
} …Run Code Online (Sandbox Code Playgroud) 我有一个名为InteractiveArrayAdapter的自定义ArrayAdapter,它为列表视图中的每个项添加按钮和按钮侦听器.在适配器内部有一个getView方法,可以创建一个视图充气器.在这里是创建我的按钮并创建buttonListener的地方.单击该按钮时,我删除与该按钮关联的ArrayList中的元素.问题是我无法弄清楚如何从这个OnClick方法中调用notifyDataSetChange,或者通知适配器需要更新listView的另一种方法.
定制适配器:
public class InteractiveArrayAdapter extends ArrayAdapter<String> {
private final List<String> list;
private final Activity context;
private ListView listV;
public InteractiveArrayAdapter(Activity context, List<String> list) {
super(context, R.layout.rowbuttonlayout, list);
this.context = context;
this.list = list;
}
static class ViewHolder {
protected TextView text;
protected Button button;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = null;
if (convertView == null) {
LayoutInflater inflator = context.getLayoutInflater();
view = inflator.inflate(R.layout.rowbuttonlayout, null);
final ViewHolder viewHolder = new ViewHolder();
viewHolder.text …Run Code Online (Sandbox Code Playgroud) 我有一个应用程序,我想通过SQLite数据库中的数据填充ListView ...在这部分我有一个问题,与arrayAdapter ...
这是我填充listView的方法代码:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list);
PetListView = (ListView)findViewById(R.id.list);
MyDatabaseHelper db = new MyDatabaseHelper(this);
String [] items = new String[100];
List<Pet> pets = db.getAllPets();
for (int i = 0; i < 10; i++) {
items[i] = pets.get(i).getName();
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.activity_list_item, R.id.textView1list, items);
PetListView.setAdapter(adapter);
}
Run Code Online (Sandbox Code Playgroud)
这是在我的数据库助手中实现的方法:
public List<Pet> getAllPets() {
List<Pet> petList = new ArrayList<Pet>();
String selectQuery = "SELECT * FROM " + TABLE_PETS;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = …Run Code Online (Sandbox Code Playgroud) 
我有一个listView,通过ArrayAdapter填充小xml子视图.每个小视图里面只有两个东西,一个复选框和一个旁边的字符串标签.
我想设置一个onCheckedChanged监听器来捕获用户检查或取消选中复选框的事件.
例如这里显示的听众:
listView.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
Toast.makeText(this, "box has been checked", Toast.LENGTH_SHORT).show();
}
Run Code Online (Sandbox Code Playgroud)
}
我在哪里放置监听器代码?以及如何设置?
ArrayAdapter的代码:
public class MobileArrayAdapter extends ArrayAdapter<CheckBoxInfo>{
CheckBoxInfo[] objects;
Context context;
int textViewResourceId;
public MobileArrayAdapter(Context context, int textViewResourceId,
CheckBoxInfo[] objects) {
super(context, textViewResourceId, objects);
this.context = context;
this.textViewResourceId = textViewResourceId;
this.objects = objects;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row_layout_view = convertView;
if ((row_layout_view == null)){
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row_layout_view = …Run Code Online (Sandbox Code Playgroud) android listener adapter oncheckedchanged android-arrayadapter
我有一个列表视图,有多个textview像这样:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="16dip"
android:textColor="#000000"
android:paddingLeft="10dip"
android:textStyle="bold"/>
<TextView
android:id="@+id/address"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="16dip"
android:textColor="#000000"
android:paddingTop="15dip"
android:paddingBottom="15dip"
android:paddingLeft="10dip"
android:textStyle="bold"/>
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
我有一个POJO列表,它有一个name和address我希望列表视图中的每个项目都填充这些值.
我的POJO是这样的:
public class Person {
private String name;
private String address;
//getter setter
public String toString() {return name;}
}
Run Code Online (Sandbox Code Playgroud)
题
当我使用我的列表设置列表适配器时,如何设置名称和地址?
目前我这样做只设置名称:
setListAdapter(new ArrayAdapter<Person>(MyActivity.this, R.layout.list_text, R.id.name, personList));
Run Code Online (Sandbox Code Playgroud) 我的关键问题是做lv_apps.setAdapter(_adapter)TWICE是我的应用程序崩溃的原因.(基本上,当我第二次调用populateListView_trial()时,应用程序崩溃 - 并且违规行是lv_apps.setAdapter(_adapter)
Stacktrace位于函数之下
private void populateListView_trial() {
blockedApps = loadArrayBlockedApps();
if(isTimerRunning()) {
blockedApps = loadArrayBlockedApps();
temp_blockedList = new ArrayAdapter<String>(
MainActivityCircularSeekbar.this,
android.R.layout.simple_list_item_1, blockedApps);
} else {
holder = new ArrayList<Datamodel>();
for(Map.Entry<String, String> entry : list_installedApps.entrySet()) {
Datamodel _appdata = new Datamodel();
_appdata.setAppname(entry.getKey());
_appdata.setSelected(true);
try {
_appdata.setAppIcon(getIconFromPackageName(entry.getValue(), this));
} catch (Exception e) {
e.printStackTrace();
} catch (OutOfMemoryError e) {
e.printStackTrace();
}
holder.add(_appdata);
}
_adapter = new MyAdapter(getApplicationContext(), holder);
try {
lv_apps.setAdapter(_adapter);
} catch (Exception e) …Run Code Online (Sandbox Code Playgroud) android listview android-arrayadapter android-listview baseadapter
我仍然在玩我的日历,我已经几乎设法将https://github.com/SundeepK/CompactCalendarView集成到我的一个片段中.只剩下一个错误,我做了一些研究,其他人也遇到了问题,例如使用ArrayList <>.
示例代码:
final ArrayAdapter adapter = new ArrayAdapter<>
(this,android.R.layout.simple_list_item_1, mutableBookings);
Run Code Online (Sandbox Code Playgroud)
IDE说:
Error:(87, 38) error: cannot infer type arguments for ArrayAdapter<>
Run Code Online (Sandbox Code Playgroud)
注意:C:...\Uebersicht.java使用或覆盖已弃用的API.注意:使用-Xlint重新编译:弃用以获取详细信息.
我已经尝试重新编译,但结果似乎不起作用
Uebersicht.java:87: error: cannot find symbol
final ArrayAdapter adapter =
new ArrayAdapter<>(this,android.R.layout.simple_list_item_1, mutableBookings);
Uebersicht.java:87: error: cannot find symbol
final ArrayAdapter adapter =
new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, mutableBookings);
Uebersicht.java:87: error: package android.R does not exist
final ArrayAdapter adapter =
new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, mutableBookings);
Run Code Online (Sandbox Code Playgroud)
如果有必要,我也可以发布我的完整Fragment类,它必须是我的API和ArrayAdapter正在使用的API吗?不要忘记我只是一个初学者,我试图自己做一些事情.
android ×10
listview ×4
java ×2
adapter ×1
baseadapter ×1
filtering ×1
listactivity ×1
listener ×1
sqlite ×1