2013-04-06 77 views
0

我有两个class.add和addmark。 在添加类中,我向数据库中提交了Id,姓名和电话号码。在addmark类中,当我从自动完成textview(通过添加类提交的数据库)中选择名称时,我想在两个不同的文本视图中设置Id和电话号码?在不同的文本视图中设置数据库值

package as.d.d; 

import java.util.List; 
import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.TextView; 
import android.widget.Toast; 

public class add extends Activity implements OnClickListener{ 


    Button b1,b2; 
    EditText e2,e3; 
    TextView t1; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.add); 

     b1=(Button)findViewById(R.id.button1); 
     b2=(Button)findViewById(R.id.button2); 
     e2=(EditText)findViewById(R.id.editText2); 
     e3=(EditText)findViewById(R.id.editText3); 
     t1=(TextView)findViewById(R.id.AtextView1); 

     b1.setOnClickListener(this); 
     b2.setOnClickListener(this); 


     DatabaseHandler db = new DatabaseHandler(this); 
     List<StudentInfo> studentInfo = db.getAllStudentInfo(); 

     for (StudentInfo cn : studentInfo){ 

      t1.setText("Id:"+(cn.getID()+1)); 
     } 
    } 

    @Override 
    public void onClick(View v) { 


     if(v==b2){ 
      startActivity(new Intent(add.this, FdActivity.class)); 
     } 
     else if(v==b1){ 

      String s2=e2.getText().toString(); 
      String s3=e3.getText().toString(); 

      if(s2.trim().equals("")||s3.trim().equals("")){ 

       Toast.makeText(getApplicationContext(), "Please Submit Student Information",Toast.LENGTH_SHORT).show(); 

       } 

      else{ 
       DatabaseHandler db = new DatabaseHandler(this); 
       Log.d("Insert: ", "Inserting .."); 
       db.addContact(new StudentInfo(s2,s3)); 
       Log.d("Reading: ", "Reading all contacts.."); 
       List<StudentInfo> studentInfo = db.getAllStudentInfo(); 
       for (StudentInfo cn : studentInfo){ 
        t1.setText("Id:"+(cn.getID()+1)); 
      }}}}} 

.....

package as.d.d; 

import java.util.ArrayList; 
import java.util.List; 

import android.app.Activity; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.AdapterView; 
import android.widget.AdapterView.OnItemClickListener; 
import android.widget.ArrayAdapter; 
import android.widget.AutoCompleteTextView; 
import android.widget.TextView; 
import android.widget.Toast; 

public class addmark extends Activity{ 

    AutoCompleteTextView a1; 
    TextView t1,t2; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.addmark); 

     a1=(AutoCompleteTextView)findViewById(R.id.autoCompleteTextView1); 
     t1=(TextView)findViewById(R.id.amtextView1); 
     t2=(TextView)findViewById(R.id.amtextView2); 




     DatabaseHandler db = new DatabaseHandler(this); 

     final List<StudentInfo> studentInfo = db.getAllStudentInfo(); 



     final ArrayList<String> s1 = new ArrayList<String>(); 


     for (StudentInfo cn : studentInfo) { 
      s1.add(cn.getName()); 


     } 
     ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line,s1); 

     a1.setThreshold(1); 
     a1.setAdapter(adapter); 



     a1.setOnItemClickListener(new OnItemClickListener() { 

      @Override 
      public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, 
        long arg3) { 
} 
     }); 

回答

0

我觉得这个代码将做到这一点

a1.setOnItemClickListener(new OnItemClickListener() { 

      @Override 
      public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, 
        long arg3) { 
       String id = studentInfo.get(arg2).getId(); 
       String phone = studentInfo.get(arg2).getPhone(); 
       t1.setText(id); 
       t2.setText(phone); 
      } 
     }); 
+0

这表明力close..not工作plz帮助我。 – bhoot4242 2013-04-06 14:56:20

+0

'getId();'和'getPhone();'我建议,你必须在'StudentInfo.class'中将它改为你的方法。 – AwadKab 2013-04-06 15:06:50

相关问题