CustomlistAdapter中的共享首选项

时间:2015-01-06 11:37:35

标签: android sharedpreferences

我需要在customlistadapter的共享偏好中存储值。当我点击列表视图时,将打开Edittext警告框。在该警告框中我们可以给出数量值。如果我点击提示中的"确定"按钮数据值应保存在共享首选项中的框。在我的情况下,当我点击listview.help我找到解决方案时应用程序崩溃。谢谢提前。

public CustomListAdapter(Activity activity, ArrayList<Movie> movieItems1,int Groupid,Context context) 
    {
        this.activity = activity;
        this.movieItems = movieItems1;
        this.Groupid=Groupid;
        this.c=context;
        //this.arraylist = new ArrayList<Movie>();
        this.arraylist=movieItems1;

    }




    @Override
    public int getCount() {
        return movieItems.size();
    }

    @Override
    public Object getItem(int location) {
        return movieItems.get(location);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }



    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        if (inflater == null)
            inflater = (LayoutInflater) activity
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        if (convertView == null)
            convertView = inflater.inflate(R.layout.list_row, null);

        if (imageLoader == null)
            imageLoader = AppController.getInstance().getImageLoader();
        NetworkImageView thumbNail = (NetworkImageView) convertView
                .findViewById(R.id.thumbnail);
        TextView title = (TextView) convertView.findViewById(R.id.title);
        TextView rating = (TextView) convertView.findViewById(R.id.rating);
        // TextView genre = (TextView) convertView.findViewById(R.id.genre);
        // TextView year = (TextView)
        // convertView.findViewById(R.id.releaseYear);
        TextView quantityTextView = (TextView) convertView.findViewById(R.id.qty);

        /*quantityTextView.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                handleQuntityClickEvent(position);

            }
        });*/




       MainActivity.listView.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,int position, long id)
            {



                handleQuntityClickEvent(position,Groupid);




            }
        });


        // getting movie data for the row
        Movie m = movieItems.get(position);

        // thumbnail image
        thumbNail.setImageUrl(m.getThumbnailUrl(), imageLoader);


        title.setText(m.getTitle());

        quantityTextView.setText("" + m.getQuantity());



        rating.setText(m.getGenre().get(0) + " "+ m.getProductscale() + " " +"Rs."+ String.valueOf(m.getGenre().get(1)));


        String genreStr = "";
        for (String str : m.getGenre()) {
            genreStr += str + ", ";
        }
        genreStr = genreStr.length() > 0 ? genreStr.substring(0,
                genreStr.length() - 2) : genreStr;
        // genre.setText(genreStr);

        // release year
        // year.setText(String.valueOf(m.getYear()));




        return convertView;
    }

    /** 
     * launch a edi
     * 
     * @param position
     */
    protected void handleQuntityClickEvent(int position, final int Groupid) {
        // TODO launch a dialog with edittext. The view has OK and Cancel
        // button. if the OK button pressed then
        this.mPosition = position;

        Builder alertDialog = new AlertDialog.Builder(activity);
        final EditText editText = new EditText(activity);

        editText.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL | InputType.TYPE_NUMBER_FLAG_SIGNED);
        editText.setHint("Quantity");
        editText.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
                LayoutParams.WRAP_CONTENT));
        alertDialog.setView(editText);// set a editext view here
        alertDialog.setTitle("Add Quantity");


      sharedpreferences = c.getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
         final Editor editor = sharedpreferences.edit();



        alertDialog.setPositiveButton("OK",
                new DialogInterface.OnClickListener() {



                    @Override
                    public void onClick(DialogInterface dialog, int which) 
                    {


                        //SharedPreferences pref = mContext.getSharedPreferences("MyPref", Context.MODE_PRIVATE); 


                          editor.putInt("Userid", Groupid);
                         editor.commit(); 

                        handleOkButtonPressed(editText);
                        dialog.dismiss();
                    }
                });
        alertDialog.setNegativeButton("Cancel", null);
        alertDialog.show();
    }

logcat的

 01-06 16:58:31.281: E/AndroidRuntime(16893): FATAL EXCEPTION: main
    01-06 16:58:31.281: E/AndroidRuntime(16893): java.lang.NullPointerException
    01-06 16:58:31.281: E/AndroidRuntime(16893):    at com.telveg.adater.CustomListAdapter.handleQuntityClickEvent(CustomListAdapter.java:196)
    01-06 16:58:31.281: E/AndroidRuntime(16893):    at com.telveg.adater.CustomListAdapter$1.onItemClick(CustomListAdapter.java:127)
    01-06 16:58:31.281: E/AndroidRuntime(16893):    at android.widget.AdapterView.performItemClick(AdapterView.java:301)
    01-06 16:58:31.281: E/AndroidRuntime(16893):    at android.widget.AbsListView.performItemClick(AbsListView.java:1508)
    01-06 16:58:31.281: E/AndroidRuntime(16893):    at android.widget.AbsListView$PerformClick.run(AbsListView.java:3293)
    01-06 16:58:31.281: E/AndroidRuntime(16893):    at android.widget.AbsListView$1.run(AbsListView.java:4554)
    01-06 16:58:31.281: E/AndroidRuntime(16893):    at android.os.Handler.handleCallback(Handler.java:725)
    01-06 16:58:31.281: E/AndroidRuntime(16893):    at android.os.Handler.dispatchMessage(Handler.java:92)
    01-06 16:58:31.281: E/AndroidRuntime(16893):    at android.os.Looper.loop(Looper.java:176)
    01-06 16:58:31.281: E/AndroidRuntime(16893):    at android.app.ActivityThread.main(ActivityThread.java:5365)
    01-06 16:58:31.281: E/AndroidRuntime(16893):    at java.lang.reflect.Method.invokeNative(Native Method)
    01-06 16:58:31.281: E/AndroidRuntime(16893):    at java.lang.reflect.Method.invoke(Method.java:511)
    01-06 16:58:31.281: E/AndroidRuntime(16893):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
    01-06 16:58:31.281: E/AndroidRuntime(16893):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
    01-06 16:58:31.281: E/AndroidRuntime(16893):    at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:0)

而不是这一行

MainActivity.listView.setOnItemClickListener(new OnItemClickListener() {

使用

convertView.setOnClickListener(){
handleQuntityClickEvent(position,Groupid);
}

并在 getView()

中移动此代码块