このコードを実装するには、ボタンのクリックを追加して行データを別のアクティビティに送信します。ここではSpinner
とCheckBox
の実装が必要なので、チェックされている人だけに渡して、数量も渡したいと思います。他のアクティビティに行データを送信するボタンのクリックを追加することによってこのコードを実装する方法
ここには、Webサービスからのデータを持つmy.javaファイルがあります。
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が
listm.xmlpackage 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>
ファイルであります
<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クラスと別のアクティビティファイルの開発を手伝ってください。
ありがとうございます。
私はあなたが私にどのようにコンテンツプロバイダを使用し、コードを再設計する必要があり –
onclickのクラスにOnCreateのクラスからの位置を継承するボタンとのonclickイベントを処理するコードを記入してくださいすることができ、データベースへのチェックされた項目の店をしたいです – Pooya
私は自分自身でこれを学んでいるので、私は本当にそれについて知らない、あなたはそれを手伝ってもらえますか?それは、最終学年の工学のための私のプロジェクトです –