我看了很多类似的问题,似乎无法发挥作用.我有一个带有这样功能的主类,编辑显示一个对话框,然后在按下按钮时编辑一个List.
public class EditPlayers extends SherlockFragmentActivity {
listPlayerNames.setAdapter(new EditPlayerAdapter(ctx,
R.layout.score_row_edit_player, listScoreEdit));
public void deletePlayer(final int position) {
AlertDialog.Builder alertDialog = new AlertDialog.Builder(
EditPlayers.this);
// Setting Dialog Title
alertDialog.setTitle("Delete Player");
// Setting Dialog Message
alertDialog.setMessage("Are you sure?");
// Setting Delete Button
alertDialog.setPositiveButton("Delete",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
listScoreEdit.remove(position);
updateListView();
}
});
// Setting Cancel Button
alertDialog.setNeutralButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
// Showing Alert Message
alertDialog.show();
}
} …Run Code Online (Sandbox Code Playgroud) 现在我使用setAdapter更新我的ListView,但我认为正确的方法是使用notifiyDatasetChanged(),我不能让它在我的主类(它在适配器中)工作.这是错误:
对于ListAdapter类型,方法notifyDatasetChanged()未定义
我猜这有更好的方法 - 有人能指出我正确的方向吗?
这是我的代码的相关部分:
public class ScoreList extends SherlockFragmentActivity {
private ListView listViewScore;
static List<Score> listScore = new ArrayList<Score>();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.score_list);
ctx = this;
listScore = dbh.getAllScores();
listViewScore = (ListView) findViewById(R.id.score_list);
listViewScore.setAdapter(new ScoreListAdapter(ctx,
R.layout.score_row_item, listScore));
listViewScore.getAdapter().notifyDatasetChanged(); //this is where I get the error
}
}
Run Code Online (Sandbox Code Playgroud)
这是适配器:
public class ScoreListAdapter extends ArrayAdapter<Score> {
private int resource;
private LayoutInflater inflater;
public ScoreListAdapter(Context ctx, int resourceId, List<Score> objects) {
super(ctx, resourceId, objects);
resource = resourceId; …Run Code Online (Sandbox Code Playgroud) 我试图让我的应用程序在2.3中正常工作(它在4.0+中运行正常)和我遇到的一个问题是在我的listview上我无法获得所选项目的背景更改.我不确定我需要改变什么 - 有谁知道吗?
这是listview本身:
<ListView
android:id="@+id/score_list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="@+id/keyboard"
android:layout_below="@+id/sort_header"
android:choiceMode="singleChoice"
android:divider="#CCCCCC"
android:dividerHeight="1dp"
android:listSelector="@drawable/selector" />
Run Code Online (Sandbox Code Playgroud)
这是在4.0+中工作的选择器(在2.3中没有颜色变化):
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:drawable="@color/highlight"/>
<item android:state_pressed="true" android:drawable="@color/highlight"/>
<item android:state_activated="true" android:drawable="@color/highlight"/>
<item android:state_selected="true" android:drawable="@color/highlight"/>
</selector>
Run Code Online (Sandbox Code Playgroud)
我实际上并不需要所有4个,但我想尝试一切.
如何将selectedPlayer设置为其他类的位置值?
我在这里已经阅读了一些类似的问题,但是我一直与我的变量和函数发生冲突.
ScoreList.java
public class ScoreList extends Activity {
Integer selectedPlayer = 0;
}
Run Code Online (Sandbox Code Playgroud)
ScoreListAdapter.java
public class ScoreListAdapter extends ArrayAdapter<Score> {
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final OnClickListener lsScoreView = new OnClickListener() {
@Override
public void onClick(View v) {
//send position to main class here
}
};
}
}
Run Code Online (Sandbox Code Playgroud) 我使用这个SO问题作为指导,但是当我点击一行时它不会保持突出显示.我的代码出了什么问题?
score_list.xml
<?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" >
<ListView
android:id="@+id/score_list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="@+id/keyboard"
android:listSelector="@drawable/selector"
android:choiceMode="singleChoice"
android:divider="#CCCCCC"
android:dividerHeight="1dp"
android:paddingLeft="15dp"
android:paddingRight="15dp" />
...
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
绘制/ selector.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:drawable="@color/highlight"/>
<item android:state_pressed="true" android:drawable="@color/highlight"/>
</selector>
Run Code Online (Sandbox Code Playgroud)
score_row_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/scoreRowLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:background="@drawable/row_selector" >
...
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
绘制/ row_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true" android:drawable="@color/highlight" />
<item android:state_activated="true" android:drawable="@color/highlight" />
<item android:drawable="@color/transparent" />
</selector>
Run Code Online (Sandbox Code Playgroud)
此代码现在可以正常运行.