Android 聯網頻道管理+資料庫
介面1
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical">
<ScrollView android:layout_width="match_parent" android:layout_height="wrap_content" android:scrollbars="none">
<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical">
<RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content">
<Button android:id="@+id/btn_finish" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:text="完成" /> </RelativeLayout>
<RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="10dp">
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="我的頻道" />
<TextView android:id="@+id/txt_edit" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:text="編輯" />
</RelativeLayout>
<com.bwie.channelmanager.MyGridView android:id="@+id/gv_my_channel" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:layout_marginRight="10dp" android:horizontalSpacing="10dp" android:numColumns="4" android:verticalSpacing="10dp"> </com.bwie.channelmanager.MyGridView>
<TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="10dp" android:text="頻道推薦" />
<com.bwie.channelmanager.MyGridView android:id="@+id/gv_recommond_channel" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:layout_marginRight="10dp" android:horizontalSpacing="10dp" android:numColumns="4" android:verticalSpacing="10dp"></com.bwie.channelmanager.MyGridView> </LinearLayout>
</ScrollView>
</LinearLayout>
介面2 按鈕
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical">
<Button android:id="@+id/btn_add" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="新增" />
</LinearLayout>
介面3 展示介面
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content">
<TextView android:id="@+id/txt_my_channel" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_centerInParent="true" android:layout_margin="10dp" android:background="@drawable/bg_gray" android:gravity="center" />
<ImageView android:id="@+id/btn_delete" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_alignParentTop="true" android:src="@drawable/img_delete" android:visibility="gone" />
</RelativeLayout>
介面4 新增介面
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/ll_add" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@drawable/bg_white" android:gravity="center" android:orientation="horizontal">
<ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/btn_add" />
<TextView android:id="@+id/txt_recommond_channel" android:layout_width="wrap_content" android:layout_height="wrap_content" />
</LinearLayout>
程式碼 MainActivity 類
package com.bwie.channelmanager;
import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.TextView;
import com.google.gson.Gson; import com.google.gson.reflect.TypeToken;
import java.lang.reflect.Type; import java.util.List;
public class MainActivity extends AppCompatActivity { private static final String TAG = "MainActivity"; private Button btnAdd;
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btnAdd = findViewById(R.id.btn_add);
btnAdd.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(MainActivity.this, ChannelActivity.class); startActivityForResult(intent, 100); } });
}
@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data);
// 1. 獲取我的頻道的第一種方式,頻道頁面回傳過一個Json串 if (requestCode == 100 && resultCode == RESULT_OK){ String channels = data.getStringExtra("channels"); // List<Channel> Gson gson = new Gson(); Type type = new TypeToken<List<Channel>>(){}.getType(); List<Channel> channelList = gson.fromJson(channels, type); for (Channel channel : channelList) { Log.i(TAG, "我的頻道-----"+channel.getId()+"---"+channel.getName()); } } }
@Override protected void onResume() { super.onResume(); // 2.獲取我的頻道的第二種方式,從資料庫中讀出儲存的資料 ChannelDao dao = new ChannelDao(this); List<Channel> channels = dao.queryAll(); for (Channel channel : channels) { Log.i(TAG, "我的頻道-----"+channel.getId()+"---"+channel.getName()); } } }製作dao 類
package com.bwie.channelmanager;
import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.Button; import android.widget.GridView; import android.widget.TextView;
import com.bwie.channelmanager.adapter.MyChannelAdapter; import com.bwie.channelmanager.adapter.RecommondChannelAdapter; import com.google.gson.Gson;
import java.util.ArrayList; import java.util.List;
public class ChannelActivity extends AppCompatActivity implements View.OnClickListener { private TextView txtEdit; private MyGridView gvMyChannel; private MyGridView gvRecommondChannel; private Button btnFinish;
// 我的頻道 private List<Channel> myChannels; // 推薦頻道 private List<Channel> recommondChannels;
private MyChannelAdapter myChannelAdapter; private RecommondChannelAdapter recommondChannelAdapter;
// 當前是否是編輯狀態 private boolean isEdited = false;
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_channel);
initView(); initData(); setListener(); }
private void setListener() { txtEdit.setOnClickListener(this); // 推薦列表的點選事件 gvRecommondChannel.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { // 獲取到當前點選的條目 Channel channel = recommondChannels.get(position); // 新增到我的頻道中 myChannels.add(channel); myChannelAdapter.notifyDataSetChanged(); // 移除下面的條目 recommondChannels.remove(position); recommondChannelAdapter.notifyDataSetChanged(); } });
btnFinish.setOnClickListener(this); }
private void initView() { txtEdit = findViewById(R.id.txt_edit); gvMyChannel = findViewById(R.id.gv_my_channel); gvRecommondChannel = findViewById(R.id.gv_recommond_channel); btnFinish = findViewById(R.id.btn_finish); }
private void initData() { myChannels = new ArrayList<>(); recommondChannels = new ArrayList<>();
addData();
myChannelAdapter = new MyChannelAdapter(this, myChannels); // 4. 初始化介面例項方法 MyChannelAdapter.OnDeleteItemClickListener listener = new MyChannelAdapter.OnDeleteItemClickListener() { @Override public void onDeleteClick(int position) { // 一點選刪除按鈕就會回撥到這個方法並且傳回點選的刪除按鈕的position // 獲取點選的條目 Channel channel = myChannels.get(position); // 新增到推薦的列表中 recommondChannels.add(channel); recommondChannelAdapter.notifyDataSetChanged(); // 我的頻道中刪除這個條目 myChannels.remove(position); myChannelAdapter.notifyDataSetChanged(); } }; // 5. 呼叫初始化介面例項的方法 myChannelAdapter.setOnDeleteItemClickListener(listener);
gvMyChannel.setAdapter(myChannelAdapter);
recommondChannelAdapter = new RecommondChannelAdapter(this, recommondChannels); gvRecommondChannel.setAdapter(recommondChannelAdapter);
}
private void addData() { Channel channel1 = new Channel(1, "關注"); Channel channel2 = new Channel(2, "推薦"); Channel channel3 = new Channel(3, "熱點"); Channel channel4 = new Channel(4, "科技"); Channel channel5 = new Channel(5, "視訊"); Channel channel6 = new Channel(6, "數碼"); Channel channel7 = new Channel(7, "汽車"); Channel channel8 = new Channel(8, "問答"); Channel channel9 = new Channel(9, "圖片"); Channel channel10 = new Channel(10, "段子"); Channel channel11 = new Channel(11, "電影"); Channel channel12 = new Channel(12, "美文"); Channel channel13 = new Channel(13, "社會"); Channel channel14 = new Channel(14, "娛樂"); Channel channel15 = new Channel(15, "軍事"); Channel channel16 = new Channel(16, "國際"); Channel channel17 = new Channel(17, "體育"); Channel channel18 = new Channel(18, "財經"); Channel channel19 = new Channel(19, "特賣"); Channel channel20 = new Channel(20, "直播"); Channel channel21 = new Channel(21, "房產"); Channel channel22 = new Channel(22, "時尚"); Channel channel23 = new Channel(23, "歷史"); Channel channel24 = new Channel(24, "旅遊");
myChannels.add(channel1); myChannels.add(channel2); myChannels.add(channel3); myChannels.add(channel4); myChannels.add(channel5); myChannels.add(channel6); myChannels.add(channel7); myChannels.add(channel8); myChannels.add(channel9); myChannels.add(channel10); myChannels.add(channel11); myChannels.add(channel12); myChannels.add(channel13); myChannels.add(channel14); myChannels.add(channel15); myChannels.add(channel16); myChannels.add(channel17); myChannels.add(channel18); recommondChannels.add(channel19); recommondChannels.add(channel20); recommondChannels.add(channel21); recommondChannels.add(channel22); recommondChannels.add(channel23); recommondChannels.add(channel24); }
@Override public void onClick(View v) { switch (v.getId()) { case R.id.txt_edit: isEdited = !isEdited; if (isEdited) { txtEdit.setText("完成"); } else { txtEdit.setText("編輯"); }
myChannelAdapter.setEdited(isEdited); break; case R.id.btn_finish: // 1.第一種方式,使用Json傳值 // Gson gson = new Gson(); // String json = gson.toJson(myChannels); // Intent intent = new Intent(); // intent.putExtra("channels", json); // setResult(RESULT_OK, intent); // finish();
// 2. 第二種方式,點選完成時把集合新增到資料庫中 ChannelDao dao = new ChannelDao(this); dao.insert(myChannels); finish(); break; } } } adapter 類 1
package com.bwie.channelmanager.adapter;
import android.content.Context; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.ImageView; import android.widget.TextView;
import com.bwie.channelmanager.Channel; import com.bwie.channelmanager.R;
import java.util.List;
/** * Created by eric on 2018/9/18. */
public class RecommondChannelAdapter extends BaseAdapter { private Context context; private List<Channel> list;
public RecommondChannelAdapter(Context context, List<Channel> list) { this.context = context; this.list = list; }
@Override public int getCount() { return list.size(); }
@Override public Object getItem(int position) { return list.get(position); }
@Override public long getItemId(int position) { return position; }
@Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder holder = null; if (convertView == null) { holder = new ViewHolder(); convertView = View.inflate(context, R.layout.item_recommond_channel, null); holder.txtName = convertView.findViewById(R.id.txt_recommond_channel); convertView.setTag(holder); } else { holder = (ViewHolder) convertView.getTag(); } holder.txtName.setText(list.get(position).getName()); return convertView; }
class ViewHolder { TextView txtName; } } adapter 類2
package com.bwie.channelmanager.adapter;
import android.content.Context; import android.util.Log; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.ImageView; import android.widget.TextView;
import com.bwie.channelmanager.Channel; import com.bwie.channelmanager.R;
import java.util.List;
/** * Created by eric on 2018/9/18. */
public class MyChannelAdapter extends BaseAdapter { private static final String TAG = "MyChannelAdapter"; // 1. 提供一個介面 public interface OnDeleteItemClickListener { void onDeleteClick(int position); }
// 2. 提供一個介面的例項 private OnDeleteItemClickListener listener;
// 3. 提供一個初始化例項的方法 public void setOnDeleteItemClickListener(OnDeleteItemClickListener listener) { this.listener = listener; }
private Context context; private List<Channel> list;
private boolean isEdited = false;
public MyChannelAdapter(Context context, List<Channel> list) { this.context = context; this.list = list; }
public void setEdited(boolean edited) { isEdited = edited; notifyDataSetChanged(); }
@Override public int getCount() { return list.size(); }
@Override public Object getItem(int position) { return list.get(position); }
@Override public long getItemId(int position) { return position; }
@Override public View getView(final int position, View convertView, ViewGroup parent) { ViewHolder holder = null; if (convertView == null) { holder = new ViewHolder(); convertView = View.inflate(context, R.layout.item_my_channel, null); holder.txtName = convertView.findViewById(R.id.txt_my_channel); holder.imgDelete = convertView.findViewById(R.id.btn_delete); convertView.setTag(holder); } else { holder = (ViewHolder) convertView.getTag(); } holder.txtName.setText(list.get(position).getName()); if (isEdited) { holder.imgDelete.setVisibility(View.VISIBLE); holder.imgDelete.setVisibility(View.GONE); } } else { holder.imgDelete.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Log.i(TAG, "onClick: 點選了刪除"); // 6.合適的時機觸發介面回撥 listener.onDeleteClick(position); } }); return convertView; }
class ViewHolder { TextView txtName; ImageView imgDelete; } }
封裝 類
package com.bwie.channelmanager;
/** * Created by eric on 2018/9/18. */
public class Channel { private int id; private String name;
public Channel() { }
public Channel(int id, String name) { this.id = id; this.name = name; }
public int getId() { return id; }
public void setId(int id) { this.id = id; }
public String getName() { return name; }
public void setName(String name) { this.name = name; } }
資料庫
package com.bwie.channelmanager;
import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper;
/** * Created by eric on 2018/9/18. */
public class DBHelper extends SQLiteOpenHelper {
public DBHelper(Context context) { super(context, "channel.db", null, 1); }
@Override public void onCreate(SQLiteDatabase db) { String sql = "create table channel(id integer primary key, name text)"; db.execSQL(sql); }
@Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
} } 繼承 佈局
package com.bwie.channelmanager;
import android.content.Context; import android.util.AttributeSet; import android.widget.GridView;
/** * Created by eric on 2018/9/18. */
public class MyGridView extends GridView { public MyGridView(Context context) { super(context); }
public MyGridView(Context context, AttributeSet attrs) { super(context, attrs); }
public MyGridView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); }
@Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST); super.onMeasure(widthMeasureSpec, expandSpec); } }