私は非常に基本的なことをやっていて、edittextを使ってリストビューのコードを書くだけで、ここでは2行のレイアウトを使用しました。私のアプリがその時間に最初に実行されるとき、編集テキストはアクティブ化されません。編集テキストをクリックするとアクティブになり、再度変更を加えると非アクティブになります。しかし問題は、edittextにクリックリスナーを設定すると適切な位置になりますが、そのデータを変更しようとするとnullポインター例外が発生することです。`パッケージ
com.android.bank.adapter;
import java.util.ArrayList;
import java.util.TreeSet;
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.android.bank.R;
import com.android.bank.model.AccountData;
public class AccountListViewAdapter extends BaseAdapter
{
private Activity context;
private ArrayList<AccountData> acountData = new ArrayList<AccountData>();
private static final int TYPE_ITEM = 0;
private static final int TYPE_MORE_RECORD = 1;
private static final int TYPE_MAX_COUNT = TYPE_MORE_RECORD + 1;
private LayoutInflater mInflater;
private TreeSet mSeparatorsSet = new TreeSet();
private ViewHolder holder = null;
public AccountListViewAdapter(Activity context) {
super();
this.context = context;
mInflater = (LayoutInflater)context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
// this.acountData = acountData;
}
public int getCount() {
// TODO Auto-generated method stub
return acountData.size();
}
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
public void addItem(final AccountData item) {
acountData.add(item);
notifyDataSetChanged();
}
@Override
public int getItemViewType(int position) {
return mSeparatorsSet.contains(position) ? TYPE_MORE_RECORD : TYPE_ITEM;
}
@Override
public int getViewTypeCount() {
return TYPE_MAX_COUNT;
}
public void addSeparatorItem(final AccountData item) {
acountData.add(item);
// save separator position
mSeparatorsSet.add(acountData.size() - 1);
notifyDataSetChanged();
}
private class ViewHolder {
TextView txtViewAccountHolder;
TextView type;
EditText ammountEdt;
ImageView arrow;
}
public View getView(final int position, View convertView, ViewGroup parent) {
int type = getItemViewType(position);
System.out.println("getView " + position + " " + convertView + " type = " + type);
if (convertView == null) {
holder = new ViewHolder();
switch (type) {
case TYPE_ITEM:
convertView = mInflater.inflate(R.layout.bank_account_row, null);
holder.txtViewAccountHolder = (TextView) convertView.findViewById(R.id.actType_textView);
holder.type = (TextView) convertView.findViewById(R.id.type_textView);
holder.ammountEdt = (EditText) convertView.findViewById(R.id.ammount_edt);
holder.arrow = (ImageView) convertView.findViewById(R.id.arrow_image);
break;
case TYPE_MORE_RECORD:
convertView = mInflater.inflate(R.layout.bank_account_addmore, null);
holder.txtViewAccountHolder = (TextView)convertView.findViewById(R.id.addMore_textView);
break;
}
convertView.setTag(holder);
} else {
holder = (ViewHolder)convertView.getTag();
}
try {
holder.txtViewAccountHolder.setText(acountData.get(position).getAccountHolder());
holder.type.setText(acountData.get(position).getAccountType());
holder.ammountEdt.setText(acountData.get(position).getAmmount());
holder.ammountEdt.setId(position);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
holder.txtViewAccountHolder.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(context, position+"pos", Toast.LENGTH_SHORT).show();
}
});
/*///we need to update adapter once we finish with editing
holder.ammountEdt.setOnFocusChangeListener(new OnFocusChangeListener() {
public void onFocusChange(View v, boolean hasFocus) {
if (!hasFocus){
final int position = v.getId();
final EditText Caption = (EditText) v;
Toast.makeText(context, Caption.getText().toString()+"pos", Toast.LENGTH_SHORT).show();
;
}
}
});
*/
if(holder.ammountEdt != null)
{
holder.ammountEdt.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(context, holder.ammountEdt.getText().toString()+"pos", Toast.LENGTH_SHORT).show();
}
});
}
return convertView;
}
}
LogCatは次のとおりです。
08-29 08:41:38.834: E/AndroidRuntime(267): FATAL EXCEPTION: main
08-29 08:41:38.834: E/AndroidRuntime(267): java.lang.NullPointerException
08-29 08:41:38.834: E/AndroidRuntime(267): at com.android.bank.adapter.AccountListViewAdapter$2.onClick(AccountListViewAdapter.java:154)
08-29 08:41:38.834: E/AndroidRuntime(267): at android.view.View.performClick(View.java:2408)
08-29 08:41:38.834: E/AndroidRuntime(267): at android.view.View$PerformClick.run(View.java:8816)
08-29 08:41:38.834: E/AndroidRuntime(267): at android.os.Handler.handleCallback(Handler.java:587)
08-29 08:41:38.834: E/AndroidRuntime(267): at android.os.Handler.dispatchMessage(Handler.java:92)
08-29 08:41:38.834: E/AndroidRuntime(267): at android.os.Looper.loop(Looper.java:123)
08-29 08:41:38.834: E/AndroidRuntime(267): at android.app.ActivityThread.main(ActivityThread.java:4627)
08-29 08:41:38.834: E/AndroidRuntime(267): at java.lang.reflect.Method.invokeNative(Native Method)
08-29 08:41:38.834: E/AndroidRuntime(267): at java.lang.reflect.Method.invoke(Method.java:521)
08-29 08:41:38.834: E/AndroidRuntime(267): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
08-29 08:41:38.834: E/AndroidRuntime(267): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
08-29 08:41:38.834: E/AndroidRuntime(267): at dalvik.system.NativeStart.main(Native Method)