主要活动
package com.example.wastewarriorsnew;
public class MainActivity extends Activity implements OnItemSelectedListener {
final String myTag = "DocsUpload";
String location,name,check_in,check_out,recy,nonrecy,recy_wt,nonrec_wt,new_loc,num,total_wt;
InputStream is=null;
String result=null;
String line=null;
int code;
String id;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Spinner element
Spinner spinner = (Spinner) findViewById(R.id.spinner1);
// Spinner click listener
spinner.setOnItemClickListener((OnItemClickListener) this);
// Spinner Drop down elements
List<String> categories = new ArrayList<String>();
categories.add("Dehradun");
categories.add("Pune");
categories.add("Mumbai");
categories.add("Delhi");
categories.add("Goa");
categories.add("Solapur");
// Creating adapter for spinner
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, categories);
// Drop down layout style - list view with radio button
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// attaching data adapter to spinner
spinner.setAdapter(dataAdapter);
final EditText ed2 = (EditText)findViewById(R.id.workername);
final EditText ed3 = (EditText)findViewById(R.id.Checkin);
final EditText ed4 = (EditText)findViewById(R.id.Chechout);
final EditText ed5 = (EditText)findViewById(R.id.Recycle);
final EditText ed6 = (EditText)findViewById(R.id.NonRecycle);
final EditText ed7 = (EditText)findViewById(R.id.Recycleweight);
final EditText ed8 = (EditText)findViewById(R.id.Nonweight);
final EditText ed9 = (EditText)findViewById(R.id.newlocation);
final EditText ed10 = (EditText)findViewById(R.id.Number);
final EditText ed11 = (EditText)findViewById(R.id.totalweight);
final Button bt1=(Button)findViewById(R.id.button1);
bt1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
name=ed2.getText().toString();
check_in=ed3.getText().toString();
check_out=ed4.getText().toString();
recy=ed5.getText().toString();
nonrecy=ed6.getText().toString();
recy_wt=ed7.getText().toString();
nonrec_wt=ed8.getText().toString();
new_loc=ed9.getText().toString();
num=ed10.getText().toString();
total_wt=ed11.getText().toString();
Log.i(myTag, "OnCreate()");
Thread t = new Thread(new Runnable() {
@Override
public void run() {
postData();
}
});
t.start();
// TODO Auto-generated method stub
}
});
}
public void postData() {
String fullUrl = "https://docs.google.com/forms/d/e/1FAIpQLScXtmI3Ev-cxc04Ai5_6zmnudLoF6W9g/formResponse";
HttpRequest mReq = new HttpRequest();
final EditText ed2 = (EditText)findViewById(R.id.workername);
final EditText ed3 = (EditText)findViewById(R.id.Checkin);
final EditText ed4 = (EditText)findViewById(R.id.Chechout);
final EditText ed5 = (EditText)findViewById(R.id.Recycle);
final EditText ed6 = (EditText)findViewById(R.id.NonRecycle);
final EditText ed7 = (EditText)findViewById(R.id.Recycleweight);
final EditText ed8 = (EditText)findViewById(R.id.Nonweight);
final EditText ed9 = (EditText)findViewById(R.id.newlocation);
final EditText ed10 = (EditText)findViewById(R.id.Number);
final EditText ed11 = (EditText)findViewById(R.id.totalweight);
//String col1 = "Hello";
//String col2 = "World";
String data =// "entry.1178044604=" + URLEncoder.encode(location) + "&" +
"entry.224535232=" + URLEncoder.encode(name) + "&" +
"entry.829233547="+ URLEncoder.encode(check_in) + "&" +
"entry.1814499237=" + URLEncoder.encode(check_out)+"&"+
"entry.1206946186="+ URLEncoder.encode(recy)+ "&" +
"entry.651664232=" + URLEncoder.encode(nonrecy) + "&" +
"entry.1115147786="+ URLEncoder.encode(recy_wt)+"&"+
"entry.472878985=" + URLEncoder.encode(nonrec_wt) +"&"+
"entry.1615743649="+ URLEncoder.encode(new_loc)+"&"+
"entry.2005603514="+ URLEncoder.encode(num)+"&"+
"entry.871683438="+ URLEncoder.encode(total_wt);
String response = mReq.sendPost(fullUrl, data);
Log.i(myTag, response);
}
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
// TODO Auto-generated method stub
// On selecting a spinner item
String item = parent.getItemAtPosition(position).toString();
// Showing selected spinner item
Toast.makeText(parent.getContext(), "Selected: " + item, Toast.LENGTH_LONG).show();
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
}
xml文件
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/back" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="630dp"
android:orientation="vertical"
tools:context=".MainActivity" >
<TextView android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Location address" />
<Spinner
android:id="@+id/spinner1"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Worker name"/>
<EditText
android:id="@+id/workername"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Worker Name"
android:inputType="text" />
<TextView android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Check in"/>
<EditText
android:id="@+id/Checkin"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Check-in time"
android:inputType="time" />
<TextView android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Check out"
/>
<EditText
android:id="@+id/Chechout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Check-out time"
android:inputType="time" />
<TextView android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="No. of recyclable bags"
/>
<EditText
android:id="@+id/Recycle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="No of bags used for recyclable material"
android:inputType="number" />
<TextView android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="No. of non-recyclable bags"
/>
<EditText
android:id="@+id/NonRecycle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="No of bags used for non-recyclable material"
android:inputType="number" />
<TextView android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Wt. of recycle material"
/>
<EditText
android:id="@+id/Recycleweight"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Weight of recyclable material"
android:inputType="text" />
<TextView android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Wt. of non-recycle material"
/>
<EditText
android:id="@+id/Nonweight"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Weight of non-recyclable material"
android:inputType="text" />
<TextView android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="New location(if shifted)"
/>
<EditText
android:id="@+id/newlocation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="New location(if shifted)"
android:inputType="text" />
<TextView android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="No. of persons"
/>
<EditText
android:id="@+id/Number"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="No. of persons per loaction"
android:inputType="number" />
<TextView android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Total weight"
/>
<EditText
android:id="@+id/totalweight"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Total weight of recyclable and non-recyclable material"
android:inputType="number" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<Button
android:id="@+id/button1"
android:layout_width="80dp"
android:layout_height="40dp"
android:layout_marginLeft="40dp"
android:layout_marginTop="50dp"
android:text="Save" />
</LinearLayout>
</LinearLayout>
</ScrollView>
我在logcat中遇到的错误是:
08-03 04:49:03.828:E / AndroidRuntime(773):java.lang.RuntimeException: 无法开始活动 ComponentInfo {} com.example./com.exampleMainActivity: java.lang.ClassCastException:com.example.MainActivity无法强制转换 到android.widget.AdapterView $ OnItemClickListener
但是现在logcat中没有消息,但msg仍然是:
不幸的是停止了工作。
当应用程序运行时,将从主活动中添加详细信息并单击“保存”按钮,它将以谷歌形式存储。 帮助我。