ListView项目的onClick侦听器

时间:2020-06-11 11:54:19

标签: android listview webview android-asynctask onclicklistener

我在WebView中保存并显示书签。我可以在listview中成功保存并显示书签。 我按照本教程进行了这项工作https://www.journaldev.com/15729/android-webview-bookmarks

我遇到的唯一问题是在列表视图中,如果用户要删除任何书签,我必须为该用户放置一个删除图标,并且在本教程中,这项工作发生在OnItemLongClickListener上。我无法找到如何我单击图标后可以删除书签,因为我无法获取该项目的位置。 我使用了SimpleAdapter,它内置于用于ListView的android适配器中。

我的课是:

import static com.example.myapplication.WebViewActivity.PREFERENCES;
import static com.example.myapplication.WebViewActivity.WEB_LINKS;
import static com.example.myapplication.WebViewActivity.WEB_TITLE;

public class BookmarkActivity extends AppCompatActivity {
    ArrayList<HashMap<String, String>> listRowData;

    public static String TAG_TITLE = "title";
    public static String TAG_LINK = "link";

    ListView listView;
    ImageView imageView;
    ListAdapter adapter;
    LinearLayout linearLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bookmark);
        getViews();
        getListeners();
    }

    private void getListeners() {



        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

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

                Object o = listView.getAdapter().getItem(position);
                if (o instanceof Map) {
                    Map map = (Map) o;
                    Intent in = new Intent(BookmarkActivity.this, WebViewActivity.class);
                    in.putExtra("url", String.valueOf(map.get(TAG_LINK)));
                    startActivity(in);
                }


            }
        });

       /* imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                }*/

                listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
                    @Override
                    public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long l) {
                        Object o = listView.getAdapter().getItem(i);
                        if (o instanceof Map) {
                            Map map = (Map) o;
                            deleteBookmark(String.valueOf(map.get(TAG_TITLE)), String.valueOf(map.get(TAG_LINK)));
                        }

                        return true;
                    }
                });

    }

    private void getViews() {
        listView = findViewById(R.id.listView);
        linearLayout = findViewById(R.id.emptyList);

        imageView = findViewById(R.id.deleteBookmark);

        new LoadBookmarks().execute();
    }

    private class LoadBookmarks extends AsyncTask<String, String, String> {


        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }

        @Override
        protected String doInBackground(String... args) {
            // updating UI from Background Thread
            runOnUiThread(new Runnable() {
                public void run() {

                    SharedPreferences sharedPreferences = getSharedPreferences(PREFERENCES, Context.MODE_PRIVATE);
                    String jsonLink = sharedPreferences.getString(WEB_LINKS, null);
                    String jsonTitle = sharedPreferences.getString(WEB_TITLE, null);
                    listRowData = new ArrayList<>();

                    if (jsonLink != null && jsonTitle != null) {

                        Gson gson = new Gson();
                        ArrayList<String> linkArray = gson.fromJson(jsonLink, new TypeToken<ArrayList<String>>() {
                        }.getType());

                        ArrayList<String> titleArray = gson.fromJson(jsonTitle, new TypeToken<ArrayList<String>>() {
                        }.getType());


                        for (int i = 0; i < linkArray.size(); i++) {
                            HashMap<String, String> map = new HashMap<>();

                            if (titleArray.get(i).length() == 0)
                                map.put(TAG_TITLE, "Bookmark " + (i + 1));
                            else
                                map.put(TAG_TITLE, titleArray.get(i));

                            map.put(TAG_LINK, linkArray.get(i));
                            listRowData.add(map);
                        }

                        adapter = new SimpleAdapter(BookmarkActivity.this,
                                listRowData, R.layout.bookmark_list_row,
                                new String[]{TAG_TITLE, TAG_LINK},
                                new int[]{R.id.title, R.id.link});

                        listView.setAdapter(adapter);
                    }

                    linearLayout.setVisibility(View.VISIBLE);
                    listView.setEmptyView(linearLayout);


                }
            });
            return null;
        }

        protected void onPostExecute(String args) {
            mSwipeRefreshLayout.setRefreshing(false);
        }

    }

0 个答案:

没有答案