2017-05-09 57 views
1

我試圖從ListView項檢索值爲「mylistview.getItemAtPosition(position).toString()」,但它只返回我點擊的項目的位置。如何從列表視圖中同時具有字符串(名稱)和整數(聯繫人)的項目?

我MainActivity.java:

public class MainActivity extends AppCompatActivity{ 
    RelativeLayout rl1; 
    Button b1,b2,b3,b4,b5,b6,b7,b8,b9,b0,bhash,bStar,del,call; 
    EditText editText; 
    Intent phoneIntent,callIntent; 
    getContacts.MyAdapter myAdapter; 
    ListView lv; 


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
     setSupportActionBar(toolbar); 
     rl1=(RelativeLayout) findViewById(R.id.relativeLayout); 
     rl1.setVisibility(View.GONE); 

     editText = (EditText)findViewById(R.id.textView); 
     editText.setShowSoftInputOnFocus(false); 
     b1 = (Button)findViewById(R.id.button); 
     b2 = (Button)findViewById(R.id.button2); 
     b3 = (Button)findViewById(R.id.button3); 
     b4 = (Button)findViewById(R.id.button4); 
     b5 = (Button)findViewById(R.id.button5); 
     b6 = (Button)findViewById(R.id.button6); 
     b7 = (Button)findViewById(R.id.button7); 
     b8 = (Button)findViewById(R.id.button8); 
     b9 = (Button)findViewById(R.id.button9); 
     b0 = (Button)findViewById(R.id.button0); 
     bhash = (Button)findViewById(R.id.buttonHash); 
     bStar = (Button)findViewById(R.id.buttonStar); 
     call = (Button) findViewById(R.id.buttonCall); 
     del = (Button)findViewById(R.id.buttonDel); 

     getContacts.getAllContacts(this.getContentResolver()); 
    myAdapter = new getContacts.MyAdapter(getApplicationContext()); 

     final List<Object> name2 = new ArrayList<>(); 
     final ListView lv= (ListView) findViewById(R.id.lv); 
     lv.setAdapter(myAdapter); 
     lv.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
      @Override 
      public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) { 

       Toast.makeText(MainActivity.this, lv.getItemAtPosition(position).toString(), Toast.LENGTH_SHORT).show(); 
      } 
     }); 
     phoneIntent = new Intent(Intent.ACTION_CALL); 
     callIntent = new Intent(Intent.ACTION_CALL); 


     FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); 
     fab.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       rl1.setVisibility(rl1.isShown()?View.GONE:View.VISIBLE); 

       b1.setOnClickListener(new View.OnClickListener() { 
        @Override 
        public void onClick(View v) { 
         int pos = editText.getSelectionStart(); 
         String str = editText.getText().toString(); 
         String str1 = (String) str.substring(0, pos); 
         String str2 = (String) str.substring(pos); 
         editText.setText(str1+"1" +str2); 
         int pos1 = pos+1; 
         editText.setSelection(pos1); 
        } 
       }); 
       b2.setOnClickListener(new View.OnClickListener() { 
        @Override 
        public void onClick(View v) { 
         int pos = editText.getSelectionStart(); 
         String str = editText.getText().toString(); 
         String str1 = (String) str.substring(0, pos); 
         String str2 = (String) str.substring(pos); 
         editText.setText(str1+"2"+str2); 
         int pos1 = pos+1; 
         editText.setSelection(pos1); 

        } 
       }); 
       b3.setOnClickListener(new View.OnClickListener() { 
        @Override 
        public void onClick(View v) { 
         int pos = editText.getSelectionStart(); 
         String str = editText.getText().toString(); 
         String str1 = (String) str.substring(0, pos); 
         String str2 = (String) str.substring(pos); 
         editText.setText(str1+"3" +str2); 
         int pos1 = pos+1; 
         editText.setSelection(pos1); 
        } 
       }); 
       b4.setOnClickListener(new View.OnClickListener() { 
        @Override 
        public void onClick(View v) { 
         int pos = editText.getSelectionStart(); 
         String str = editText.getText().toString(); 
         String str1 = (String) str.substring(0, pos); 
         String str2 = (String) str.substring(pos); 
         editText.setText(str1+"4" +str2); 
         int pos1 = pos+1; 
         editText.setSelection(pos1); 
        } 
       }); 
       b5.setOnClickListener(new View.OnClickListener() { 
        @Override 
        public void onClick(View v) { 
         int pos = editText.getSelectionStart(); 
         String str = editText.getText().toString(); 
         String str1 = (String) str.substring(0, pos); 
         String str2 = (String) str.substring(pos); 
         editText.setText(str1+"5" +str2); 
         int pos1 = pos+1; 
         editText.setSelection(pos1); 
        } 
       }); 
       b6.setOnClickListener(new View.OnClickListener() { 
        @Override 
        public void onClick(View v) { 
         int pos = editText.getSelectionStart(); 
         String str = editText.getText().toString(); 
         String str1 = (String) str.substring(0, pos); 
         String str2 = (String) str.substring(pos); 
         editText.setText(str1+"6" +str2); 
         int pos1 = pos+1; 
         editText.setSelection(pos1); 
        } 
       }); 
       b7.setOnClickListener(new View.OnClickListener() { 
        @Override 
        public void onClick(View v) { 
         int pos = editText.getSelectionStart(); 
         String str = editText.getText().toString(); 
         String str1 = (String) str.substring(0, pos); 
         String str2 = (String) str.substring(pos); 
         editText.setText(str1+"7" +str2); 
         int pos1 = pos+1; 
         editText.setSelection(pos1); 
        } 
       }); 
       b8.setOnClickListener(new View.OnClickListener() { 
        @Override 
        public void onClick(View v) { 
         int pos = editText.getSelectionStart(); 
         String str = editText.getText().toString(); 
         String str1 = (String) str.substring(0, pos); 
         String str2 = (String) str.substring(pos); 
         editText.setText(str1+"8" +str2); 
         int pos1 = pos+1; 
         editText.setSelection(pos1); 
        } 
       }); 
       b9.setOnClickListener(new View.OnClickListener() { 
        @Override 
        public void onClick(View v) { 
         int pos = editText.getSelectionStart(); 
         String str = editText.getText().toString(); 
         String str1 = (String) str.substring(0, pos); 
         String str2 = (String) str.substring(pos); 
         editText.setText(str1+"9" +str2); 
         int pos1 = pos+1; 
         editText.setSelection(pos1); 
        } 
       }); 
       b0.setOnClickListener(new View.OnClickListener() { 
        @Override 
        public void onClick(View v) { 
         int pos = editText.getSelectionStart(); 
         String str = editText.getText().toString(); 
         String str1 = (String) str.substring(0, pos); 
         String str2 = (String) str.substring(pos); 
         editText.setText(str1+"0" +str2); 
         int pos1 = pos+1; 
         editText.setSelection(pos1); 
        } 
       }); 
       bhash.setOnClickListener(new View.OnClickListener() { 
        @Override 
        public void onClick(View v) { 
         int pos = editText.getSelectionStart(); 
         String str = editText.getText().toString(); 
         String str1 = (String) str.substring(0, pos); 
         String str2 = (String) str.substring(pos); 
         editText.setText(str1+"#" +str2); 
         int pos1 = pos+1; 
         editText.setSelection(pos1); 
        } 
       }); 
       bStar.setOnClickListener(new View.OnClickListener() { 
        @Override 
        public void onClick(View v) { 
         int pos = editText.getSelectionStart(); 
         String str = editText.getText().toString(); 
         String str1 = (String) str.substring(0, pos); 
         String str2 = (String) str.substring(pos); 
         editText.setText(str1+"*" +str2); 
         int pos1 = pos+1; 
         editText.setSelection(pos1); 
        } 
       }); 
       call.setOnClickListener(new View.OnClickListener() { 
        @Override 
        public void onClick(View v) { 
         String number = editText.getText().toString(); 
         if (number.charAt(0)!='*') { 
          if (number.length()<=9) 
          { 
           Toast.makeText(MainActivity.this, "Please enter a valid number", Toast.LENGTH_SHORT).show(); 
          } 
          else { 
           phoneIntent.setData(Uri.parse("tel:" + number)); 
           startActivity(phoneIntent); 
          } 
         } 
         else if (number.subSequence(number.length()-1,number.length()).equals("#")) 
         { 
          number = number.substring(0, number.length() - 1); 
          String callInfo = "tel:" + number + Uri.encode("#"); 
          callIntent.setData(Uri.parse(callInfo)); 
          startActivity(callIntent); 
         } 
        } 
       }); 
       del.setOnClickListener(new View.OnClickListener() { 
        @Override 
        public void onClick(View v) { 
         int posElse = editText.getSelectionStart(); 
         String text = editText.getText().toString(); 
         if(text.length()!=0 && posElse >= 1) { 
          int pos = editText.getSelectionStart(); 
          String str1 = (String) text.substring(0, pos-1); 
          if(str1.equals(null)) 
          { 

          } 
          String str2 = (String) text.substring(pos); 
          String latest = str1+str2; 
          editText.setText(latest); 
          int pos1 = pos-1; 
          editText.setSelection(pos1); 
         } 
         else if (posElse < 1) 
         { 
          int pos = editText.getSelectionStart(); 

          String str2 = (String) text.substring(pos); 
          String latest = str2; 
          editText.setText(latest); 
          int pos1 = pos; 
          editText.setSelection(pos1); 
         } 
        } 
       }); 

       del.setOnLongClickListener(new View.OnLongClickListener() { 
        @Override 
        public boolean onLongClick(View v) { 
         if (del.equals(v)) 
         { 
          editText.setText(""); 
         } 
         return false; 
        } 
       }); 
       /*Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG) 
         .setAction("Action", null).show();*/ 
      } 
     }); 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.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(); 

     //noinspection SimplifiableIfStatement 
     if (id == R.id.action_settings) { 
      return true; 
     } 

     return super.onOptionsItemSelected(item); 
    } 


} 

我getContacts.java:

public class getContacts { 
    static List<String> name1 = new ArrayList<String>(); 
    static List<String> phno1 = new ArrayList<String>(); 

    public static void getAllContacts(ContentResolver cr) { 

     Cursor phones = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,null,null, null); 
     while (phones.moveToNext()) 
     { 
      String name=phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME)); 
      String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); 
      System.out.println(".................."+phoneNumber); 
      name1.add(name); 
      phno1.add(phoneNumber); 
     } 

     phones.close(); 
    } 

    static class MyAdapter extends BaseAdapter 
    { 
     private SparseBooleanArray mCheckStates; 
     LayoutInflater mInflater; 
     TextView tv1,tv; 
     MyAdapter(Context ctx) 
     { 
      mCheckStates = new SparseBooleanArray(name1.size()); 
      //mInflater = (LayoutInflater)Display.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      mInflater = (LayoutInflater)ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     } 
     @Override 
     public int getCount() { 
      // TODO Auto-generated method stub 
      return name1.size(); 
     } 

     @Override 
     public Object getItem(int position) { 
      // TODO Auto-generated method stub 
      return position; 
     } 

     @Override 
     public long getItemId(int position) { 
      // TODO Auto-generated method stub 

      return 0; 
     } 

     @Override 
     public View getView(final int position, View convertView, ViewGroup parent) { 
      // TODO Auto-generated method stub 
      View vi=convertView; 
      if(convertView==null) 
       vi = mInflater.inflate(R.layout.row, null); 
      TextView tv= (TextView) vi.findViewById(R.id.textView1); 
      tv1= (TextView) vi.findViewById(R.id.textView2); 
      tv.setText(name1.get(position)); 
      // tv1.setText(phno1.get(position)); 


      return vi; 
     } 

    } 

} 

有人可以請幫我擺脫選中的列表項的數據?

回答

0

你可以嘗試讓你的適配器方法,像這樣:

public String getName(int position) { 
    return name1.get(position); 
} 

public String getPhoneNumber(int position) { 
    return phno1.get(position); 
} 

既然你說你有索引,調用適配器對象在你mainactivity這些方法。這隻有在你的數組列表中的項目位置匹配時纔有效。

我還建議使用對象來存儲您的數據,在這裏尋找更多信息:https://www.raywenderlich.com/124438/android-listview-tutorial

相關問題