从mysql数据库putExtra到不能运行Android的新活动

时间:2014-08-04 20:09:34

标签: android mysql database sqlite

我的代码有问题。

此活动(收藏夹)有一个包含许多项目的listView,每个项目都有一个从sqlite数据库加载的textview。

所有项目都是可点击的,当您单击一个项目时,它会将该项目中的textView与mysql数据库进行比较。

如果mysql数据库具有该值,我将其和其余数据库值发送到新活动(标题,文本和图像)。

但问题是,当您单击一个项目时,新活动将启动,但它不会通过所有putExtra值发送(仅标题通过)。但是,当您再次使用箭头并再次点击同一项目时,它会将值传递给新活动。

我该如何解决这个问题?我可以先做一些事情从mysql数据库加载数据,准备好后,启动新活动吗?有任何想法吗?如果有,请解释一些代码示例。

Favorite.java

public class Favorite extends ActionBarActivity {

private FavoriteListAdapter adapter;

public final static String EXTRA_TEXT = "xxx.xxx.xxx.TEXT";
public final static String EXTRA_TITLE = "xxx.xxx.xxx.TITLE";
public final static String EXTRA_IMAGE = "xxx.xxx.xxx.IMAGE";

public String db_image;
String listTitle;

MySQLiteHelper db;
ListView favoriteListView;

TextView title;
TextView text;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.favorite);

    text = (TextView) findViewById(R.id.text);
    title = (TextView) findViewById(R.id.title);

    text.setVisibility(View.INVISIBLE);
    title.setVisibility(View.INVISIBLE);

    setupListViewAdapter();

    favoriteListView
            .setOnItemClickListener(new AdapterView.OnItemClickListener() {

                @Override
                public void onItemClick(AdapterView<?> av, View arg1,
                        int index, long arg3) {

                    new task().execute();

                    listTitle = av.getItemAtPosition(index).toString();

                    title.setText(listTitle);

                    Intent toNewActivity = new Intent(Favorite.this,
                            NewActivity.class);

                    TextView textView = (TextView) findViewById(R.id.text);
                    String text = textView.getText().toString();
                    toNewActivity.putExtra(EXTRA_TEXT, text);

                    TextView titleView = (TextView) findViewById(R.id.title);
                    String title = titleView.getText()
                            .toString();
                    toNewActivity.putExtra(EXTRA_TITLE, title);
                    toNewActivity.putExtra(EXTRA_IMAGE, db_image);

                    startActivity(toNewActivity);
                }

            });

}

private void setupListViewAdapter() {

    db = new MySQLiteHelper(this);
    adapter = new FavoriteListAdapter(Favorite.this, R.layout.list_view,
            db.getAllXXX());

    favoriteListView = (ListView) findViewById(R.id.favorite_list);
    favoriteListView.setAdapter(adapter);
}

class task extends AsyncTask<String, String, Void> {
    private ProgressDialog progressDialog = new ProgressDialog(
            Favorite.this);
    InputStream is = null;
    String result = "";

    protected void onPreExecute() {
        progressDialog.setMessage("Loading...");
        progressDialog.show();
        progressDialog.setOnCancelListener(new OnCancelListener() {
            @Override
            public void onCancel(DialogInterface arg0) {
                task.this.cancel(true);
            }
        });
    }

    @Override
    protected Void doInBackground(String... params) {
        String url_select = "http://xxx.xxx/db.php";

        HttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(url_select);

        ArrayList<NameValuePair> param = new ArrayList<NameValuePair>();

        try {
            httpPost.setEntity(new UrlEncodedFormEntity(param));

            HttpResponse httpResponse = httpClient.execute(httpPost);
            HttpEntity httpEntity = httpResponse.getEntity();

            is = httpEntity.getContent();

        } catch (Exception e) {

            Log.e("log_tag", "Error in http connection " + e.toString());
        }
        try {
            BufferedReader br = new BufferedReader(new InputStreamReader(
                    is, "iso-8859-1"), 8);
            StringBuilder sb = new StringBuilder();
            String line = "";
            while ((line = br.readLine()) != null) {
                sb.append(line + "\n");
            }
            is.close();
            result = sb.toString();

        } catch (Exception e) {
            Log.e("log_tag", "Error converting result " + e.toString());
        }

        return null;

    }

    protected void onPostExecute(Void v) {

        try {
            JSONArray Jarray = new JSONArray(result);

            for (int i = 0; i < Jarray.length(); i++) {
                JSONObject Jasonobject = null;
                // text_1 = (TextView)findViewById(R.id.txt1);
                Jasonobject = Jarray.getJSONObject(i);

                String db_text = "";

                if (title.getText().toString()
                        .equalsIgnoreCase(Jasonobject.getString("title"))) {
                    db_text = Jasonobject.getString("text");
                    text.setText(db_text);

                    db_image = Jasonobject.getString("image");

                    title.setVisibility(View.INVISIBLE);

                    break;
                }

            }
            this.progressDialog.dismiss();

        } catch (Exception e) {
            // TODO: handle exception
            Log.e("log_tag", "Error parsing data " + e.toString());
        }
    }
}

FavoriteListAdapter.java

public class FavoriteListAdapter extends ArrayAdapter<DBLite> {

TextView title;

protected static final String LOG_TAG = FavoriteListAdapter.class
        .getSimpleName();

private List<DBLite> items;
private int layoutResourceId;
private Context context;

public FavoriteListAdapter(Context context, int layoutResourceId,
        List<DBLite> items) {
    super(context, layoutResourceId, items);
    this.layoutResourceId = layoutResourceId;
    this.context = context;
    this.items = items;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View row = convertView;
    favoriteListHolder holder = null;

    LayoutInflater inflater = ((Activity) context).getLayoutInflater();
    row = inflater.inflate(layoutResourceId, parent, false);

    holder = new favoriteListHolder();
    holder.favoriteList = items.get(position);

    holder.title = (TextView) row.findViewById(R.id.txt);
    holder.title.setTag(holder.favoriteList);

    row.setTag(holder);

    setupItem(holder);
    return row;
}

private void setupItem(favoriteListHolder holder) {
    holder.title.setText(holder.favoriteList.getTitle());

}

public static class favoriteListHolder {
    DBLite favoriteList;
    TextView title;
}
}

0 个答案:

没有答案