在android中的listview特定行中动态添加和删除视图

时间:2013-05-16 06:49:01

标签: android android-listview android-arrayadapter

在我的应用程序中,我创建了Custom ListView。所以我的问题我想添加和删除ListView的特定行的一些视图。根据我的编码。但是它给了我空指针异常
有一个按钮(比如添加),我正在查看它到特定的位置。通过点击它我添加了一个带有textView的布局和按钮(比如删除)。成功后我可以添加新的布局。
但它不是删除添加的布局(空指针exp。)。
以下是适配器类:

public class TestAdapter extends ArrayAdapter<String> {
    Context con;
    ArrayList<String> listdata;

    public TestAdapter(Context context, int textViewResourceId,ArrayList<String> myitem) 
    {
        super(context, textViewResourceId, myitem);
        this.con = context;
        this.listdata = myitem;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) 
    {
        LayoutInflater inflater = (LayoutInflater) con.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        final LinearLayout.LayoutParams linearparams=new LinearLayout.LayoutParams(
                android.widget.LinearLayout.LayoutParams.WRAP_CONTENT,android.widget.LinearLayout.LayoutParams.WRAP_CONTENT);

        if (convertView == null) 
        {
            convertView = inflater.inflate(R.layout.fieldlist, null);
        }
        TextView data = (TextView) convertView.findViewById(R.id.VTItemTextView);
        Button add=(Button)convertView.findViewById(R.id.add);

        final EditText amount=(EditText)convertView.findViewById(R.id.amount);
        final LinearLayout llmain=(LinearLayout)convertView.findViewById(R.id.llmain);

        if(listdata.get(position).equalsIgnoreCase("lorem"))
        {
            add.setVisibility(View.VISIBLE);

            add.setOnClickListener(new OnClickListener() 
            {                   
                @Override
                public void onClick(View v) 
                {                       
                    final LinearLayout llcontent=new LinearLayout(con);
                    llcontent.setLayoutParams(linearparams);
                    final TextView textView=new TextView(con);
                    textView.setLayoutParams(linearparams);
                    textView.setText(amount.getText().toString());
                    textView.setTag(position);
                    final Button delete=new Button(con);
                    delete.setLayoutParams(linearparams);
                    delete.setText("delete");                       
                    delete.setTag(position);
                    llcontent.addView(textView);
                    llcontent.addView(delete);
                    llmain.addView(llcontent);                      
                    notifyDataSetChanged();
                    delete.setOnClickListener(new OnClickListener() {

                        @Override
                        public void onClick(View v) {
                             Integer index = (Integer) v.getTag();
                        //here is Null Pointer Exception
                            llcontent.removeViewAt(index.intValue()); 

                                Integer index1 = (Integer)textView.getTag();
                                llcontent.removeViewAt(index1.intValue()); 

                            notifyDataSetChanged();

                        }
                    });
                }
            });
        }
        else
            {
                add.setVisibility(View.GONE);
                amount.setVisibility(View.GONE);
            }
        data.setText(listdata.get(position));
        return convertView;
    }
}


LogCat:

  FATAL EXCEPTION: main
java.lang.NullPointerException
at android.view.ViewGroup.removeViewInternal(ViewGroup.java:3603)
at android.view.ViewGroup.removeViewAt(ViewGroup.java:3567)
at com.example.testapp.TestAdapter$1$1.onClick(TestAdapter.java:91)
at android.view.View.performClick(View.java:4204)
at android.view.View$PerformClick.run(View.java:17355)
at android.os.Handler.handleCallback(Handler.java:725)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5041)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(Native Method)

为测试我创建了一个单独的演示并且它工作正常但是如果我在另一个应用程序中使用此逻辑它不起作用。为什么这样......?帮助我......!

1 个答案:

答案 0 :(得分:0)

尝试在代码中使用context对象而不是MainActivity.this。看来你使用的是错误的上下文。请仔细检查您使用MainActivity.this的行。