嗨在我的应用程序中我显示所有学生姓名并且滚动否,我有一个按钮如果我单击保存按钮我希望将记录存储到另一个表中。
现在我的问题是如何使用数组编辑文本的学生姓名和滚动号。为了测试目的我使用单一名称编辑文本显示学生姓名的单个值和滚动号。通过使用吐司点击保存按钮,它只显示学生姓名,滚动显示为空。
我错了可以请任何人帮助我。
Attend.java
public class Attend extends Activity implements OnClickListener {
String data = "";
TableLayout tl;
TableRow tr;
TextView studentName;
EditText Firstname,stRoll;
CheckBox present;
String[] class_name, section_name;
@SuppressLint("NewApi")
String student_name,roll;
Button save;
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tl = (TableLayout) findViewById(R.id.maintable);
save = (Button)findViewById(R.id.save);
Firstname = (EditText) findViewById(R.id.studentname );
Firstname.setVisibility(View.GONE);
stRoll = (EditText) findViewById(R.id.rollnumber );
stRoll.setVisibility(View.GONE);
save.setOnClickListener(new View.OnClickListener() {
@TargetApi(Build.VERSION_CODES.GINGERBREAD)
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//save.setText("Record Saved Sucessfully");
student_name = Firstname.getText().toString();
System.out.println(student_name);
roll = stRoll.getText().toString();
System.out.println(roll);
Toast.makeText(
Attend.this,
"Result : " + "\nStudentName : " +student_name
+ "\nRollno : " + roll,
Toast.LENGTH_SHORT).show();
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("student_name",student_name));
nameValuePairs.add(new BasicNameValuePair("roll_no",roll));
//nameValuePairs.add(new BasicNameValuePair("f3",v3));
StrictMode.setThreadPolicy(policy);
data = DatabaseUtility.executeQueryPhp("save","");
System.out.println(data);
}
});
String class_id = getIntent().getStringExtra("class_id");
String section_id = getIntent().getStringExtra("section_id");
Toast.makeText(
this,
"Result : " + "\nclassName : " + class_id + "\nSectionName : "
+ section_id,
Toast.LENGTH_SHORT).show();
final String queryString = "class_id=" + class_id + "§ion_id="
+ section_id;
/*String result = DatabaseUtility.executeQueryPhp("getStudent",
queryString);*/
new Thread(new Runnable() {
public void run() {
data = DatabaseUtility.executeQueryPhp("getStudent",queryString);
System.out.println(data);
runOnUiThread(new Runnable() {
@Override
public void run() {
ArrayList<StudentDetails> users = parseJSON(data);
addData(users);
}
});
}
}).start();
}
public ArrayList<StudentDetails> parseJSON1(String result1) {
ArrayList<StudentDetails> users = new ArrayList<StudentDetails>();
try {
JSONArray jArray = new JSONArray(result1);
for (int i = 0; i < jArray.length(); i++) {
JSONObject json_data = jArray.getJSONObject(i);
StudentDetails user = new StudentDetails();
user.setStudentname(json_data.getString("student_nme"));
user.setRollno(json_data.getString("roll_no"));
users.add(user);
}
} catch (JSONException e) {
Log.e("log_tag", "Error parsing data " + e.toString());
}
return users;
}
public ArrayList<StudentDetails> parseJSON(String result) {
ArrayList<StudentDetails> users = new ArrayList<StudentDetails>();
try {
JSONArray jArray = new JSONArray(result);
for (int i = 0; i < jArray.length(); i++) {
JSONObject json_data = jArray.getJSONObject(i);
StudentDetails user = new StudentDetails();
// user.setId(json_data.getInt("class_name"));
// user.setClass_id(json_data.getInt("section_name"));
// user.setSection_id(json_data.getInt("section_id"));
user.setStudentname(json_data.getString("first_name"));
user.setRollno(json_data.getString("roll_no"));
users.add(user);
}
} catch (JSONException e) {
Log.e("log_tag", "Error parsing data " + e.toString());
}
return users;
}
void addHeader() {
tr = new TableRow(this);
studentName = new TextView(this);
studentName.setText("StudentName");
studentName.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
studentName.setPadding(5, 5, 5, 5);
studentName.setBackgroundColor(Color.RED);
LinearLayout Ll = new LinearLayout(this);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
params.setMargins(5, 5, 5, 5);
Ll.addView(studentName, params);
tr.addView((View) Ll);
TextView RollNo = new TextView(this);
RollNo.setText("RollNo");
RollNo.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
RollNo.setPadding(5, 5, 5, 5);
RollNo.setBackgroundColor(Color.RED);
Ll = new LinearLayout(this);
params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT);
params.setMargins(0, 5, 5, 5);
Ll.addView(RollNo, params);
tr.addView((View) Ll);
TextView Present = new TextView(this);
Present.setText("Present");
Present.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
Present.setPadding(5, 5, 5, 5);
Present.setBackgroundColor(Color.RED);
Ll = new LinearLayout(this);
params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT);
params.setMargins(0, 5, 5, 5);
Ll.addView(Present, params);
tr.addView((View) Ll);
tl.addView(tr, new TableLayout.LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
}
@SuppressWarnings({ "rawtypes" })
public void addData(ArrayList<StudentDetails> users) {
addHeader();
for (Iterator i = users.iterator(); i.hasNext();) {
StudentDetails p = (StudentDetails) i.next();
/** Create a TableRow dynamically **/
tr = new TableRow(this);
/** Creating a TextView to add to the row **/
Firstname = new EditText(this);
Firstname.setText(p.getStudentname());
Firstname.setId(p.getId());
Firstname.setLayoutParams(new LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
Firstname.setPadding(5, 5, 5, 5);
LinearLayout Ll = new LinearLayout(this);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
params.setMargins(5, 2, 2, 2);
Ll.addView(Firstname, params);
tr.addView((View) Ll);
EditText stRoll1= new EditText(this);
stRoll1.setText(p.getRollno());
stRoll1.setLayoutParams(new LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
stRoll1.setPadding(5, 5, 5, 5);
Ll = new LinearLayout(this);
LinearLayout.LayoutParams params1 = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT);
params1.setMargins(0, 3, 3, 3);
Ll.addView(stRoll1, params1);
tr.addView((View) Ll);
CheckBox present = new CheckBox(this);
present.setLayoutParams(new LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
present.setPadding(5, 5, 5, 5);
Ll = new LinearLayout(this);
params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT);
params.setMargins(0, 3, 3, 3);
Ll.addView(present, params);
tr.addView((View) Ll);
tl.addView(tr, new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
}
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
}
activity_main.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" >
<ScrollView
android:id="@+id/scrollView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fillViewport="true"
android:scrollbars="none" >
<TableLayout
android:id="@+id/maintable"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:stretchColumns="1,0,0" >
</TableLayout>
</ScrollView>
<Button
android:id="@+id/save"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="save" />
<EditText
android:id="@+id/studentname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/scrollView1"
android:ems="10" >
<requestFocus />
</EditText>
<EditText
android:id="@+id/rollnumber"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/save"
android:ems="10" />
</RelativeLayout>
** DatabaseUtility.java **
public static String executeQueryPhp(String phpScript,String queryString) {
InputStream is = null;
String line = null;
String result = "";
try {
HttpClient httpclient = new DefaultHttpClient();
String url = "";
if(queryString!=""){
url= "http://10.0.2.2/android_connect/"+phpScript+".php?"+queryString;
}else{
url = "http://10.0.2.2/android_connect/"+phpScript+".php";
}
HttpPost httppost = new HttpPost(url);
HttpResponse response = httpclient.execute(httppost);
Log.e("Fail 1", "3");
HttpEntity entity = response.getEntity();
Log.e("Fail 1", "4");
is = entity.getContent();
Log.e("Pass 1", "connection success ");
} catch (Exception e) {
Log.e("Fail 1", e.toString());
/*
* Toast.makeText(getApplicationContext(), "Invalid IP Address",
* Toast.LENGTH_LONG).show();
*/
/* finish(); */
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
result = sb.toString();
} catch (Exception e) {
Log.e("Fail 2", e.toString());
}
return result;
}
}
PHP:
mysql_connect("localhost","root","");
mysql_select_db("login");
$student_name = !empty($_POST['student_name']) ? $_POST['student_name'] : $_GET['student_name'];
$roll_no = !empty($_POST['roll_no']) ? $_POST['roll_no'] : $_GET['roll_no'];
$query = "insert into save (student_name,roll_no) values('$student_name','$roll_no')";
$json_output = array();
if (!mysql_query($query )) {
$json_output[] = mysql_errno() . ": ".mysql_error();
}else{
$json_output[] = true;
}
print(json_encode($json_output));
mysql_close();
?>
答案 0 :(得分:0)
在你的addData上,你要为你的rollno创建一个新的EditBox,而不是像你使用Firstname那样使用你定义的那个。我的消化是改变
EditText stRoll1=
到
stRoll
以及所有stRoll1到stRoll。