小诺我开发这个微商城的过程中遇到了各种问题,话说就我自己开发,反正各种不懂,既然不懂就要去查呀,慢慢的查的多了,用的多了,原理也慢慢懂了(刚开始我打算先弄懂原理的,可是发现看不懂。。。咳咳咳,所以就先用了,然后不懂得再查)
1.ListView中item中添加ImageView实现商品数量的增加与减少。开发的过程中遇到需要增加商品的数量,于是就在item布局中添加增加与减少两个图标,刚开始的时候,发现点击没效果,只是对单个的item有点击事件,然而点击加减的时候,根本没反应的说。
后来才发现需要在布局中设置android:descendantFocusability="blocksDescendants"
在ImageView中设置android:focusable="false"
然后又用到了ViewHolder来提升listview的性能,下边是list_item.xml的代码
参考实现ListView中点击ImageView的点击的博客
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:descendantFocusability="blocksDescendants" >
<ImageView
android:id="@+id/imageView"
android:layout_width="40dp"
android:layout_height="40dp"
android:src="@drawable/fruit" />
<LinearLayout
android:id="@+id/buy_number"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/imageView"
android:orientation="vertical" >
<TextView
android:id="@+id/titleTextView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Title_info"
android:textSize="12dp" />
<LinearLayout
android:id="@+id/buy_number_image"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="right|bottom"
android:orientation="horizontal" >
<TextView
android:id="@+id/descTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingRight="150dp"
android:text="Description"
android:textSize="12dp" />
<ImageView
android:id="@+id/imageView_delete"
android:layout_width="20dp"
android:layout_height="20dp"
android:focusable="false"
android:src="@drawable/delete" />
<TextView
android:id="@+id/list_buy_number"
android:layout_width="20dp"
android:layout_height="20dp"
android:text="0"
android:textSize="15sp" />
<ImageView
android:id="@+id/imageView_add"
android:layout_width="20dp"
android:layout_height="20dp"
android:focusable="false"
android:src="@drawable/add" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
ViewHolder实现优化,提升性能
public final class ViewHolder {
TextView titleTextView;
TextView descTextView;
ImageView imageView_delete;
TextView list_buy_number;
ImageView imageView_add;
}
public class MyAdapter extends BaseAdapter {
private LayoutInflater mInflater;
public MyAdapter(Context context) {
this.mInflater = LayoutInflater.from(context);
}
@Override
public int getCount() {
return mListItem.size();
}
@Override
public Object getItem(int position) {
return mListItem.get(position);
}
@Override
public long getItemId(int position) {
return 0;
}
@SuppressLint("InflateParams") @Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
Map<String, Object> item = new HashMap<String, Object>();
item = mListItem.get(position);//获得所有数据
ViewHolder holder = null;
if (convertView == null) {
holder = new ViewHolder();
convertView = mInflater.inflate(R.layout.list_item, null);
holder.titleTextView = (TextView) convertView
.findViewById(R.id.titleTextView);
holder.descTextView = (TextView) convertView
.findViewById(R.id.descTextView);
holder.imageView_delete = (ImageView) convertView
.findViewById(R.id.imageView_delete);
holder.list_buy_number = (TextView) convertView
.findViewById(R.id.list_buy_number);
holder.imageView_add = (ImageView) convertView
.findViewById(R.id.imageView_add);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.titleTextView.setText(item.get("titleTextView").toString());
holder.descTextView.setText(item.get("descTextView").toString());
//点击添加和减少商品的图标
holder.imageView_delete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getActivity(), "delete user", Toast.LENGTH_SHORT).show();
}
});
holder.imageView_add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getActivity(), "add user", Toast.LENGTH_SHORT).show();
}
});
return convertView;
}
}
}
2.下边是tapTwo.java实现代码,添加解析JSON数据的功能,从网页获取JSON数据进行解析,以前只是看过解析如何实现的,但是发现实现的时候出现了不少的问题,JSON解析有很多种方式,我选择了比较简单的方式。具体用法,可以自己多查资料,我参考的是郭霖大神的第一行代码上边的实现方式,下面将我的具体实现代码列出来,给大家提供思路。(解析JSON数据的时候,可以自己定义好一个JSON数据,然后试试能不能解析到,然后再从服务器获取数据进行测试)
我的测试做的比较成功,但是在添加到自己的项目中的时候出现了很多问题,数据的获取和适配器的链接,调了半天的bug,最后在公司老大的指导下,(竟然没有实例化,我哭会。。。)
JSON数据的处理过程中,需要开辟线程从网络获取数据,然后处理数据的时候,需要用到Handler进行处理信息(例如:线程中不能实现UI。。。具体细节,大家多动手,多查查资料。)
private void parseJSONWithJSONObject(String jsonData) {
try {
//String jsonData="[{'id':'5','version':'5.5','name':'Angry Birds'},{'id':'6','version':'7.0','name':'Angry Dog'}]";
//String jsonData="http://www.xxxjqt.cn/amallphone/index.php/Home/Mobile/";
JSONArray jsonArray = new JSONArray(jsonData);
Map<String, Object> map ;
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
map = new HashMap<String, Object>();
/*String id = jsonObject.getString("id");
String name = jsonObject.getString("name");
String version = jsonObject.getString("version");*/
String id = jsonObject.getString("gid");
String name = jsonObject.getString("price");
String version = jsonObject.getString("original_price");
map.put("titleTextView", id);
map.put("descTextView", name);
mListItem.add(map);
Log.d("MainActivity", "id is " + id);
Log.d("MainActivity", "name is " + name);
Log.d("MainActivity", "version is " + version);
}
Toast.makeText(getActivity(), mListItem.size(), Toast.LENGTH_SHORT).show();
} catch (Exception e) {
e.printStackTrace();
}
}
private void sendRequestWithHttpClient() {
new Thread(new Runnable() {
@Override
public void run() {
try {
HttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(
"http://www.xxxjqt.cn/amallphone/index.php/Home/Mobile/");
HttpResponse httpResponse = httpClient.execute(httpGet);
if (httpResponse.getStatusLine().getStatusCode() == 200) {
HttpEntity entity = httpResponse.getEntity();
String response = EntityUtils.toString(entity, "utf-8");
//parseJSONWithJSONObject(response);
Message message = new Message();
message.what = 1;
message.obj = response.toString();
handler.sendMessage(message);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
}
线程很重要,这个必须要会
private Handler handler = new Handler() {
public void handleMessage(Message msg) {
switch (msg.what) {
case 1:
String response = (String) msg.obj;
parseJSONWithJSONObject(response);
//Toast.makeText(getActivity(), response, Toast.LENGTH_SHORT).show();
MyAdapter adapter = new MyAdapter(getActivity());
mListView.setAdapter(adapter);
mListView_1.setAdapter(adapter);
// 设置进入选购中心显示的商品界面,修复进入界面是加载全部的信息问题
mListView.setVisibility(View.INVISIBLE);
mListView_1.setVisibility(View.INVISIBLE);
// getActivity使用在适配器中适用于Fragment中
ArrayAdapter<String> typeAdapter = new ArrayAdapter<String>
(getActivity(), android.R.layout.simple_list_item_1, data);
lv_type.setAdapter(typeAdapter);
// 分类适配器类别添加点击事件
lv_type.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// 对点击位置进行判断,添加事件
if (data[position].equals("热卖专区")) {
mListView.setVisibility(View.VISIBLE);
mListView_1.setVisibility(View.GONE);
}
if (data[position].equals("进口精品")) {
mListView.setVisibility(View.GONE);
mListView_1.setVisibility(View.VISIBLE);
}
if (data[position].equals("国产优选")) {
mListView.setVisibility(View.GONE);
mListView_1.setVisibility(View.GONE);
}
if (data[position].equals("森林干果")) {
mListView.setVisibility(View.VISIBLE);
mListView_1.setVisibility(View.GONE);
}
if (data[position].equals("礼盒整箱")) {
mListView.setVisibility(View.GONE);
mListView_1.setVisibility(View.GONE);
}
if (data[position].equals("新鲜果汁")) {
mListView.setVisibility(View.VISIBLE);
mListView_1.setVisibility(View.GONE);
}
}
});
break;
}
}
};