2016-04-07 3 views
0

Ich möchte diesen Code implementieren, indem Sie auf die Schaltflächen klicken, die Zeilendaten an eine andere Aktivität senden. Hier benötigte Implementierung in Spinner und CheckBox, so übergebe ich diesen Wert nur wer überprüft wird und ich auch Quantität weiterleiten möchte.wie Sie diesen Code implementieren, indem Sie Schaltflächen hinzufügen, die Zeilendaten an eine andere Aktivität senden

Hier ist meine list.java Datei, die Daten von Web-Services haben.

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); 
    } 
} 

hier ist es meine customadapter.java Datei

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; 
       }   
} 

Hier ist mein Model.java

package com.example.hello; 
public class Model{ 
String name; 
String price; 
int Quantity; 
int value; /* 0 -&gt; checkbox disable, 1 -&gt; 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 Datei

Ich möchte überprüfte Artikel mit Menge, Preis und Artikelname an eine andere Aktivität senden, indem Sie die SQLite-Datenbank verwenden.

können Sie mir bitte helfen, onClick-Klasse und eine andere Aktivitätsdatei zu entwickeln.

Vielen Dank.

Antwort

0

Es gibt mehrere Möglichkeiten, wie Sie dies erreichen können, basierend auf Ihrem Programmierstil, den Sie wählen können!

1-

Eine sehr robuste Art und Weise ist content providers zu verwenden und versuchen URIs zwischen verschiedenen Absichten von Intent.putData(URI) mit zu übergeben und sie durch Intent.getData() abrufen. Mit dem URI verwenden Sie einen implementierten Inhaltsanbieter, um die Daten für die andere Aktivität zu laden. Diese Methode benötigt einige Umcodierung aber ist zuverlässige

2-

Lassen Sie Ihre Modellklasse Parcelable Schnittstelle implementieren und dann Daten weiter mit Intent.putExtra("tag", Parcelable) und Abrufen auf der anderen Absicht mit Intent.getExtra("tag").

3-

Einfach nur die Ganzzahl oder Zeichenfolgendaten mit Intent.putExtra("quantity", quantity) Pass Intent.putExtra("price", price) und es in der anderen Anwendung mit Intent.getExtra("price") und Intent.getExtra("quantity") abzurufen. Dies ist einfacher, aber auch weniger flexibel. Ich implementieren diese Methode für ein besseres Verständnis für Sie:

//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); 
+0

ich will überprüft Artikel Speicher-Datenbank, so können Sie mir bitte Code geben, der das Onclick-Ereignis für Schaltfläche umgehen und wie die Position von oncreate Klasse Onclick Klasse erben –

+0

Sie Content-Anbieter verwenden sollten und Ihren Code neu gestalten – Pooya

+0

Ich weiß es nicht wirklich, weil ich das selbst erlerne. Kannst du mir dabei helfen? es ist meine Projektarbeit für das letzte Jahr Bachelor Engineering –

3

Ich habe den Quellcode

Modell ändern.

java
package com.example.hello; 
    public class Model{ 
    String name; 
    String price; 
    int Quantity; 
    int value; /* 0 -&gt; checkbox disable, 1 -&gt; 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 setValue(int value){ 
    return this.value=value; 
    } 
    public int getQuantity(){ 
     return this.Quantity; 
    } 
    public String getPrice(){ 
    return this.price; 
    } 
    } 

Code for list.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);  
       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: 
     { 
    for (int i = 0; i < modelItems.length(); i++) 
      { 
    if(modelItems[i].getValue()==1) 
    { 
        Toast t = Toast.makeText(this, 
           modelItems[i].getName(), Toast.LENGTH_LONG); 
    t.setGravity(Gravity.CENTER, 0, 0); 
         t.show(); 
    } 
     } 
     } 
     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); 
    } 


public class CustomAdapter extends ArrayAdapter<Model> 

{ 

      Context context; 
      public CustomAdapter(Context context) { 
      super(context,R.layout.listv,resource); 
      // TODO Auto-generated constructor stub 
      this.context = context; 

} 

      @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; 



     cb.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       CheckBox cb1 = (CheckBox) v; 

       if (cb1.isChecked()) { 

        modelItems[position].setValue(1) 
       } else { 

        modelItems[position].setValue(0) 
       } 
       notifyDataSetChanged(); 

      } 
     }); 
       }   
} 
} 
Verwandte Themen