本文介绍了Intent结束后,如何使用Contextmenu更新自定义列表视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在活动中自定义了列表视图.此列表视图具有上下文菜单.我必须启动一个Intent(Intent.ACTION_CALL).意图之后,我的应用程序显示了一个AlertDialog.AlertDialog,它是多选的.当用户单击PositiveButton时,程序会得到一个数字.我想在AdapterContextMenuInfo.position的列表视图上设置此值.另一方面,我的自定义列表视图arraylist是由另一个arraylist.size使用for方法创建的.当用户单击PositiveButton时,如何更新列表视图?

I have customized listview in my activity. This listview has contextmenu. I have to start an Intent(Intent.ACTION_CALL). After the Intent, my Application is showing an AlertDialog.AlertDialog that is multichoicable.,Program get a number when User click on the PositiveButton. I want to set this value on the listview at AdapterContextMenuInfo.position. In the other hand in my customized listview arraylist created by another arraylist.size with for method.How can I update my listview when user click on the PositiveButton?

public class AnaMenu extends AppCompatActivity {
    public static final int MENU_Ara = Menu.FIRST + 1;

    static final int AnketDegerlendirmesi = 1337;

    String[] afteractivitydialogitems;
    int selectedPosition ;
    String[] dialogdegerleri;
    int toplamaislemi;
    boolean[] dialogcheckeditems ;

    ArrayList<String> aramadegerlendirmesisonucu = new ArrayList<>();

    TextView sayılar;
    ListView liste;


    ArrayList<String> kullanıcısayıdegerlistesi = new ArrayList<>();

    ArrayList<Integer> mUserselecteditems = new ArrayList<>();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ana_menu);
        liste = (ListView) findViewById(R.id.oyun);
        //Adapter im.
        OyunTextView oyunTextView = new OyunTextView();

        liste.setAdapter(oyunTextView);

        registerForContextMenu(liste);

        afteractivitydialogitems = getResources().getStringArray(R.array.anket);

        dialogcheckeditems = new boolean[afteractivitydialogitems.length];

        dialogdegerleri = getResources().getStringArray(R.array.anketdeğer);
    }


    class OyunTextView extends BaseAdapter {

        @Override
        public int getCount() {
            Intent i = getIntent();
            final ArrayList<String> secilmis = i.getStringArrayListExtra("listem");

            return secilmis.size();
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

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

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            convertView = getLayoutInflater().inflate(R.layout.oyuntextview, null);
            TextView namesbox = (TextView) convertView.findViewById(R.id.isim);
             sayılar = (TextView) convertView.findViewById(R.id.sayı);
            Intent i = getIntent();
            final ArrayList<String> secilmis = i.getStringArrayListExtra("listem");

            for (int defa =0 ; defa<secilmis.size(); defa++){kullanıcısayıdegerlistesi.add("35");}


            namesbox.setText(secilmis.get(position));

            sayılar.setText(kullanıcısayıdegerlistesi.get(position));

            return convertView;
        }
    }


    public boolean onContextItemSelected(MenuItem item) {
        AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();

        switch (item.getItemId()) {
            case MENU_Ara:
                Intent i = getIntent();
                final ArrayList<String> secilmis = i.getStringArrayListExtra("listem");
   selectedPosition = info.position;
                Intent ara = new Intent(Intent.ACTION_CALL);
                ara.setData(Uri.parse("tel:" + secilmis.get(info.position).toString()));
                startActivityForResult(ara, AnketDegerlendirmesi);
   return true;
        }

        return super.onContextItemSelected(item);
    }


    protected void onActivityResult(int requestCode, int resultCode, final Intent data) {
        if (requestCode == AnketDegerlendirmesi){
            final AlertDialog.Builder  mBuilder = new AlertDialog.Builder(AnaMenu.this);
            mBuilder.setTitle("Değerlendirme Formu");
            mBuilder.setIcon(R.drawable.ic_build_black_24dp);
            mBuilder.setMultiChoiceItems(afteractivitydialogitems, dialogcheckeditems, new DialogInterface.OnMultiChoiceClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int position, boolean isChecked) {

                    if (isChecked){
                        mUserselecteditems.add(Integer.valueOf(dialogdegerleri[position]));
                    }
                    if (!isChecked) {
                        if (mUserselecteditems.contains(Integer.valueOf(dialogdegerleri[position]))){
                            mUserselecteditems.remove(Integer.valueOf(dialogdegerleri[position]));
                        }
                    }
                }
            });

            mBuilder.setCancelable(false);
            mBuilder.setPositiveButton("Tamamdır.", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    String items = "";
                    toplamaislemi = 0;
                    for (int item:mUserselecteditems){
                        items += "-" + item + "\n";

                        toplamaislemi = toplamaislemi + item;
                    }

                    //I want to set this value on Listview at calling AdapterContextMenuInfo.position 
                    kullanıcısayıdegerlistesi.remove(selectedPosition);
                    kullanıcısayıdegerlistesi.add(selectedPosition,String.valueOf(toplamaislemi));
                    oyunTextView.notifyDataSetChanged();

                }
            }).show();
        }

        super.onActivityResult(requestCode, resultCode, data);
    }

推荐答案

在onCreate()之前定义适配器

Define your adapter before onCreate()

OyunTextView oyunTextView;
int selectedPosition;

在onCreate()中创建适配器的新实例

Create your adapter's new instance in onCreate()

@Override
protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ana_menu);
        oyunTextView = new OyunTextView();
}

在菜单项上单击设置当前位置

Set current position on menu item click

public boolean onContextItemSelected(MenuItem item) {
        AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
        selectedPosition = info.position;
}

更新您的kullanıcısayıdegerlistesi并在点击侦听器中通知适配器

Update your kullanıcısayıdegerlistesi and notify adapter in the click listener

mBuilder.setPositiveButton("Tamamdır.", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            String items = "";
                          int toplamaislemi = 0;
                            for (int item:mUserselecteditems){
                                items += "-" + item + "\n";

                     toplamaislemi = toplamaislemi + item;
                     kullanıcısayıdegerlistesi.add(toplamaislemi);
                     oyunTextView.notifyDataSetChanged();
          }

这篇关于Intent结束后,如何使用Contextmenu更新自定义列表视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 22:12