我在20行EditText
内有Checkbox
和ListView
。前7行隐藏EditText
,所有其他行隐藏CheckBox
。 onItemClick
对于前7行是可见的,然后对其他行是隐藏的。 toast
我已经让Edittext
获取了NullPointerException
的文本,该文本适用于启动10行但不适用于滚动时出现的最后一行。应用程序崩溃后会发送{ {1}}错误
public View getView(int position, View convertView, ViewGroup parent) {
View vi=convertView;
ViewHolder holder;
if(convertView==null){
/********** Inflate tabitem.xml file for each row ( Defined below ) ************/
vi = inflater.inflate(R.layout.tabitem, null);
/******** View Holder Object to contain tabitem.xml file elements ************/
holder=new ViewHolder();
holder.text=(TextView)vi.findViewById(R.id.textView1);
holder.check =(CheckBox)vi.findViewById(R.id.checkBox1);
holder.text2=(EditText)vi.findViewById(R.id.editText1);
/************ Set holder with LayoutInflater ************/
vi.setTag(holder);
j++;
if(j<8){
holder.text2.setVisibility(View.INVISIBLE);
holder.check.setX(-150);
}else
{
holder.check.setVisibility(View.INVISIBLE);
}
}
else
holder=(ViewHolder)vi.getTag();
holder.text2.getText();
if(data.size()<=0)
{
holder.text.setText("No Data");
holder.text2.getText();
// Log.v("BBBB", holder.text2.getText().toString());
}
else
{
/***** Get each Model object from Arraylist ********/
tempValues=null;
tempValues = (ListModel) data.get(position);
/************ Set Model values in Holder elements ***********/
holder.text.setText(tempValues.getCompanyName());
holder.text2.getText();
/******** Set Item Click Listner for LayoutInflater for each row ***********/
vi.setOnClickListener(new OnItemClickListener(position));
}
Log.v("DDD", holder.text2.getText().toString());
return vi;
}
和
@Override
public int getViewTypeCount() {
return getCount();
}
@Override
public int getItemViewType(int position) {
return position;
}
但是应用程序崩溃了onITemClick方法......甚至EditText文本都会导致错误...
public void onItemClick(int mPosition)
{
View vListSortOrder;
vListSortOrder=list.getChildAt(mPosition);
Log.d("valueeeeeeee", Integer.toString(mPosition));
EditText edit=(EditText)vListSortOrder.findViewById(R.id.editText1);
String temp=edit.getText().toString();
}
public void onClick(View arg0) {
CustomListViewAndroidExample sct = (CustomListViewAndroidExample)activity; sct.onItemClick(mPosition); }
上面的onItemClick可以很好地启动10行..但是在第11行,它会在EditText edit
行崩溃并出现空指针异常错误
logcat的
04-28 00:46:29.101: W/dalvikvm(2178): threadid=1: thread exiting with uncaught exception (group=0x41465700)
04-28 00:46:29.161: E/AndroidRuntime(2178): FATAL EXCEPTION: main
04-28 00:46:29.161: E/AndroidRuntime(2178): java.lang.NullPointerException
04-28 00:46:29.161: E/AndroidRuntime(2178): at com.androidexample.customlistview.CustomListViewAndroidExample.onItemClick(CustomListViewAndroidExample.java:172)
04-28 00:46:29.161: E/AndroidRuntime(2178): at com.androidexample.customlistview.CustomAdapter$OnItemClickListener.onClick(CustomAdapter.java:222)
完整的适配器代码
@SuppressLint("NewApi") public class CustomAdapter extends BaseAdapter implements OnClickListener {
/*********** Declare Used Variables *********/
private Activity activity;
private ArrayList data;
private static LayoutInflater inflater=null;
public Resources res;
ListModel tempValues=null;
int i=0;
int j=0;
public ArrayList<String> selectedStrings = new ArrayList<String>();
/************* CustomAdapter Constructor *****************/
public CustomAdapter(Activity a, ArrayList d,Resources resLocal) {
/********** Take passed values **********/
activity = a;
data=d;
res = resLocal;
/*********** Layout inflator to call external xml layout () **********************/
inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
/******** What is the size of Passed Arraylist Size ************/
public int getCount() {
if(data.size()<=0)
return 1;
return data.size();
}
public Object getItem(int position) {
return data.get(position);
}
public long getItemId(int position) {
return 0;
}
/********* Create a holder to contain inflated xml file elements ***********/
public static class ViewHolder{
public TextView text;
public TextView text1;
public EditText text2;
public TextView textWide;
public ImageView image;
public CheckBox check;
}
/*********** Depends upon data size called for each row , Create each ListView row ***********/
public View getView(int position, View convertView, ViewGroup parent) {
View vi=convertView;
final ViewHolder holder;
if(convertView==null){
/********** Inflate tabitem.xml file for each row ( Defined below ) ************/
vi = inflater.inflate(R.layout.tabitem, null);
/******** View Holder Object to contain tabitem.xml file elements ************/
holder=new ViewHolder();
holder.text=(TextView)vi.findViewById(R.id.textView1);
holder.check =(CheckBox)vi.findViewById(R.id.checkBox1);
holder.text2=(EditText)vi.findViewById(R.id.editText1);
holder.check.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v)
{
if (((CheckBox) v).isChecked())
{
holder.text2.setText("Checked");
}
else
{
holder.text2.setText("Not Checked");
}
}
});
/************ Set holder with LayoutInflater ************/
vi.setTag(holder);
j++;
if(j<8){
holder.text2.setVisibility(View.INVISIBLE);
holder.check.setX(-150);
holder.check.setVisibility(View.VISIBLE);
}else
{
holder.check.setVisibility(View.INVISIBLE);
holder.text2.setVisibility(View.VISIBLE);
}
}
else
holder=(ViewHolder)vi.getTag();
holder.text2.getText();
if(data.size()<=0)
{
holder.text.setText("No Data");
holder.text2.getText();
}
else
{
/***** Get each Model object from Arraylist ********/
tempValues=null;
tempValues = (ListModel) data.get(position);
/************ Set Model values in Holder elements ***********/
holder.text.setText(tempValues.getCompanyName());
/******** Set Item Click Listner for LayoutInflater for each row ***********/
vi.setOnClickListener(new OnItemClickListener(position));
}
return vi;
}
@Override
public int getViewTypeCount() {
return getCount();
}
@Override
public int getItemViewType(int position) {
return position;
}
@Override
public void onClick(View v) {
Log.v("CustomAdapter", "=====Row button clicked");
}
/********* Called when Item click in ListView ************/
private class OnItemClickListener implements OnClickListener{
private int mPosition;
OnItemClickListener(int position){
mPosition = position;
}
@Override
public void onClick(View arg0) {
CustomListViewAndroidExample sct = (CustomListViewAndroidExample)activity;
sct.onItemClick(mPosition);
}
}
}
04-28 03:33:23.542: E/AndroidRuntime(4917): FATAL EXCEPTION: main
04-28 03:33:23.542: E/AndroidRuntime(4917): java.lang.NullPointerException
04-28 03:33:23.542: E/AndroidRuntime(4917): at com.androidexample.customlistview.CustomListViewAndroidExample$1.onClick(CustomListViewAndroidExample.java:89)
04-28 03:33:23.542: E/AndroidRuntime(4917): at android.view.View.performClick(View.java:4240)
04-28 03:33:23.542: E/AndroidRuntime(4917): at android.view.View$PerformClick.run(View.java:17721)
04-28 03:33:23.542: E/AndroidRuntime(4917): at android.os.Handler.handleCallback(Handler.java:730)
答案 0 :(得分:0)
你写了令人困惑的代码。
将标签插入到text2:
holder.text2.setTag(1);
然后使用下一行:
int position12 = (Integer)holder.text2.getTag();
所以每次position12
等于1。
接下来就是使用j
而不是位置。
您需要更改自己的可见度,就像跟随代码一样,因为ListView's recycling
if(j<8){
holder.text2.setVisibility(View.INVISIBLE);
holder.check.setVisibility(View.VISIBLE);
holder.check.setX(-150);
}else{
holder.text2.setVisibility(View.VISIBLE);
holder.check.setVisibility(View.INVISIBLE);
}
并且由于您有一个布局,为什么要覆盖getViewTypeCount
和getItemViewType
?
请从getView方法
中删除以下行 holder.text2.getText();
和
Log.v("DDD", holder.text2.getText().toString());
您的问题
你的问题是:
private class OnItemClickListener implements OnClickListener{
private int mPosition;
OnItemClickListener(int position){
mPosition = position;
}
@Override
public void onClick(View arg0) {
CustomListViewAndroidExample sct = (CustomListViewAndroidExample)activity;
sct.onItemClick(mPosition); <--- problem is here
}
}
我不知道你想要什么,但你可以这样做。
1-改变:
vi.setOnClickListener(new OnItemClickListener(position));
到
vi.setOnClickListener(this);
2-为getView方法添加以下行:
holder.text.setTag(position);
3-将以下代码添加到onClick方法:
@Override
public void onClick(View v) {
TextView tv = (TextView)v.findViewById(R.id.textView1);
int pos = (Integer)tv.getTag();
Log.d("position of clicked item is", ""+pos);
}
4-改变
if(j<8)
到
if( position <8)
答案 1 :(得分:0)
@Override
public int getCount() {
return your_objects_list.size();
}
@Override
public YourObjectType getItem(int position) {
return your_objects_list.get(position);
}
@Override
public long getItemId(int position) {
// you could return any id related to your context, i just return 0 because I don't need it
return 0;
}
你绝对不需要这些:
@Override
public int getViewTypeCount() {
return getCount();
}
@Override
public int getItemViewType(int position) {
return position;
}
看,让我解释你何时应该使用这些方法:
<强> getViewTypeCount 强>:
此方法应返回您正在使用的不同 XML布局文件的数量。例如,如果您对行使用row_layout_1.xml和row_layout_2.xml文件,则getViewTypeCount()应始终返回2.
因此,如果您对所有行使用单个.XML文件,那么您真的不应该覆盖此方法。
<强> getItemViewType 强>:
此方法在单个列表项上调用时,应返回该类型。在我之前提到的例子中,它应该根据项目的位置返回1或2。
由于您使用的是单个XML行类型,因此您也不应该覆盖此方法。
让我知道你的结果。
j
,而是使用position
。移动此代码并在返回vi之前将其放置一行;
if(position <8){
holder.text2.setVisibility(View.INVISIBLE);
holder.check.setX(-150);
holder.check.setVisibility(View.VISIBLE);
}else
{
holder.check.setVisibility(View.INVISIBLE);
holder.text2.setVisibility(View.VISIBLE);
}
让我知道你的结果。