正在为自定义ListView设置自定义适配器



我目前正在进行一个项目,该项目将跟踪名为Farkle的游戏的玩家分数和其他信息。我在谷歌上搜索了大约100次,也阅读了这里的一些条目。我已经试着实现和使用我所发现的一切,但我在这里已经到了极限。

行.xml将作为单个项加载到ListView中。

最终,我想按顺序排列(您将在下面看到我的row.xml布局):"球员姓名"复选框"球员得分"。

我可以让应用程序运行,所以我在发布时没有问题,但是,一旦我尝试添加新玩家,应用程序就会崩溃。

现在,我下面的代码是一个新项目,因为上一个项目已经崩溃到没有错误,但在发布时会崩溃。我尝试过使用自定义的ArrayAdapter,我相信这是一种适合我的方法,但我似乎无法让它在不崩溃的情况下加载row.xml。我还是ArrayAdapter的新手,我已经做了一些教程,但我仍然无法让它发挥作用。

如果有人对我能做些什么来实现这一目标有任何想法,请帮助我,让我朝着正确的方向前进。我已经做了两个星期了!提前谢谢。

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="@+id/btnSetScore"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="@string/winning_score" />
<Button
android:id="@+id/btnAddPlayer"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/add_player"
android:textSize="@dimen/activity_horizontal_margin" />
<Button
android:id="@+id/btnSetPenalty"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="@string/set_penalty"
android:textSize="@dimen/activity_horizontal_margin" />
</LinearLayout>
<ListView
android:id="@+id/lvScoreBoard"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="10"
tools:listitem="@layout/row" >
</ListView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="@+id/btnStatistics"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="@string/statistics" />
<Button
android:id="@+id/btnNewGame"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="@string/new_game" />
</LinearLayout>

这是我的行.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<TextView
android:id="@+id/tvPlayer"
android:layout_weight="4"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="@string/player"
android:textAppearance="?android:attr/textAppearanceLarge" />
<CheckBox
android:id="@+id/cbFarkle1"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:enabled="false"
android:focusable="false"
android:text="" />
<CheckBox
android:id="@+id/cbFarkle2"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:enabled="false"
android:focusable="false"
android:text="" />
<TextView
android:id="@+id/tvScore"
android:layout_weight="2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="@string/player_score"
android:gravity="right"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>

这是我的MainActivity.java

public class MainActivity extends Activity{
String winningScore = "";
String playerName = "";
String playerScore = "";
String farklePenalty = "";
int penalty;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Creates Buttons
final Button btnSetScore = (Button) findViewById(R.id.btnSetScore);
Button btnAddPlayer = (Button) findViewById(R.id.btnAddPlayer);
final Button btnSetPenalty = (Button) findViewById(R.id.btnSetPenalty);
Button btnStatistics = (Button) findViewById(R.id.btnStatistics);
Button btnNewGame = (Button) findViewById(R.id.btnNewGame);
// Creates ListView for player score board
final ListView lvScoreBoard = (ListView) findViewById(R.id.lvScoreBoard);
// Sets the game winning score when the Set Score button is pressed.
btnSetScore.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO:
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("Set Winning Score");
final EditText input = new EditText(MainActivity.this);
input.setInputType(InputType.TYPE_CLASS_NUMBER);
builder.setView(input);
builder.setPositiveButton("Save", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
winningScore = input.getText().toString();
btnSetScore.setText(winningScore);
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.HIDE_NOT_ALWAYS, 0);
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {    
dialog.cancel();        
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.HIDE_NOT_ALWAYS, 0);
}
});
builder.show();
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
}
});
// Adds a new player to the game when Add Player button is pressed.
btnAddPlayer.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("Add Player");
final EditText input = new EditText(MainActivity.this);
input.setInputType(InputType.TYPE_CLASS_TEXT);
builder.setView(input);
builder.setPositiveButton("Save", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
playerName = input.getText().toString();

InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.HIDE_NOT_ALWAYS, 0);
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {    
dialog.cancel();        
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.HIDE_NOT_ALWAYS, 0);
}
});
builder.show();
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
}
});
// Sets the third farkle penalty when Set Penalty button is pressed.
btnSetPenalty.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("Set Farkle Penalty");
final EditText input = new EditText(MainActivity.this);
input.setInputType(InputType.TYPE_CLASS_NUMBER);
builder.setView(input);
builder.setPositiveButton("Save", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
farklePenalty = input.getText().toString();
penalty = Integer.parseInt(input.getText().toString());
btnSetPenalty.setText(farklePenalty);
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.HIDE_NOT_ALWAYS, 0);
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {    
dialog.cancel();        
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.HIDE_NOT_ALWAYS, 0);
}
});
builder.show();
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
}
});
// Changes to statistics activity when Statistics button is pressed.
btnStatistics.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
// Resets all data when New Game button is pressed.
// This will also have the ability to keep player names, 
// but erase farkle counts, player scores, penalty, 
// and winning score.
btnNewGame.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}

自定义ListAdapter,但我知道它是错误的。

public class GeneralAdapter extends BaseAdapter {
private static final CharSequence Name = null;
private LayoutInflater mInflater = null;
private ArrayList<String> info = null;
public GeneralAdapter( ArrayList<String> info) {
this.info = info;
}
String name;
String score = "0";
boolean farkle1 = false;
boolean farkle2 = false;
@Override
public int getCount() {
return info.size();
}
@Override
public Object getItem(int position) {
return info.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.row, null);
holder = new ViewHolder();
holder.generalTV = (TextView) convertView.findViewById(R.id.tvPlayer);
holder.generalCB = (CheckBox) convertView.findViewById(R.id.cbFarkle1);
holder.generalCB2 = (CheckBox) convertView.findViewById(R.id.cbFarkle2);
holder.generalTV2 = (TextView) convertView.findViewById(R.id.tvScore);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.generalTV.setText(name);
holder.generalCB.setChecked(farkle1);
holder.generalCB2.setChecked(farkle2);
holder.generalTV2.setText(score);
return null;
}
private class ViewHolder {
TextView generalTV;
TextView generalTV2;
CheckBox generalCB;
CheckBox generalCB2;
}

}

崩溃日志或至少您遇到的异常会有所帮助。

也就是说,我可以看到一个问题。您从getView()返回null。尝试返回convertView:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.row, null);
holder = new ViewHolder();
holder.generalTV = (TextView) convertView.findViewById(R.id.tvPlayer);
holder.generalCB = (CheckBox) convertView.findViewById(R.id.cbFarkle1);
holder.generalCB2 = (CheckBox) convertView.findViewById(R.id.cbFarkle2);
holder.generalTV2 = (TextView) convertView.findViewById(R.id.tvScore);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.generalTV.setText(name);
holder.generalCB.setChecked(farkle1);
holder.generalCB2.setChecked(farkle2);
holder.generalTV2.setText(score);
return convertView;  // HERE!!
}

此外,当您从convertView获取holder时,您可能需要进行一些null检查。如果由于某种原因,它最终为空,那么从中引用的第一个东西将导致NRE。我看不出你的代码有什么错误,但由于你遇到了崩溃,所以值得一试。


退一步说,您永远不会使用适配器中的信息。换句话说,每个列表项看起来都完全相同,因为它们使用了相同的值(name、farkle1、farkle2、score)。

也许你的意思是有一个对象数组,上面有这些成员?类似的东西:

public class DataItem {
String name;
String score = "0";
boolean farkle1 = false;
boolean farkle2 = false;
}

然后将其传递到适配器中,类似于。。(注意:我还没有尝试编译)

public class GeneralAdapter extends BaseAdapter {
private static final CharSequence Name = null;
private LayoutInflater mInflater = null;
private ArrayList<DataItem> info = null;
public GeneralAdapter( ArrayList<DataItem> info) {
this.info = info;
}
@Override
public int getCount() {
return info.size();
}
@Override
public Object getItem(int position) {
return info.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
DataItem dataItem = getItem(position);
if (convertView == null) {
convertView = mInflater.inflate(R.layout.row, null);
holder = new ViewHolder();
holder.generalTV = (TextView) convertView.findViewById(R.id.tvPlayer);
holder.generalCB = (CheckBox) convertView.findViewById(R.id.cbFarkle1);
holder.generalCB2 = (CheckBox) convertView.findViewById(R.id.cbFarkle2);
holder.generalTV2 = (TextView) convertView.findViewById(R.id.tvScore);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.generalTV.setText(dataItem.name);
holder.generalCB.setChecked(dataItem.farkle1);
holder.generalCB2.setChecked(dataItem.farkle2);
holder.generalTV2.setText(dataItem.score);
return convertView;
}
private class ViewHolder {
TextView generalTV;
TextView generalTV2;
CheckBox generalCB;
CheckBox generalCB2;
}
}

顺便说一句,这基本上是一个ArrayAdapter,您可以很容易地使用它。

为什么在getView中返回null

更改为return convertView;

最新更新