我遇到了ArrayAdapter的问题。
我在TabView中的两个片段中列出了两个元素列表。
Code of the class:
public class AdapterListaInCasa extends ArrayAdapter<String> {
private int layout_resource;
private List<String> str;
private Context mContext;
public AdapterListaInCasa(Context context, int textViewResourceId){
super(context, textViewResourceId);
// TODO Auto-generated constructor stub
}
public AdapterListaInCasa(Context context, int layout_resource,List<String> str) {
super(context, layout_resource, str);
this.mContext = context;
this.str = str;
this.layout_resource = layout_resource;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final int mPosition = position;
LayoutInflater vi;
vi = LayoutInflater.from(getContext());
if (convertView == null) {
convertView = vi.inflate(layout_resource, null);
}
TextView nomeElemento = (TextView)convertView.findViewById(R.id.nomeElemento);
nomeElemento.setText(str.get(mPosition));
ImageButton btnTerminato = (ImageButton) convertView.findViewById(R.id.terminati);
btnTerminato.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) { // bottone elimina
SharedPreferences preferences;
preferences = PreferenceManager.getDefaultSharedPreferences(getContext());
SharedPreferences.Editor editor = preferences.edit();
String lista = "inCasa"+mPosition;
editor.remove(lista);
str.remove(mPosition);
notifyDataSetChanged();
int i=0;
String TAG = "daComprare"+i;
String eliminati = preferences.getString(TAG, "nonTrovato");
while (eliminati != "nonTrovato"){
i++;
TAG = "daComprare"+i;
eliminati = preferences.getString(TAG, "nonTrovato");
}
editor.putString(TAG, str.get(mPosition));
editor.commit();
}
});
ImageButton btnNNmiPiace = (ImageButton) convertView.findViewById(R.id.nonMiPiace);
btnNNmiPiace.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) { // bottone elimina
SharedPreferences preferences;
preferences = PreferenceManager.getDefaultSharedPreferences(getContext());
SharedPreferences.Editor editor = preferences.edit();
String lista = "inCasa"+mPosition;
editor.remove(lista);
str.remove(mPosition);
notifyDataSetChanged();
int i=0;
String TAG = "nonMiPiace"+i;
String nonMiPiace = preferences.getString(TAG, "nonTrovato");
while (nonMiPiace != "nonTrovato"){
i++;
TAG = "nonMiPiace"+i;
nonMiPiace = preferences.getString(TAG, "nonTrovato");
}
editor.putString(TAG, str.get(mPosition));
editor.commit();
}
});
return convertView;
}
}
Code of the xml:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_centerVertical="true"
android:layout_marginLeft="20dp"
android:id="@+id/nomeElemento"/>
<ImageButton
android:id="@+id/terminati"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_margin="10dp"
android:src="@drawable/ic_carrello" />
<ImageButton
android:id="@+id/nonMiPiace"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_margin="10dp"
android:layout_toLeftOf="@+id/terminati"
android:layout_toStartOf="@+id/terminati"
android:src="@drawable/ic_carrello" />
</RelativeLayout>
问题是当我尝试使用Button btnTerminato和
时btnTerminato.setOnClickListener(new View.OnClickListener() {...
我发现一个致命错误,说该变量为null:
FATAL EXCEPTION: main
Process: com.giulio.marketrack, PID: 24457
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageButton.setMaxWidth(int)' on a null object reference
at com.giulio.marketrack.AdapterListaInCasa.getView(AdapterListaInCasa.java:65)
奇怪的是,唯一的问题可能是ID的引用,但是,你怎么看,ID是正确的。
有人看到失败吗?
提前很多!
答案 0 :(得分:0)
if (convertView == null) {
convertView = = inflater.inflate(R.layout.yourlayout name, null);
}