新添加长按删除好友功能
This commit is contained in:
17
.idea/deploymentTargetDropDown.xml
generated
17
.idea/deploymentTargetDropDown.xml
generated
@@ -1,17 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="deploymentTargetDropDown">
|
||||
<runningDeviceTargetSelectedWithDropDown>
|
||||
<Target>
|
||||
<type value="RUNNING_DEVICE_TARGET" />
|
||||
<deviceKey>
|
||||
<Key>
|
||||
<type value="SERIAL_NUMBER" />
|
||||
<value value="STS0219524008890" />
|
||||
</Key>
|
||||
</deviceKey>
|
||||
</Target>
|
||||
</runningDeviceTargetSelectedWithDropDown>
|
||||
<timeTargetWasSelectedWithDropDown value="2022-11-11T08:33:48.357677900Z" />
|
||||
</component>
|
||||
</project>
|
||||
@@ -8,11 +8,13 @@ import android.os.Handler;
|
||||
import android.os.Message;
|
||||
import android.util.Log;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.MenuItem;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.AdapterView;
|
||||
import android.widget.ImageView;
|
||||
import android.widget.ListView;
|
||||
import android.widget.PopupMenu;
|
||||
import android.widget.TextView;
|
||||
|
||||
import com.example.teambag.adapter.SortAdapter;
|
||||
@@ -43,6 +45,7 @@ public class ContactListFragment extends Fragment {
|
||||
String[] imgUrl;
|
||||
String[] name;
|
||||
String[] friend;
|
||||
String hisNumber;
|
||||
private String number; //微信号,通过微信号去查找通讯录
|
||||
/* 声明组件*/
|
||||
private ListView listView;
|
||||
@@ -126,9 +129,53 @@ public class ContactListFragment extends Fragment {
|
||||
/*创建自定义适配器,并设置给listview*/
|
||||
SortAdapter adapter = new SortAdapter(getActivity().getApplicationContext(), list, list2, data);
|
||||
listView.setAdapter(adapter);
|
||||
listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
|
||||
@Override
|
||||
public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long l) {
|
||||
if(i<4)
|
||||
return false;
|
||||
hisNumber=list.get(i).getNumber();
|
||||
PopupMenu popupMenu = new PopupMenu(getActivity().getApplicationContext(), view);
|
||||
popupMenu.getMenuInflater().inflate(R.menu.sample_menu,popupMenu.getMenu());
|
||||
popupMenu.show();
|
||||
popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
|
||||
@Override
|
||||
public boolean onMenuItemClick(MenuItem menuItem) {
|
||||
switch (menuItem.getItemId()){
|
||||
case R.id.showFriend:
|
||||
break;
|
||||
case R.id.deleteFriend:
|
||||
Thread thread2 = new Thread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
httpUrlConnPostToDelete(Main.number,hisNumber,i);
|
||||
}
|
||||
});
|
||||
thread2.start();
|
||||
/*等待线性处理完成*/
|
||||
try {
|
||||
thread2.join();
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
adapter.notifyDataSetChanged();
|
||||
break;
|
||||
case R.id.chatFriend:
|
||||
break;
|
||||
case R.id.blockFriend:
|
||||
break;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
});
|
||||
return true;
|
||||
}
|
||||
});
|
||||
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
|
||||
@Override
|
||||
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
|
||||
if(i<4)
|
||||
return;
|
||||
String his_name=list.get(i).getName();
|
||||
Intent intent = new Intent(getActivity(),ChatRoom.class);
|
||||
for(int k =0;k<data.size();k++) {
|
||||
@@ -146,6 +193,75 @@ public class ContactListFragment extends Fragment {
|
||||
});
|
||||
}
|
||||
|
||||
public void httpUrlConnPostToDelete(String number, String friend,int i){
|
||||
HttpURLConnection urlConnection = null;
|
||||
URL url;
|
||||
try {
|
||||
// 请求的URL地地址
|
||||
url = new URL(
|
||||
"http://192.168.31.102:8808/AndroidServer_war_exploded/DeleteFriend");
|
||||
urlConnection = (HttpURLConnection) url.openConnection();// 打开http连接
|
||||
urlConnection.setConnectTimeout(3000);// 连接的超时时间
|
||||
urlConnection.setUseCaches(false);// 不使用缓存
|
||||
// urlConnection.setFollowRedirects(false);是static函数,作用于所有的URLConnection对象。
|
||||
urlConnection.setInstanceFollowRedirects(true);// 是成员函数,仅作用于当前函数,设置这个连接是否可以被重定向
|
||||
urlConnection.setReadTimeout(3000);// 响应的超时时间
|
||||
urlConnection.setDoInput(true);// 设置这个连接是否可以写入数据
|
||||
urlConnection.setDoOutput(true);// 设置这个连接是否可以输出数据
|
||||
urlConnection.setRequestMethod("POST");// 设置请求的方式
|
||||
urlConnection.setRequestProperty("Content-Type",
|
||||
"application/json;charset=UTF-8");// 设置消息的类型
|
||||
urlConnection.connect();// 连接,从上述至此的配置必须要在connect之前完成,实际上它只是建立了一个与服务器的TCP连接
|
||||
JSONObject json = new JSONObject();// 创建json对象
|
||||
json.put("number", URLEncoder.encode(number, "UTF-8"));// 把数据put进json对象中
|
||||
json.put("friend",URLEncoder.encode(friend, "UTF-8"));
|
||||
String jsonstr = json.toString();// 把JSON对象按JSON的编码格式转换为字符串
|
||||
// ------------字符流写入数据------------
|
||||
OutputStream out = urlConnection.getOutputStream();// 输出流,用来发送请求,http请求实际上直到这个函数里面才正式发送出去
|
||||
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(out));// 创建字符流对象并用高效缓冲流包装它,便获得最高的效率,发送的是字符串推荐用字符流,其它数据就用字节流
|
||||
bw.write(jsonstr);// 把json字符串写入缓冲区中
|
||||
bw.flush();// 刷新缓冲区,把数据发送出去,这步很重要
|
||||
out.close();
|
||||
bw.close();// 使用完关闭
|
||||
Log.i("aa", urlConnection.getResponseCode()+"");
|
||||
//以下判斷是否訪問成功,如果返回的状态码是200则说明访问成功
|
||||
if (urlConnection.getResponseCode() == HttpURLConnection.HTTP_OK) {// 得到服务端的返回码是否连接成功
|
||||
// ------------字符流读取服务端返回的数据------------
|
||||
InputStream in = urlConnection.getInputStream();
|
||||
BufferedReader br = new BufferedReader(
|
||||
new InputStreamReader(in));
|
||||
String str = null;
|
||||
StringBuffer buffer = new StringBuffer();
|
||||
while ((str = br.readLine()) != null) {// BufferedReader特有功能,一次读取一行数据
|
||||
System.out.println("测试:" + str);
|
||||
buffer.append(str);
|
||||
}
|
||||
in.close();
|
||||
br.close();
|
||||
JSONObject rjson = new JSONObject(buffer.toString());
|
||||
boolean result = rjson.getBoolean("j1");// 从rjson对象中得到key值为"json"的数据,这里服务端返回的是一个boolean类型的数据
|
||||
//如果服务器端返回的是true,则说明注册成功,否则注册失败
|
||||
if (result) {// 判断结果是否正确
|
||||
//在Android中http请求,必须放到线程中去作请求,但是在线程中不可以直接修改UI,只能通过hander机制来完成对UI的操作
|
||||
myhander.sendEmptyMessage(1);
|
||||
list.remove(i);
|
||||
Log.i("用户:", "删除好友成功");
|
||||
} else {
|
||||
myhander.sendEmptyMessage(2);
|
||||
Log.i("用户:", "删除好友失败");
|
||||
}
|
||||
} else {
|
||||
myhander.sendEmptyMessage(2);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
Log.i("aa", e.toString());
|
||||
myhander.sendEmptyMessage(2);
|
||||
} finally {
|
||||
urlConnection.disconnect();// 使用完关闭TCP连接,释放资源
|
||||
}
|
||||
}
|
||||
|
||||
// 1.编写一个发送请求的方法
|
||||
// 发送请求的主要方法
|
||||
public void httpUrlConnPost(String number) {
|
||||
|
||||
24
app/src/main/res/menu/sample_menu.xml
Normal file
24
app/src/main/res/menu/sample_menu.xml
Normal file
@@ -0,0 +1,24 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<menu xmlns:android="http://schemas.android.com/apk/res/android">
|
||||
|
||||
<item
|
||||
android:id="@+id/showFriend"
|
||||
android:icon="@mipmap/ic_launcher"
|
||||
android:title="查看好友"/>
|
||||
|
||||
<item
|
||||
android:id="@+id/deleteFriend"
|
||||
android:icon="@mipmap/ic_launcher"
|
||||
android:title="删除好友"/>
|
||||
|
||||
<item
|
||||
android:id="@+id/chatFriend"
|
||||
android:icon="@mipmap/ic_launcher"
|
||||
android:title="聊天"/>
|
||||
|
||||
<item
|
||||
android:id="@+id/blockFriend"
|
||||
android:icon="@mipmap/ic_launcher"
|
||||
android:title="拉黑"/>
|
||||
|
||||
</menu>
|
||||
Reference in New Issue
Block a user