请帮助我,如果有人能让我想到这一点会很棒。 在这里,我试图将 kfcTab2.java 的详细信息(EditText,String和Image Button Click)发送到 KFCdata.java
显示错误
KFCdata中的KFCdata(android.content.Context)无法应用 到(com.packagename)
kfcTab2.java
public class kfcTab2 extends Fragment implements View.OnClickListener {
ImageButton menu1;
EditText K_No;
String KNo;
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.kfctab_2,container,false);
K_No = (EditText)v.findViewById(R.id.nos);
menu1 = (ImageButton)v.findViewById(R.id.kmenu1);
menu1.setOnClickListener(this);
return v;
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.kmenu1:
String Name = "Rudresh";
String order = "Chicken Biriyani";
KNo = K_No.getText().toString();
KFCdata BackTask = new KFCdata(this);
BackTask.execute(Name,order,KNo);
break;
}
}
}
KFCdata.java
public class KFCdata extends AsyncTask<String,Void,String> {
Context ctx;
KFCdata(Context ctx){
this.ctx = ctx;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected String doInBackground(String... params) {
String ORDER_URL = "http://zesto596.96.lt/orders.php";
String Customer = params[0];
String Order = params[1];
String Quantity = params[2];
try {
URL url = new URL(ORDER_URL);
HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setDoOutput(true);
OutputStream OS = httpURLConnection.getOutputStream();
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(OS,"UTF-8"));
String data = URLEncoder.encode("CUSTOMER","UTF-8")+"="+URLEncoder.encode(Customer,"UTF-8")+"&"+
URLEncoder.encode("ITEM","UTF-8")+"="+URLEncoder.encode(Order,"UTF-8")+"&"+
URLEncoder.encode("QUANTITY","UTF-8")+"="+URLEncoder.encode(Quantity,"UTF-8");
bufferedWriter.write(data);
bufferedWriter.flush();
bufferedWriter.close();
OS.close();
InputStream IS = httpURLConnection.getInputStream();
IS.close();
return "Item Added, please swipe to ORDERS...";
}catch (MalformedURLException e){
e.printStackTrace();
}catch (IOException e){
e.printStackTrace();
}
return null;
}
@Override
protected void onProgressUpdate(Void... values) {
super.onProgressUpdate(values);
}
@Override
protected void onPostExecute(String result) {
Toast.makeText(ctx, result, Toast.LENGTH_LONG).show();
}
}
kfcTab2.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TableLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorAccent">
<TableRow>
<ImageView
android:layout_width="40sp"
android:layout_height="60sp"
android:src="@drawable/biryani"/>
<TextView
android:layout_height="match_parent"
android:layout_width="match_parent"
android:text="Chicken Biriyani"
android:textSize="25sp"/>
<TextView
android:layout_height="match_parent"
android:layout_width="match_parent"
android:text="description"
android:layout_marginLeft="-150sp"
android:layout_marginStart="-150sp"
android:layout_marginTop="30sp"/>
<EditText
android:id="@+id/nos"
android:layout_height="40sp"
android:layout_width="40sp"
android:background="#f3f3f3"
android:hint="Nos"
android:layout_marginLeft="100sp"/>
<ImageButton
android:id="@+id/kmenu1"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_marginLeft="10sp"
android:src="@drawable/addtoplate"
android:onClick=""
android:background="@color/colorAccent"/>
</TableRow>
</TableLayout>
</RelativeLayout>
答案 0 :(得分:0)
替换您的代码
这与当前活动背景
KFCdata BackTask = new KFCdata(this.kfcTab2.getActivity()); BackTask.execute(Name,order,KNo); break;