您的位置:首页 > 移动开发 > Android开发

ListView嵌套ListView

2016-03-28 18:20 387 查看
listStockAdapter = new ListStockAdapter(linkedPlansBeans);
lv_detailstrategy.setAdapter(listStockAdapter);

/**

* @Description 股票适配器
* @author jaily.zhang
* @date 2014-9-9 下午3:39:03
* @version V1.3.1
*/
class ListStockAdapter extends BaseAdapter {

private LayoutInflater mInflater;
private List<PlansBean> listPlansBeans;

public ListStockAdapter(List<PlansBean> listPlansBeans) {
mInflater = getLayoutInflater();
this.listPlansBeans = listPlansBeans;
}

@Override
public int getCount() {
// TODO Auto-generated method stub
return listPlansBeans.size();
}

@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return listPlansBeans.get(position);
}

@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
try {
NestedListview v = null;
if (convertView == null) {
v = new NestedListview(getApplicationContext(), listPlansBeans.get(position));
convertView = v.getView();
convertView.setTag(v.getView());
} else {
v = new NestedListview(getApplicationContext(), listPlansBeans.get(position));
convertView = v.getView();
}
} catch (Exception e) {
// TODO: handle exception
Tools.toastShow(getApplicationContext(), getResources().getString(R.string.datafail));
}

return convertView;

}

}

/**

* @Description 嵌套listView
* @author jaily.zhang
* @date 2015-1-9 下午12:57:17
* @version V1.3.1
*/
class NestedListview {

private View myview;
private PlansBean plansBean;
private Context mycontext;
private MyListView mylistview;
private NestedAdapter adapter;
private TextView tv_date;
private TextView tv_name;
private LinearLayout layout_none;

public NestedListview(Context context, PlansBean listData) {
this.mycontext = context;
this.plansBean = listData;
if (listData.getPlan() != null)
this.adapter = new NestedAdapter(context, listData.getPlan());
InitView();
}

private void InitView() {
// TODO Auto-generated method stub
try {
myview = LayoutInflater.from(mycontext).inflate(R.layout.nestedlistviewlayout, null);
mylistview = (MyListView) myview.findViewById(R.id.list_item);
layout_nested = (LinearLayout) myview.findViewById(R.id.layout_nested);
tv_date = (TextView) myview.findViewById(R.id.tv_date);
tv_name = (TextView) myview.findViewById(R.id.tv_name);
layout_none = (LinearLayout) myview.findViewById(R.id.layout_none);

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date now = new Date(System.currentTimeMillis());
if (plansBean.getDate() != null) {
try {
Date date = sdf.parse(plansBean.getDate());
long day = (now.getTime() - date.getTime()) / (24 * 60 * 60 * 1000);

String[] strings = plansBean.getDate().split("-");
tv_date.setText(strings[1] + "月" + strings[2] + "日");

if (day == 0) {
tv_name.setText("今日推荐");
} else if (day == 1) {
tv_name.setText("昨日推荐");
} else {
tv_name.setText("推荐股票");
}
} catch (Exception e) {

}
}
if (plansBean.getPlan() != null) {
layout_none.setVisibility(View.GONE);
mylistview.setDivider(null);
mylistview.setAdapter(adapter);
tv_date.setVisibility(View.VISIBLE);
tv_name.setVisibility(View.VISIBLE);
layout_nested.setVisibility(View.VISIBLE);
} else {
tv_date.setVisibility(View.GONE);
tv_name.setVisibility(View.GONE);
layout_none.setVisibility(View.GONE);
layout_nested.setVisibility(View.GONE);
}

} catch (Exception e) {
// TODO: handle exception
Tools.toastShow(getApplicationContext(), getResources().getString(R.string.datafail));
}

}

public View getView() {
// TODO Auto-generated method stub
return myview;
}

/**

* @Description 嵌套适配器
* @author jaily.zhang
* @date 2015-1-9 下午2:50:36
* @version V1.3.1
*/
class NestedAdapter extends BaseAdapter {

Context mycontext;
List<PlanBean> mylist;

public NestedAdapter(Context context, List<PlanBean> list) {
mycontext = context;
mylist = list;
}

@Override
public int getCount() {
// TODO Auto-generated method stub
return
d7b4
mylist.size();
}

@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return mylist.get(arg0);
}

@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return arg0;
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ViewHolder vh = null;
if (convertView == null) {
convertView = LayoutInflater.from(mycontext).inflate(R.layout.strategystock_detailnews, null);
vh = new ViewHolder();

vh.tv_ar = (TextView) convertView.findViewById(R.id.tv_ar);
vh.tv_stockname = (TextView) convertView.findViewById(R.id.tv_stockname);
vh.tv_stockcode = (TextView) convertView.findViewById(R.id.tv_stockcode);
vh.tv_acname = (TextView) convertView.findViewById(R.id.tv_acname);
vh.tv_addstock = (TextView) convertView.findViewById(R.id.tv_addstock);
vh.tv_d = (TextView) convertView.findViewById(R.id.tv_d);
vh.layout_shouyi = (LinearLayout) convertView.findViewById(R.id.layout_shouyi);
vh.stockdetail_layout = (LinearLayout) convertView.findViewById(R.id.stockdetail_layout);
vh.layout_add = (LinearLayout) convertView.findViewById(R.id.layout_add);
vh.strategy_listinfos_ll = (LinearLayout) convertView.findViewById(R.id.strategy_listinfos_ll);
convertView.setTag(vh);
} else {
vh = (ViewHolder) convertView.getTag();
}

try {
final PlanBean planBean = mylist.get(position);
// 累计收益率
if (planBean.getAr() != null) {

if (planBean.getAr() > 2) {
vh.layout_shouyi.setBackgroundColor(getResources().getColor(R.color.c359d3));
} else if (planBean.getAr() > 0 && planBean.getAr() < 2) {
vh.layout_shouyi.setBackgroundColor(getResources().getColor(R.color.fce));
} else {
vh.layout_shouyi.setBackgroundColor(getResources().getColor(R.color.fcae51));
}
vh.tv_ar.setText(planBean.getAr() + "");
} else
vh.tv_ar.setText("--");
// 股票名称
if (planBean.getCpname() != null) {
vh.tv_stockname.setText(planBean.getCpname().getSzh());
} else {
vh.tv_stockname.setText("--");
}
// 股票code
vh.tv_stockcode.setText("[" + planBean.getCode().split("_")[0] + "]");
// 股票内容
if (planBean.getAcname() != null) {
// 截掉冒号
String acname = planBean.getAcname();
String isjudge = ":";
if (acname.contains(isjudge)) {
String acnameStr = acname.substring(acname.indexOf(":") + 1, acname.length());
vh.tv_acname.setText(acnameStr);
} else {
vh.tv_acname.setText(planBean.getAcname());
}

} else {

}
if (planBean.getTag() != null) {
// vh.iv_tag.setVisibility(View.VISIBLE);
// vh.tv_tag.setText(planBean.getTag().get(0));
List<String> listString = new ArrayList<>();
for (int i = 0; i < planBean.getTag().size(); i++)
listString.add(planBean.getTag().get(i));
setStrategyInfos(listString, vh.strategy_listinfos_ll);
} else {
// vh.iv_tag.setVisibility(View.GONE);
}
if (planBean.getD() != null)
vh.tv_d.setText("建议" + planBean.getD() + "天后卖出");

final LinearLayout layout = vh.stockdetail_layout;
vh.stockdetail_layout.setOnClickListener(new OnClickListener() {

// @Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
stockBean = new StockBean(planBean.getCode(), planBean.getCpname().getSzh(), "", "");
Map<String, String> map = new HashMap<String, String>();
map.put(Constant.SECU, planBean.getCode());
List<Map<String, String>> list = new ArrayList<Map<String, String>>();
list.add(map);
// 根据公司code 获取行业信息
new MyGetIndustryInfoAsyncTask().execute(Constant.COMPANY_INDUSTRY, list, planBean.getCpname());

}
});
final Map<Integer, Boolean> mapselect = new HashMap<>();
mapselect.put(position, false);
for (int i = 0; i < listSearchValues.size(); i++) {
if (listSearchValues.get(i).getCode().equals(planBean.getCode())) {
// vh.tv_addstock.setText(DELESTOCK);
vh.tv_addstock.setBackgroundDrawable(getResources().getDrawable(R.drawable.found_btn_added));
mapselect.put(position, true);
break;
} else {
mapselect.put(position, false);
vh.tv_addstock.setBackgroundDrawable(getResources().getDrawable(R.drawable.found_btn_addnew));
// vh.tv_addstock.setText(ADDSTOCK);
}
}
final TextView tv_addstock = vh.tv_addstock;

vh.layout_add.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Value = (PlanBean) getItem(position);
mapselect.put(position, !mapselect.get(position));
if (isloginuser) {
if (!mapselect.get(position)) {

} // 添加自选股
else {
tv_addstock.setBackgroundDrawable(getResources().getDrawable(R.drawable.found_btn_added));
Map<String, String> map = new HashMap<String, String>();
map.put(Constant.USERID, userid);
Map<String, String> secumap = new HashMap<>();
secumap.put(Constant.SECU, Value.getCode());
Map<String, String> typemap = new HashMap<>();
typemap.put(Constant.TYPE, Constant.ADD);

List<Map<String, String>> list = new ArrayList<>();
list.add(map);
list.add(secumap);
list.add(typemap);
// 添加股票的异步任务
new MyAddStockAsyncTask().execute(Constant.COMPANY_UPDATE, list);
}
} else {
// 删除自选股
if (!mapselect.get(position)) {

}
// 添加自选股
else {
Cursor cursor = dbHelper.getReadableDatabase().rawQuery("select * from selectstock where userid = ?", new String[] { userid });
listSearchValues = Tools.converCursorToList(cursor);
for (int i = 0; i < listSearchValues.size(); i++) {

if (Value.getCode().equals(listSearchValues.get(i).getCode())) {
flag = false;
break;
}

}

if (flag) {
tv_addstock.setBackgroundDrawable(getResources().getDrawable(R.drawable.found_btn_added));
dbHelper.getReadableDatabase().execSQL("insert into selectstock values(null,?,?,?)", new String[] { userid, Value.getCode(), Value.getCpname().getSzh() });
Cursor cursornew = dbHelper.getReadableDatabase().rawQuery("select * from sortselectstock where userid = ?", new String[] { userid });
if (cursornew.getCount() > 0) {
String s = "";
while (cursornew.moveToNext()) {

s = cursornew.getString(2);

}
if (!s.contains(Value.getCode())) {
dbHelper.getReadableDatabase().delete("sortselectstock", "userid=?", new String[] { userid });
s = (Value.getCode() + ",")+s;
//
dbHelper.getReadableDatabase().execSQL("insert into sortselectstock values(null,?,?)", new String[] { userid, s });
}

}

}

}

}

}
});
} catch (Exception e) {
// TODO: handle exception
Tools.toastShow(getApplicationContext(), getResources().getString(R.string.datafail));
}

return convertView;

}
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android