You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

253 lines
10 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package com.example.SearchCenter;
import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import com.example.Util.ConditionCode;
import com.example.Util.ReturnData;
import com.example.Util.cmkgWebServiceGet;
import com.example.Util.publicStringUtil;
import com.example.cmknowledgegraph.R;
import com.loopeer.cardstack.AllMoveDownAnimatorAdapter;
import com.loopeer.cardstack.CardStackView;
import com.loopeer.cardstack.UpDownAnimatorAdapter;
import com.loopeer.cardstack.UpDownStackAnimatorAdapter;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;
public class SearchEntryActivity extends AppCompatActivity implements CardStackView.ItemExpendListener,ConditionCode{
public static ArrayList<String> content=new ArrayList<String>();//存储数据内容
public static String model_Text;
public static int[] index=new int[3];//在随机生成选定联想搜索产生的索引值
Integer[] color = {
R.color.holo_blue_bright,
R.color.holo_orange_light,
R.color.holo_purple,
R.color.holo_red_light,
R.color.holo_blue_dark,
R.color.holo_orange_dark,
R.color.holo_blue_dark,
R.color.holo_red_dark,
R.color.holo_orange_light,
R.color.holo_red_dark
};
private CardStackView mStackView;
private LinearLayout mActionButtonContainer;
private CMKGStackAdapter cmkgStackAdapter;
Integer[] new_color;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search_entry);
Intent intent = getIntent();
Bundle bundle = intent.getExtras();
init(bundle);
}
public void onItemExpend(boolean expend) {
}
void init(Bundle bundle)
{
List<List<ContentData>> lists=new LinkedList<>();
//获取response后将这个json数据解析
ReturnData response = (ReturnData) bundle.getSerializable("msg");
System.out.println("code=!!!!!!!!1"+response.getCode());
Log.i("response+++++",response+"返回值");
try {
int code = response.getCode();
Log.i("code:",""+code);
String message = response.getMsg();
Log.i("msg:",""+message);
if(code == DiseaseToSymptom){
/**
* //返回的是病症
* 标题就是病的名字内容就是症状。lists就是病名list就是病症
*/
List<ContentData> list = new LinkedList<>();
list.add(new ContentData((String)response.getData()));
Log.i("response.getData",(String)response.getData());
lists.add(list);
new_color=new Integer[1];
}else if(code==DiseaseToMedicines){
/**
* //返回的是结解决的多种药品方法
* 标题就是药品的名字,内容就是用法,暂时不做用法(用法先用数字代替)
*/
Log.i("hah",""+(String)response.getData());
JSONArray jsonArray = new JSONArray((String)response.getData());
for(int i=0;i<jsonArray.length();i++){
List<ContentData> list=new LinkedList<>();
list.add(new ContentData("我是您的用法哦~请您看看我嘞\n我无比的期待您的光临嘞\n我们没有连数据呢马上就有嘞"));
content.add((String) jsonArray.get(i));
lists.add(list);
}
new_color=new Integer[jsonArray.length()];
}else if(code==MedicineToOperation) {
/**
* //返回的是药的用法
* //标题就是药品的名字,内容是用法
*/
List<ContentData> list = new LinkedList<>();
list.add(new ContentData((String)response.getData()));
lists.add(list);
new_color=new Integer[1];
}else{
/**
* //返回的是药的主治症状和对应的
* 标题是药的名字和“可以治疗的疾病”,内容是症状,和十种药物
*/
List<ContentData> list = new LinkedList<>();
String data=(String)response.getData();
JSONObject jo=new JSONObject(data);
String medicine_symptom=jo.getString("symptom");
list.add(new ContentData(medicine_symptom));
lists.add(list);
String disease_names=new String(jo.getString("name"));
JSONArray ja=new JSONArray(disease_names);
for(int i=0;i<ja.length();i++){
List<ContentData> list_1=new LinkedList<>();
list_1.add(new ContentData(content.get(i)));
lists.add(list);
}
new_color=new Integer[1];
}
for(int i=0;i<new_color.length;i++){
new_color[i]=color[i];
}
} catch (JSONException e) {
e.printStackTrace();
}
CardStackView mCardStack = (CardStackView)findViewById(R.id.cardStackView);
CMKGStackAdapter adapter = new CMKGStackAdapter(this,response);
mCardStack.setAdapter(adapter);
mCardStack.setItemExpendListener(this);
/**
* 这里分很多种情况,目前有四种模板,每种模板对应的标题内容可能有些不同
* 为了方便我们先不删除原有模板的items我们点开之后有几种情况
* 病-症状:标题是名字,内容是症状
* 病-治疗药品:标题是药的名字,内容是药的用法(暂时不做)
* 药-用法:标题是药的名字,内容是药的用法
* 药-作用:标题是药的名字和 “可以治疗的疾病”,内容是药的用法
*
* 每种不同的情况对应不同的状态码,用接口实现状态码
*/
mCardStack.setAnimatorAdapter(new AllMoveDownAnimatorAdapter(mCardStack));
mCardStack.setAnimatorAdapter(new UpDownAnimatorAdapter(mCardStack));
mCardStack.setAnimatorAdapter(new UpDownStackAnimatorAdapter(mCardStack));
/**
* 从这里开始是联想搜索
* 病——症状 联想搜索nm能吃什么药
* 病——药 俩像搜索nhm能指什么病nhm能治什么病nhm能治什么病
* 药——操作方法 联想搜索nhm能治什么病 nhm的证候nhm的功能
* 药——作用 联想搜索nhm能治什么病nhm的证候nhm的功能
*/
TextView asscoiate_text=findViewById(R.id.asscoiate_text);//显示文本“你可能感兴趣”
String[] texts=new String[3];//存放listView中的显示的数据
if(response.getCode()==DiseaseToSymptom){
/**
* 病——症状 nm能吃什么药
*/
texts[0]=response.getMsg()+"应该吃什么药";
model_Text=Model_Symptom;
}else if(response.getCode()==DiseaseToMedicines){
/**
* 病——药 nhm能指什么病nhm能治什么病nhm能治什么病
* 随机选三种药品
*/
Random r=new Random();
for(int i=0;i<3;i++) {
index[i] = r.nextInt(9) + 1;
texts[i]=content.get(index[i])+"能治什么病症";
}
model_Text=Model_Function;
}else if(response.getCode()==MedicineToOperation){
/**
* 药——操作方法
* nhm能治什么病
*/
texts[0]=response.getMsg()+"能治什么病";
model_Text=Model_Function;
}else {
/**
*药——作用,能治什么病
* nhm 操作方法
*/
texts[0]=response.getMsg()+"的用法";
model_Text=Model_Operation;
}
ArrayAdapter<String> view_adapter = new ArrayAdapter<String>(SearchEntryActivity.this,R.layout.list_view, texts);
ListView listView=findViewById(R.id.listview_group);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
new Thread(new Runnable() {
@Override
public void run() {
ReturnData rd=null;
if(response.getCode()==DiseaseToMedicines) {
rd = cmkgWebServiceGet.executeHttpGet(publicStringUtil.questionArgsname + content.get(index[position]) + model_Text, publicStringUtil.questionSearchURL);
}
else{
rd= cmkgWebServiceGet.executeHttpGet(publicStringUtil.questionArgsname+response.getMsg()+model_Text, publicStringUtil.questionSearchURL);
}
Message m = new Message();
Bundle bundle = new Bundle();
bundle.putSerializable("msg", rd);
Intent intent = new Intent();
intent.setClass(SearchEntryActivity.this, SearchEntryActivity.class);
intent.putExtras(bundle);
startActivity(intent);
}
}).start();
}
});
listView.setAdapter(view_adapter);
adapter.updateData(Arrays.asList(new_color),lists);
}
}