团队冲刺第十六天

团队代码https://www.cnblogs.com/yumazemadui/p/12927064.html

自己遇到的主要的问题是自己两个界面同时跳转到同一个界面。出现传参冲突等问题。今天团队的收获如下。

然后就是关于多个相关的fragment来回滑动怎样进行界面的刷新。一下的代码完美解决问题。

复制代码

 //刷新作用
    @Override
    public void onResume() {
        super.onResume();
       init();
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        // Todo Auto-generated method stub
        super.onHiddenChanged(hidden);
        init();
    }

复制代码

今天对自己原来的的listview的相应的界面进行了改进。设置了长按触发事件和点击事件。

具体的相关的代码如下:

复制代码

package com.showlocation;

import androidx.fragment.app.Fragment;
import com.bean.*;
import android.accounts.Account;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import com.DBOpenMessageUser.*;
import com.example.keeprunning1.R;
import com.showlocation.StaticDemo2;

import java.util.ArrayList;
import java.util.List;

public class showlocationFragment extends Fragment {
    private DBOpenMessageUserlocation dbOpenMessage;
    private String username;
    private ListView listview;
    private List<location> alllistmessage = new ArrayList<location>();
    private LocationAdapter adapter;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.activity_showlocation, container, false);
        listview = (ListView) view.findViewById(R.id.list_view);
        init();
        return view;
    }
    private void init()
    {
        dbOpenMessage = new DBOpenMessageUserlocation(getActivity(), "db_location", null, 1);

        Intent intent = getActivity().getIntent();//获取Intent对象
        username = intent.getStringExtra("username");
        //username = getArguments().getString("username");
        alllistmessage.clear();
        getMessage1(username);
        adapter = new LocationAdapter(getActivity(), alllistmessage);
        //listview.setAdapter(null);
        //adapter.notifyDataSetChanged();

        listview.setAdapter(adapter);

        listview.setonItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView parent, View view, int position, long id) {

                //   Intent intent=new Intent(getActivity(),AllMessage.class);
                // intent.putExtra("username",username2);
                //startActivity(intent);
                location message = (location) parent.getItemAtPosition(position);

                Intent intent = new Intent();
                intent.setClass(getActivity(), StaticDemo2.class);
                Bundle bundle = new Bundle();
                bundle.putSerializable("message", message);
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });
    }

    /*  private void xianshixinxi()
      {
          Cursor cursor1=dbOpenMessage.getReadableDatabase().query("db_wen2",null,"username=?",new String[]{username},null,null,null);
          ArrayList<Map<String,String>> resultlist=new ArrayList<Map<String,String >>();
      }*/
    private void getMessage1(String username) {
        Cursor cursor = dbOpenMessage.getAllLocation(username);
        if (cursor != null && cursor.getCount() > 0) {
            cursor.movetoFirst();
            for (cursor.movetoFirst(); !cursor.isAfterLast();cursor.movetoNext()){
                location message2 = new location();
                message2.distance = cursor.getString(cursor.getColumnIndex("distance"));
                message2.energy = cursor.getString(cursor.getColumnIndex("energy"));
                message2.date = cursor.getString(cursor.getColumnIndex("date"));
                message2.points = cursor.getString(cursor.getColumnIndex("points"));
                message2.speed = cursor.getString(cursor.getColumnIndex("speed"));
                message2.time = cursor.getString(cursor.getColumnIndex("time"));
                message2.username = cursor.getString(cursor.getColumnIndex("username"));
                message2.id = cursor.getInt(cursor.getColumnIndex("_id"));
                alllistmessage.add(message2);
            }
        }
    }

    //实现长按删除listview里的item事件
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        final ListView listView = (ListView) getActivity().findViewById(R.id.list_view);

        listView.setonItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, final View view, final int position, long id) {
                AlertDialog.Builder builder = new AlertDialog.Builder(view.getContext());
                final location[] account = {null};
                account[0] = alllistmessage.get(position);
                builder.setTitle("是否确定删除此运动???");
                builder.setMessage("记录内容如下:\n"+"用户: "+account[0].username+"\n运动日期: "+account[0].date+"\n运动距离: "+account[0].distance+"\n消耗能量: "+account[0].energy);
                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dbOpenMessage.deletebynameanddistance(account[0].username,account[0].date);
                        alllistmessage.remove(position);
                        adapter.notifyDataSetChanged();
                        Toast.makeText(view.getContext(), "删除该事件成功", Toast.LENGTH_SHORT).show();
                    }
                });
                builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                });
                builder.create().show();
                return true;
            }
        });

    }
    //刷新作用
    @Override
    public void onResume() {
        super.onResume();
       init();
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        // Todo Auto-generated method stub
        super.onHiddenChanged(hidden);
        init();
    }

}

复制代码

相关文章

Android性能优化——之控件的优化 前面讲了图像的优化,接下...
前言 上一篇已经讲了如何实现textView中粗字体效果,里面主要...
最近项目重构,涉及到了数据库和文件下载,发现GreenDao这个...
WebView加载页面的两种方式 一、加载网络页面 加载网络页面,...
给APP全局设置字体主要分为两个方面来介绍 一、给原生界面设...
前言 最近UI大牛出了一版新的效果图,按照IOS的效果做的,页...