抱歉我的英语不好......这是数据视图活动的一部分
DataView活动:
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
/*
* StrictMode is most commonly used to catch accidental disk or network
* access on the application's main thread
*/
StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
.detectDiskReads().detectDiskWrites().detectNetwork()
.penaltyLog().build());
super.onCreate(savedInstanceState);
setContentView(R.layout.view);
name = (TextView) findViewById(R.id.tv_name);
fb = (ImageButton) findViewById(R.id.btn_fb);
name1 = (TextView) findViewById(R.id.tv_name1);
favo = (ImageButton) findViewById(R.id.btn_fav);
contact = (TextView) findViewById(R.id.tv_contact);
address = (TextView) findViewById(R.id.tv_address);
category = (TextView) findViewById(R.id.tv_category);
view_map = (ImageButton) findViewById(R.id.view_direction);
db = new SQLController(this);
url = getResources().getString(R.string.url);
Bundle b = getIntent().getExtras();
data = b.getString("itemName");
new async_dataDetail(DataViewActivity.this).execute();
favo.setOnClickListener(this);
view_map.setOnClickListener(this);
fb.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_fav:
String myUser = name.getText().toString();
String storedUser = db.Exist(myUser);
// If Username exist
if (myUser.equals(storedUser)) {
Toast.makeText(getApplicationContext(), "Already exists !!!",
Toast.LENGTH_SHORT).show();
} else {
db.adddata(context, name.getText().toString(), category
.getText().toString(), id, MainActivity.a);
favoriteList = db.getFavList();
Log.d("aaaa", name.getText().toString()
+ category.getText().toString() + MainActivity.a);
Toast.makeText(getApplicationContext(),
"Successfully, added to Favourities", 0).show();
}
break;
}
}
class async_dataDetail extends AsyncTask<String, Void, Boolean> {
public async_dataDetail(DataViewActivity activity) {
context = activity;
}
@Override
protected Boolean doInBackground(String... arg0) {
// declare parameters that are passed to PHP script i.e. the
ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
if (MainActivity.a == 1) {
postParameters.add(new BasicNameValuePair("777", data));
try {
CustomHttpClient.executeHttpGet("777");
} catch (Exception e1) {
e1.printStackTrace();
}
} else if (Favourite.a == 1) {
postParameters.add(new BasicNameValuePair("524", data));
try {
CustomHttpClient.executeHttpGet("524");
} catch (Exception e1) {
e1.printStackTrace();
}
}
String response = null;
// call executeHttpPost method passing necessary parameters
try {
response = CustomHttpClient.executeHttpPost(
url,
postParameters);
// store the result returned by PHP script that runs
// MySQL query
String result = response.toString();
// parse json data
try {
JSONArray jArray = new JSONArray(result);
for (int i = 0; i < jArray.length(); i++) {
JSONObject json_data = jArray.getJSONObject(i);
st_name = " " + json_data.getString("name");
id = json_data.getString("id");
st_name1 = " Name : "
+ json_data.getString("name");
st_contact = " Contact : " + " "
+ json_data.getString("contact");
st_category = " Category : "
+ json_data.getString("Category");
st_address = " Address : "
+ json_data.getString("address");
Log.d("favourite_data", st_name + " " + st_contact
+ " " + st_category + " " + st_address);
}
} catch (JSONException e) {
Log.e("log_tag", "Error parsing data " + e.toString());
}
} catch (Exception e) {
Log.e("log_tag", "Error in http connection!!" + e.toString());
}
return null;
}
@Override
protected void onPostExecute(Boolean result) {
super.onPostExecute(result);
progressdialog.dismiss();
try {
name.setText(st_name);
name1.setText(st_name1);
contact.setText(st_contact);
category.setText(st_category);
address.setText(st_address);
} catch (Exception e) {
Log.e("log_tag", "Error in Display!" + e.toString());
}
}
@Override
protected void onPreExecute() {
super.onPreExecute();
progressdialog = new ProgressDialog(DataViewActivity.this);
progressdialog.setTitle("Processing....");
progressdialog.setMessage("Please Wait.....");
progressdialog.setCancelable(false);
progressdialog.show();
}
}
最喜欢的项目活动
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fav);
setTitle("Favorites");
// listView = (ListView) findViewById(R.id.listView);
listView = getListView();
db = new SQLController(this);
favoriteList = db.getFavList();
listView.setAdapter(new ViewAdapter());
}
public class ViewAdapter extends BaseAdapter {
LayoutInflater mInflater;
public ViewAdapter() {
mInflater = LayoutInflater.from(context);
}
@Override
public int getCount() {
return favoriteList.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView,
ViewGroup parent) {
if (convertView == null) {
convertView = mInflater.inflate(R.layout.listitem, null);
}
nameText = (TextView) convertView.findViewById(R.id.nameText);
nameText.setText(" Name : "
+ favoriteList.get(position).getName());
final TextView ageText = (TextView) convertView
.findViewById(R.id.ageText);
ageText.setText(favoriteList.get(position).getAge());
final Button delete = (Button) convertView
.findViewById(R.id.delete);
delete.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
db.removeFav(favoriteList.get(position).getId());
favoriteList = db.getFavList();
listView.setAdapter(new ViewAdapter());
}
});
convertView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
a = 1;
String aa = favoriteList.get(position).getCateg()
.toString();
String bb = favoriteList.get(position).getTable()
.toString();
intent.putExtra("itemName", aa + bb);
startActivity(intent);
}
});
return convertView;
}
}
Logcat:
10-16 14:41:36.700: W/System.err(23063): at java.lang.Thread.run(Thread.java:864)
10-16 14:41:36.830: D/OpenGLRenderer(23063): Flushing caches (mode 0)
10-16 14:41:36.830: D/memalloc(23063): /dev/pmem: Unmapping buffer base:0x556ab000 size:6144000 offset:4608000
10-16 14:41:36.830: D/memalloc(23063): /dev/pmem: Unmapping buffer base:0x58d7c000 size:7680000 offset:6144000
10-16 14:41:36.830: D/memalloc(23063): /dev/pmem: Unmapping buffer base:0x54043000 size:4608000 offset:3072000
10-16 14:41:37.831: E/log_tag(23063): Error parsing data org.json.JSONException: Value null of type org.json.JSONObject$1 cannot be converted to JSONArray
10-16 14:41:37.891: D/memalloc(23063): /dev/pmem: Unmapping buffer base:0x56291000 size:15728640 offset:15237120
10-16 14:41:37.891: D/memalloc(23063): /dev/pmem: Unmapping buffer base:0x594cf000 size:22794240 offset:22302720
10-16 14:41:37.901: D/memalloc(23063): /dev/pmem: Unmapping buffer base:0x5d32e000 size:24821760 offset:24330240
我在ViewData activity
从sql数据库通过json获取一些数据..在此活动中,当我按下favo
按钮时,一些细节被添加到SQLite数据库并显示在名为{{1的列表视图中}} ..
问题是,当我在收藏项目中添加一些数据时,它会成功添加。但是当我点击列表视图中的项目以获取项目的详细信息时,它没有显示它会给出错误。但当我重新启动我的应用程序并单击列表视图中的项目它工作正常..每次当我添加一些项目细节没有显示但是当我重新启动它工作正常..
答案 0 :(得分:0)
我在调试后得到了解决方案,问题是Mainactivity.a
的值和Favourite.a
重叠。所以responce
为空。但
最后,当点击收藏列表项时,我将值favourite.a = 1
更改为MainActvity.a =2
if (MainActivity.a == 1) {
postParameters.add(new BasicNameValuePair("777", data));
try {
CustomHttpClient.executeHttpGet("777");
} catch (Exception e1) {
e1.printStackTrace();
}
} else if (MainActivity.a == 2) {
postParameters.add(new BasicNameValuePair("524", data));
try {
CustomHttpClient.executeHttpGet("524");
} catch (Exception e1) {
e1.printStackTrace();
}
现在工作正常。