我想通过添加将行数据发送到另一个活动的按钮单击来实现此代码。这里需要在Spinner
和CheckBox
中实现,所以我只传递了那个被检查过的人,我也希望传递数量。
这是我的list.java文件,其中包含来自Web服务的数据。
package com.example.hello;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.util.SparseBooleanArray;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ListView;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.TableRow.LayoutParams;
public class List extends ActionBarActivity implements OnClickListener {
ListView listview;
Model[] modelItems;
private SQLiteDatabase db;
public void onCreate(Bundle cv) {
super.onCreate(cv);
setContentView(R.layout.listv);
listview = (ListView) findViewById(R.id.listView1);
/*Button b22=(Button)findViewById(R.id.b52);
Button b11=(Button)findViewById(R.id.b51);
b11.setOnClickListener(this);
b22.setOnClickListener(this);*/
String response = null;
try {
response = CustomHttpClient.executeHttpGet("http://legacysofttech.com/menulist.php/");
JSONArray jArray = new JSONArray(response);
modelItems = new Model[jArray.length()];
for (int i = 0; i < jArray.length(); i++)
{
JSONObject json_data = jArray.getJSONObject(i);
modelItems[i] = new Model(json_data.getString("Item"),json_data.getString("price"), 0,1);
// item_list.add(json_data.getString("Item"));
//price_list.add(json_data.getString("price"));
}
CustomAdapter adapter = new CustomAdapter(this, modelItems);
listview.setAdapter(adapter);
} catch (Exception e) {
Toast.makeText(getApplicationContext(), "your internet working to slow", Toast.LENGTH_SHORT).show();
}
}
public void onClick(View v)
{
switch(v.getId())
{
case R.id.b52:
{
}
case R.id.b51:
{
}
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
这是我的customadapter.java文件
package com.example.hello;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.Spinner;
import android.widget.TextView;
public class CustomAdapter extends ArrayAdapter<Model>
{
Model[] modelItems = null;
Context context;
public CustomAdapter(Context context, Model[] resource) {
super(context,R.layout.listv,resource);
// TODO Auto-generated constructor stub
this.context = context;
this.modelItems = resource;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
convertView = inflater.inflate(R.layout.listm, parent, false);
TextView name = (TextView) convertView.findViewById(R.id.textView1);
TextView price = (TextView) convertView.findViewById(R.id.textView2);
CheckBox cb = (CheckBox) convertView.findViewById(R.id.checkBox1);
Spinner spinner=(Spinner) convertView.findViewById(R.id.spinner1);
name.setText(modelItems[position].getName());
price.setText(modelItems[position].getPrice());
spinner.setId(modelItems[position].getQuantity());
if(modelItems[position].getValue() == 1)
cb.setChecked(true);
else
cb.setChecked(false);
return convertView;
}
}
这是我的Model.java文件
package com.example.hello;
public class Model{
String name;
String price;
int Quantity;
int value; /* 0 -> checkbox disable, 1 -> checkbox enable */
Model(String name, String price,int value,int Quantity){
this.name = name;
this.value = value;
this.price = price;
this.Quantity = Quantity;
}
public String getName(){
return this.name;
}
public int getValue(){
return this.value;
}
public int getQuantity(){
return this.Quantity;
}
public String getPrice(){
return this.price;
}
}
listv.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginBottom="50dp">
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
>
</ListView>
</RelativeLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:orientation="horizontal">
<Button
android:id="@+id/b51"
android:layout_gravity="bottom"
android:layout_width="100dp"
android:layout_height="40dp"
android:layout_marginRight="3dp"
android:layout_marginTop="1dp"
android:text="@string/CONFIRM"
android:layout_weight="1"
android:background="@drawable/button_default_bg"
/>
<Button
android:id="@+id/b52"
android:layout_gravity="bottom"
android:layout_width="100dp"
android:layout_height="40dp"
android:layout_marginRight="3dp"
android:layout_marginTop="1dp"
android:layout_weight="1"
android:background="@drawable/button_default_bg"
android:text="@string/cancel" />
</LinearLayout>
</RelativeLayout>
listm.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<CheckBox
android:id="@+id/checkBox1"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:text="" />
<TextView
android:id="@+id/textView1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="TextView" />
<TextView
android:id="@+id/textView2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="TextView"
android:layout_weight="1"
/>
<Spinner
android:id="@+id/spinner1"
style="@style/AppBaseTheme"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:entries="@array/Quantity"
/>
</LinearLayout>
我想使用SQLite数据库将带有数量,价格和商品名称的已检查项目发送到另一个活动。
你可以帮我开发onClick类和另一个活动文件。
谢谢。
答案 0 :(得分:0)
根据您可以选择的编程风格,有几种方法可以实现这一目标!
1-
一种非常强大的方法是使用content providers
并尝试使用URIs
在不同意图之间传递Intent.putData(URI)
并通过Intent.getData()
检索它。使用URI
使用已实现的内容提供程序来加载其他活动的数据。这种方法需要一些重新编码,但更可靠
2-
让您的Model类实现Parcelable
接口,然后使用Intent.putExtra("tag", Parcelable)
传递数据,并使用Intent.getExtra("tag")
检索其他意图。
3-
只需使用Intent.putExtra("quantity", quantity)
,Intent.putExtra("price", price)
传递整数或字符串数据,然后使用Intent.getExtra("price")
和Intent.getExtra("quantity")
在其他应用程序中检索它。这更简单但也不太灵活。为了更好地理解你,我实现了这个方法:
//I suppose a variable called "index" is used to hold the index of data you want to send from your Model[] array
Intent intent = new Intent(this, NewActivity.class);
intent.putExtra("quantity", modelItems[index].getPrice());
intent.putExtra("price", modelItems[index].getQuantity());
startActivity(intent);