如何获取列表视图中单击的单元格的名称以加载到新活动中?

查里斯

在此处输入图片说明

如何获取列表视图中特定单元格的名称以加载到新活动中?目前,当我单击任意箭头时,它将在单击该箭头时加载的下一个活动中加载联系人(伊冯)中的最后一个人。我希望在下一个活动中加载相应单元格中的名称。我怎样才能做到这一点?

例如,在上图中,我希望Alexi加载到下一个Activity中。但是我却一直得到伊冯。

目前,我的代码如下所示:

public class MainActivity extends Activity {


    // ArrayList called selectContacts that will contain SelectContact info
    ArrayList<SelectContact> selectContacts;

    ListView listView;

    SearchView search;
    SelectContactAdapter adapter;
    String name;
    String phoneNumber;
    String lookupkey;
    CharSequence nameofcontact;

    //    *****18-04-2016***
    Cursor cursor;
//    ListView mainListView;
//    ArrayList hashMapsArrayList;

    public String cleartext;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //selectContacts is an empty array list that will hold our SelectContct info
        selectContacts = new ArrayList<SelectContact>();

        listView = (ListView) findViewById(R.id.contacts_list);

        search = (SearchView) findViewById(R.id.searchView);


        //*** setOnQueryTextListener ***
        search.setOnQueryTextListener(new SearchView.OnQueryTextListener() {

            @Override
            public boolean onQueryTextSubmit(String query) {
                // TODO Auto-generated method stub

                return false;
            }

            @Override
            public boolean onQueryTextChange(String newText) {
                // TODO Auto-generated method stub
                adapter.filter(newText);
                return false;
            }
        });


    }

    // Load data on background
    class LoadContact extends AsyncTask<Void, Void, Void> {
        @Override
        protected void onPreExecute() {
            super.onPreExecute();

        }


        @Override
        protected Void doInBackground(Void... voids) {

//            Perhaps running this thread on the UI thread has solved the issue of the app
//            crashing? ListView had not been updating properly, I think.
            runOnUiThread(new Runnable() {
                public void run() {

//          we want to delete the old selectContacts from the listview when the Activity loads
//          because it may need to be updated and we want the user to see the updated listview,
//          like if the user adds new names and numbers to their phone contacts.
                    selectContacts.clear();

//          we have this here to avoid cursor errors
                    if (cursor != null) {
                        cursor.moveToFirst();

                    }
                    try {

//                get a handle on the Content Resolver, so we can query the provider,
                        cursor = getApplicationContext().getContentResolver()
//                the table to query
                                .query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
//               Null. This means that we are not making any conditional query into the contacts table.
//               Hence, all data is returned into the cursor.
//                                Projection - the columns you want to query
                                        null,
//                                Selection - with this you are extracting records with assigned (by you) conditions and rules
                                        null,
//                                SelectionArgs - This replaces any question marks (?) in the selection string
//                               if you have something like String[] args = { "first string", "[email protected]" };
                                        null,
//                                display in ascending order
                                        ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " COLLATE LOCALIZED ASC");

//                get the column number of the Contact_ID column, make it an integer.
//                I think having it stored as a number makes for faster operations later on.
                        int Idx = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.CONTACT_ID);
//                get the column number of the DISPLAY_NAME column
                        int nameIdx = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
//                 get the column number of the NUMBER column
                        int phoneNumberIdx = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);

//                ****
                        int contactlookupkey = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.LOOKUP_KEY);
//                ****



                        cursor.moveToFirst();

//              We make a new Hashset to hold all our contact_ids, including duplicates, if they come up
                        Set<String> ids = new HashSet<>();
                        do {
                            System.out.println("=====>in while");
//                  get a handle on the contactid, which is a string. Loop through all the contact_ids
                            String contactid = cursor.getString(Idx);
//                  if our Hashset doesn't already contain the contactid string,
//                    then add it to the hashset
                            if (!ids.contains(contactid)) {
                                ids.add(contactid);

                                HashMap<String, String> hashMap = new HashMap<String, String>();
//                        get a handle on the display name, which is a string
                                name = cursor.getString(nameIdx);
//                        get a handle on the phone number, which is a string
                                phoneNumber = cursor.getString(phoneNumberIdx);
//                        String image = cursor.getString(photoIdIdx);

                                lookupkey = cursor.getString(contactlookupkey);

                                System.out.println("Id--->" + contactid + " Name--->" + name);
                                System.out.println("Id--->" + contactid + " Number--->" + phoneNumber);
                                System.out.println("Id--->" + contactid + " lookupkey--->" + lookupkey);


                                SelectContact selectContact = new SelectContact();

                                selectContact.setName(name);
                                selectContact.setPhone(phoneNumber);

                                selectContacts.add(selectContact);
                            }


                        } while (cursor.moveToNext());


                    } catch (Exception e) {
                        e.printStackTrace();
                    } finally {

                    }
                }});

            return null;

        }

        @Override
        protected void onPostExecute(Void aVoid) {

            super.onPostExecute(aVoid);

//into each inflate_listview, put a name and phone number, which are the details making
//            our SelectContact, above. And SelectContacts is all these inflate_listviews together
//            This is the first property of our SelectContactAdapter, a list
//            The next part, MainActivity.this, is our context, which is where we want the list to appear
            adapter = new SelectContactAdapter(selectContacts, MainActivity.this);

//            adapter.notifyDataSetChanged();

            listView.setAdapter(adapter);

            // Select item on listclick
            listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {



                    listView.setFastScrollEnabled(true);
//                    we need to notify the listview that changes may have been made on
//                    the background thread, doInBackground, like adding or deleting contacts,
//                    and these changes need to be reflected visibly in the listview. It works
//                    in conjunction with selectContacts.clear()

//                    adapter.notifyDataSetChanged();


                }



            });
        }}

    //the is the arrow image, it opens the activity for show and edit
    public void DisplayorEditContact(View v) {
        System.out.println("works so far");
        System.out.println(name);
        Intent intent = new Intent(getApplicationContext(), EditorNewContact.class).putExtra("thecontactname",name);

        startActivity(intent);
    }


    @Override
    protected void onStop() {
        super.onStop();

    }



    @Override
    protected void onResume() {
//I want to clear the searchview text when my app resumes or closes, but I keep getting an error, my app shuts down
//    cleartext =  findViewById(R.id.searchView).toString();
//    cleartext.isEmpty();
//        search.setQuery("", false);
        super.onResume();
//    load the contacts again, refresh them, when the user resumes the activity
        LoadContact loadContact = new LoadContact();
        loadContact.execute();
//    cursor.close();
    }


}

我认为代码的重要部分是:

//the is the arrow image, it opens the activity for show and edit
public void DisplayorEditContact(View v) {
    System.out.println("works so far");
    System.out.println(name);
    Intent intent = new Intent(getApplicationContext(), EditorNewContact.class).putExtra("thecontactname",name);

    startActivity(intent);
}

我希望Alexi加载到其中的子活动(目前我一直在使用Yvonne)看起来像这样:

public class EditorNewContact extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.edit_contact);

        String s= getIntent().getStringExtra("thecontactname");
        System.out.println("the name is" + s);

        EditText edittext = (EditText) findViewById(R.id.editText);
        edittext.setText(s);

我被要求分享我的SelectContactAdapter,所以这里是:

public class SelectContactAdapter extends BaseAdapter {

    //define a list made out of SelectContacts and call it _data
    public List<SelectContact> _data;
    //define an array list made out of SelectContacts and call it arraylist
    private ArrayList<SelectContact> arraylist;
    Context _c;

    //define a ViewHolder to hold our name and number info, instead of constantly querying
    // findviewbyid. Makes the ListView run smoother
    ViewHolder v;

//    RoundImage roundedImage;

    public SelectContactAdapter(List<SelectContact> selectContacts, Context context) {
        _data = selectContacts;
        _c = context;
        this.arraylist = new ArrayList<SelectContact>();
        this.arraylist.addAll(_data);
    }

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

    @Override
    public Object getItem(int i) {
        return _data.get(i);
    }

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

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    @Override
    public View getView(int i, View convertView, ViewGroup viewGroup) {

        //we're naming our convertView as view
        View view = convertView;
        //if there is nothing there (if it's null) inflate the layout for each row
        if (view == null) {
            LayoutInflater li = (LayoutInflater) _c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = li.inflate(R.layout.inflate_listview, null);
//            Log.e("Inside", "here--------------------------- In view1");

            //or else use the view (what we can see in each row) that is already there
        } else {
            view = convertView;
//            Log.e("Inside", "here--------------------------- In view2");
        }

        v = new ViewHolder();

//      So, for example, title is cast to the name id, in activity main,
//        phone is cast to the id called no etc
        v.title = (TextView) view.findViewById(R.id.name);
//        v.check = (CheckBox) view.findViewById(R.id.check);
        v.phone = (TextView) view.findViewById(R.id.no);
        v.imageView = (ImageView) view.findViewById(R.id.arrowright);

//        for each new cell with title, name, number etc...
//
        final SelectContact data = (SelectContact) _data.get(i);
        v.title.setText(data.getName());
//        v.check.setChecked(data.getCheckedBox());
        v.phone.setText(data.getPhone());

        view.setTag(data);
        return view;
    }

    // Filter Class
    public void filter(String charText) {
        charText = charText.toLowerCase(Locale.getDefault());
//        _data is our list of contacts
        _data.clear();
//        If there is nothing in the searchview, if the charText length is 0,
//        then show all the contacts
        if (charText.length() == 0) {
            _data.addAll(arraylist);
//            or else....
        } else {
            for (SelectContact wp : arraylist) {
//                If a contact's name matches the input thus far, which is charText,
//                then include it in the listview.
                if ((wp.getName().toLowerCase(Locale.getDefault())
                        .contains(charText)) || (wp.getPhone().toLowerCase(Locale.getDefault())
                        .contains(charText)))
                     {

                        _data.add(wp);
                    }
            }
        }
        notifyDataSetChanged();
    }


    static class ViewHolder {
//        In each cell in the listview show the items you want to have
        ImageView imageView;
        TextView title, phone;
//        CheckBox check;
    }
}
苛刻的478​​9

请添加以下行到您的 SelectContactsAdapter.java

final SelectContact data = (SelectContact) _data.get(i);
v.title.setText(data.getName());
v.phone.setText(data.getPhone());

// Please add this line to your existing code right after above lines
v.imageView.setTag(data.getName());

如下修改您的方法

public void DisplayorEditContact(View v) {
        System.out.println("works so far");
        System.out.println(v.getTag().toString());
        Intent intent = new Intent(getApplicationContext(), EditorNewContact.class).putExtra("thecontactname",v.getTag().toString());
        startActivity(intent);
  }

希望这可以帮助

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

当用户单击单元格时,如何从表详细视图中的数组加载视频?

来自分类Dev

单击复选框时从集合视图单元格中获取名称

来自分类Dev

在单元格编辑事件中获取活动的datagrid的名称?

来自分类Dev

如何在C#中的网格视图中获取选定单元格的值

来自分类Dev

如何获取以编程方式在放置在iOS中的表格视图的单元格和单元格索引路径中的滚动视图中创建的视图的位置

来自分类Dev

表格视图中的单元格单击无法快速导航到新控制器

来自分类Dev

如何通过快速点击集合视图的单元格在表视图中重新加载数据

来自分类Dev

图像继续快速加载到集合视图单元格中

来自分类Dev

如何在Javafx(fxml)的表格视图中的单元格上单击鼠标右键?

来自分类Dev

加载表视图中的所有单元格后,会在表视图单元格中重新加载集合视图

来自分类Dev

单击指定的单元格后,在搜索栏占位符中获取表格视图单元格文本值

来自分类Dev

Swift-如何在单击按钮时在自定义表格视图单元格中获取标签值

来自分类Dev

Javafx如何从列表中获取单元格(不是值)

来自分类Dev

如何使新插入的表格视图单元格的textField处于活动状态?

来自分类Dev

如何将字典数组中的值加载到TableView单元格中

来自分类Dev

如何在列表视图中更改单元格的基于行颜色的值

来自分类Dev

如何在Xamarin Forms的列表视图中的多个项目中添加图像单元格

来自分类Dev

如何在Xamarin Forms的列表视图中的多个项目中添加图像单元格

来自分类Dev

如何使集合视图中的不同单元格进入Swift中的不同视图控制器

来自分类Dev

将远程图像加载到Tableview单元格中

来自分类Dev

列表视图中单元格中的多行文本标签

来自分类Dev

单元格中的多行用于列表视图中的文本标签

来自分类Dev

如何在网格视图中获取最后一列单元格的值

来自分类Dev

单击单元格时列表视图更改图标

来自分类Dev

将单元格加载到变体中后,如何获得其地址?

来自分类Dev

如何将范围内每个单元格的颜色索引加载到数组中?

来自分类Dev

单击按钮后获取表视图单元格索引

来自分类Dev

在ReactJs中单击时设置活动单元格

来自分类Dev

单元格已加载到cellForItemAt中,但未加载到visibleCells中

Related 相关文章

  1. 1

    当用户单击单元格时,如何从表详细视图中的数组加载视频?

  2. 2

    单击复选框时从集合视图单元格中获取名称

  3. 3

    在单元格编辑事件中获取活动的datagrid的名称?

  4. 4

    如何在C#中的网格视图中获取选定单元格的值

  5. 5

    如何获取以编程方式在放置在iOS中的表格视图的单元格和单元格索引路径中的滚动视图中创建的视图的位置

  6. 6

    表格视图中的单元格单击无法快速导航到新控制器

  7. 7

    如何通过快速点击集合视图的单元格在表视图中重新加载数据

  8. 8

    图像继续快速加载到集合视图单元格中

  9. 9

    如何在Javafx(fxml)的表格视图中的单元格上单击鼠标右键?

  10. 10

    加载表视图中的所有单元格后,会在表视图单元格中重新加载集合视图

  11. 11

    单击指定的单元格后,在搜索栏占位符中获取表格视图单元格文本值

  12. 12

    Swift-如何在单击按钮时在自定义表格视图单元格中获取标签值

  13. 13

    Javafx如何从列表中获取单元格(不是值)

  14. 14

    如何使新插入的表格视图单元格的textField处于活动状态?

  15. 15

    如何将字典数组中的值加载到TableView单元格中

  16. 16

    如何在列表视图中更改单元格的基于行颜色的值

  17. 17

    如何在Xamarin Forms的列表视图中的多个项目中添加图像单元格

  18. 18

    如何在Xamarin Forms的列表视图中的多个项目中添加图像单元格

  19. 19

    如何使集合视图中的不同单元格进入Swift中的不同视图控制器

  20. 20

    将远程图像加载到Tableview单元格中

  21. 21

    列表视图中单元格中的多行文本标签

  22. 22

    单元格中的多行用于列表视图中的文本标签

  23. 23

    如何在网格视图中获取最后一列单元格的值

  24. 24

    单击单元格时列表视图更改图标

  25. 25

    将单元格加载到变体中后,如何获得其地址?

  26. 26

    如何将范围内每个单元格的颜色索引加载到数组中?

  27. 27

    单击按钮后获取表视图单元格索引

  28. 28

    在ReactJs中单击时设置活动单元格

  29. 29

    单元格已加载到cellForItemAt中,但未加载到visibleCells中

热门标签

归档