package bwie.caipu; import android.content.Intent; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.EditText; import android.widget.GridView; import com.google.gson.Gson; import java.io.IOException; import java.util.List; import Util.OkHttpUtils; import bean.Bean; import okhttp3.Call; import okhttp3.Callback; import okhttp3.Response; public class MainActivity extends AppCompatActivity { private String time; private ArrayAdapter<Bean.TngouBean> adapter; private EditText ed; private GridView lv; private Button btn; private List<Bean.TngouBean> list; Handler handler = new Handler() { @Override public void handleMessage(Message msg) { String str = (String) msg.obj; Gson gson = new Gson(); Bean be = gson.fromJson(str, Bean.class); list = be.getTngou(); adapter = new ArrayAdapter<Bean.TngouBean>(MainActivity.this, android.R.layout.simple_list_item_1, list); lv.setAdapter(adapter); super.handleMessage(msg); } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ed = (EditText) findViewById(R.id.et); lv = (GridView) findViewById(R.id.listView); btn = (Button) findViewById(R.id.but); lv.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) { Intent intent = new Intent(MainActivity.this, Activitysec.class); intent.putExtra("id", list.get(i - 1).getId()); startActivity(intent); } }); btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { String name = ed.getText().toString(); for (Bean.TngouBean li : list) { if (name.equals(li.getName())) { Intent intent = new Intent(MainActivity.this, Activitysec.class); intent.putExtra("id", li.getId()); startActivity(intent); } } } }); getEnqueue(); } private void getEnqueue() { OkHttpUtils.get("http://www.tngou.net/api/cook/classify", new Callback() { @Override public void onFailure(Call call, IOException e) { } @Override public void onResponse(Call call, Response response) throws IOException { final String result = response.body().string(); Message msg = Message.obtain(); msg.obj = result; handler.sendMessage(msg); } }); } }
Glide.with(context) .load("http://tnfs.tngou.net/image" + tn.getImg()) .placeholder(R.mipmap.ic_launcher) .into(vh.imageView);