仿今日頭條頻道管理
阿新 • • 發佈:2019-02-04
package com.example.pingdao;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.TextView;
import com.example.pingdao.bean.channeldata;
import com.google.gson.Gson;
public class MainActivity extends Activity {
GridView gv_wd, gv_gd;
channeldata cd;
List<String> list_top;
List<String> list;
private myadapter myadapter;
private myadapter1 myadapter1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gv_gd = (GridView) findViewById(R.id.gv_gd);
gv_wd = (GridView) findViewById(R.id.gv__wd);
initdata();
myadapter = new myadapter();
myadapter1 = new myadapter1();
gv_gd.setAdapter(myadapter);
gv_wd.setAdapter(myadapter1);
}
private void initdata() {
// TODO Auto-generated method stub
new AsyncTask<String, String, String>() {
@Override
protected String doInBackground(String... params) {
// TODO Auto-generated method stub
return getdata();
}
@Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
Gson gson = new Gson();
cd = gson.fromJson(result, channeldata.class);
// Log.i("=====", cd.toString());
list_top = new ArrayList<String>();
list = new ArrayList<String>();
for (int i = 0; i < cd.getChannel().size(); i++) {
list_top.add(cd.getChannel().get(i).getChannel_me());
list.add(cd.getChannel().get(i).getChannel_more());
}
gv_gd.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
list_top.add(list.get(position));
list.remove(position);
myadapter.notifyDataSetChanged();
myadapter1.notifyDataSetChanged();
}
});
gv_wd.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
list.add(list_top.get(position));
list_top.remove(position);
myadapter.notifyDataSetChanged();
myadapter1.notifyDataSetChanged();
}
});
myadapter.notifyDataSetChanged();
myadapter1.notifyDataSetChanged();
}
}.execute();
}
class myadapter extends BaseAdapter {
@Override
public int getCount() {
// TODO Auto-generated method stub
if (list != null) {
return list.size();
}
return 0;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return list.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ViewHolder holder;
if (convertView == null) {
convertView = View.inflate(MainActivity.this, R.layout.item,
null);
holder = new ViewHolder();
holder.tv_name = (TextView) convertView
.findViewById(R.id.tv_name);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.tv_name.setText(list.get(position));
return convertView;
}
}
static class ViewHolder {
TextView tv_name;
}
class myadapter1 extends BaseAdapter {
private static final int Object = 0;
@Override
public int getCount() {
// TODO Auto-generated method stub
if (list_top != null) {
return list_top.size();
}
return 0;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return list_top.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ViewHolder1 holder1;
if (convertView == null) {
convertView = View.inflate(MainActivity.this, R.layout.item,
null);
holder1 = new ViewHolder1();
holder1.tv_name1 = (TextView) convertView
.findViewById(R.id.tv_name);
convertView.setTag(holder1);
} else {
holder1 = (ViewHolder1) convertView.getTag();
}
holder1.tv_name1.setText(list_top.get(position));
return convertView;
}
}
static class ViewHolder1 {
TextView tv_name1;
}
protected String getdata() {
// TODO Auto-generated method stub
String result = null;
try {
URL url = new URL(
"http://result.eolinker.com/iYXEPGn4e9c6dafce6e5cdd23287d2bb136ee7e9194d3e9?uri=channel ");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
int code = conn.getResponseCode();
if (code == 200) {
InputStream is = conn.getInputStream();
ByteArrayOutputStream bos = new ByteArrayOutputStream();
int len = -1;
byte[] buff = new byte[1024];
while ((len = is.read(buff)) != -1) {
bos.write(buff, 0, len);
}
result = new String(bos.toByteArray());
}
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
}
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.TextView;
import com.example.pingdao.bean.channeldata;
import com.google.gson.Gson;
public class MainActivity extends Activity {
GridView gv_wd, gv_gd;
channeldata cd;
List<String> list_top;
List<String> list;
private myadapter myadapter;
private myadapter1 myadapter1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gv_gd = (GridView) findViewById(R.id.gv_gd);
gv_wd = (GridView) findViewById(R.id.gv__wd);
initdata();
myadapter = new myadapter();
myadapter1 = new myadapter1();
gv_gd.setAdapter(myadapter);
gv_wd.setAdapter(myadapter1);
}
private void initdata() {
// TODO Auto-generated method stub
new AsyncTask<String, String, String>() {
@Override
protected String doInBackground(String... params) {
// TODO Auto-generated method stub
return getdata();
}
@Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
Gson gson = new Gson();
cd = gson.fromJson(result, channeldata.class);
// Log.i("=====", cd.toString());
list_top = new ArrayList<String>();
list = new ArrayList<String>();
for (int i = 0; i < cd.getChannel().size(); i++) {
list_top.add(cd.getChannel().get(i).getChannel_me());
list.add(cd.getChannel().get(i).getChannel_more());
}
gv_gd.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
list_top.add(list.get(position));
list.remove(position);
myadapter.notifyDataSetChanged();
myadapter1.notifyDataSetChanged();
}
});
gv_wd.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
list.add(list_top.get(position));
list_top.remove(position);
myadapter.notifyDataSetChanged();
myadapter1.notifyDataSetChanged();
}
});
myadapter.notifyDataSetChanged();
myadapter1.notifyDataSetChanged();
}
}.execute();
}
class myadapter extends BaseAdapter {
@Override
public int getCount() {
// TODO Auto-generated method stub
if (list != null) {
return list.size();
}
return 0;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return list.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ViewHolder holder;
if (convertView == null) {
convertView = View.inflate(MainActivity.this, R.layout.item,
null);
holder = new ViewHolder();
holder.tv_name = (TextView) convertView
.findViewById(R.id.tv_name);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.tv_name.setText(list.get(position));
return convertView;
}
}
static class ViewHolder {
TextView tv_name;
}
class myadapter1 extends BaseAdapter {
private static final int Object = 0;
@Override
public int getCount() {
// TODO Auto-generated method stub
if (list_top != null) {
return list_top.size();
}
return 0;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return list_top.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ViewHolder1 holder1;
if (convertView == null) {
convertView = View.inflate(MainActivity.this, R.layout.item,
null);
holder1 = new ViewHolder1();
holder1.tv_name1 = (TextView) convertView
.findViewById(R.id.tv_name);
convertView.setTag(holder1);
} else {
holder1 = (ViewHolder1) convertView.getTag();
}
holder1.tv_name1.setText(list_top.get(position));
return convertView;
}
}
static class ViewHolder1 {
TextView tv_name1;
}
protected String getdata() {
// TODO Auto-generated method stub
String result = null;
try {
URL url = new URL(
"http://result.eolinker.com/iYXEPGn4e9c6dafce6e5cdd23287d2bb136ee7e9194d3e9?uri=channel ");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
int code = conn.getResponseCode();
if (code == 200) {
InputStream is = conn.getInputStream();
ByteArrayOutputStream bos = new ByteArrayOutputStream();
int len = -1;
byte[] buff = new byte[1024];
while ((len = is.read(buff)) != -1) {
bos.write(buff, 0, len);
}
result = new String(bos.toByteArray());
}
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
}