如何替换listview中的文本

时间:2016-11-19 07:38:32

标签: android listview

我需要在Listview中替换“gold”

AndroidListView1 =“gold”> “goldennnnnnn”AndroidListView2 =“gold”> “golderrrrrrrrr”

当我在 edittext 中设置它然后它的工作!!但是当我在列表视图中设置它时,只打印1项(“goldennnnnnn”)## 的 this.et3.setText(AndroidListView); (edittext work) this.Listview.setText(AndroidListView1);无法正常工作

这是我的代码 提前感谢

ListView listView ;
EditText myTextBox,myOuttext,et3;
Button btn;

ClipboardManager myClipboard;
private Object adapter;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.activity_main);
     myTextBox = (EditText) findViewById(R.id.editText1);
     myOuttext = (EditText) findViewById(R.id.editText2);
     et3=(EditText)findViewById(R.id.editText3);
     btn=(Button)findViewById(R.id.button1);
     myClipboard = (ClipboardManager)getSystemService(CLIPBOARD_SERVICE);


     // Get ListView object from xml
     listView = (ListView) findViewById(R.id.listView1);

     // Defined Array values to show in ListView
     String[] values = new String[]
             { "AndroidListView1", 
             "AndroidListView2",
             "AndroidListView3",
             "AndroidListView4",
             "AndroidListView5",
             "AndroidListView6",
             "AndroidListView7",
             "AndroidListView8",
             "AndroidListView9",
             "AndroidListView10",
             "AndroidListView11",
             "AndroidListView12",
             "AndroidListView13",
             "AndroidListView14",
             "AndroidListView15",
             "AndroidListView16",
             "AndroidListView17",

                                     };

     // Define a new Adapter
     // First parameter - Context
     // Second parameter - Layout for the row
     // Third parameter - ID of the TextView to which the data is written
     // Forth - the Array of data
     ArrayList<?> yourlist = new ArrayList<Object>(); 
     ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
       android.R.layout.simple_list_item_1, android.R.id.text1, values);
   listView.setAdapter(adapter);

     // Assign adapter to ListView
     listView.setAdapter(new ArrayAdapter<String>(MainActivity.this,
             android.R.layout.simple_list_item_1, values));

     // ListView Item Click Listener
     listView.setOnItemClickListener(new OnItemClickListener() {

           @Override
           public void onItemClick(AdapterView<?> parent, View view,
              int position, long id) {

            // ListView Clicked item index
            int itemPosition     = position;

            // ListView Clicked item value
            String  itemValue    = (String) listView.getItemAtPosition(position);

             // Show Alert 
             Toast.makeText(getApplicationContext(),
               "Position :"+itemPosition+"  ListItem : " +itemValue , Toast.LENGTH_LONG)
               .show();

           }

      }); 
     myTextBox.addTextChangedListener(new TextWatcher() {

           public void afterTextChanged(Editable s) {
           }

           public void beforeTextChanged(CharSequence s, int start, 
             int count, int after) {
           }

           @SuppressLint("NewApi")
        public void onTextChanged(CharSequence s, int start, 
             int before, int count) {

               MainActivity.this.my1(s);
               MainActivity.this.AndroidListView(s);
           }
          });

}
 private String my1(CharSequence arg) {

        String str = "";
        str = arg.toString().replace(" ", "  " ).replace("gold", "goldennnnnnn");
        this.myOuttext.setText(str);
          System.out.println(str);
        return str;
    }
        private String AndroidListView(CharSequence arg) {

    String AndroidListView = "";
    AndroidListView= arg.toString().replace(" ", "").replace("gold", "golderrrrrrrrr");
     this.et3.setText(AndroidListView);
      System.out.println(AndroidListView);
    return AndroidListView;
}

}

1 个答案:

答案 0 :(得分:2)

尝试使用此代码更改特定位置的listView项目:

ArrayList<?> yourlist = new ArrayList<Object>();
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1, android.R.id.text1, values);
listView.setAdapter(adapter);

//listView.setAdapter(new ArrayAdapter<String>(MainActivity.this,
//            android.R.layout.simple_list_item_1, values)); 



values[3] = "myNewItem";
adapter.notifyDataSetChanged();

上述代码的回复是: Code_Response

编辑:搜索特定项目并进行更改:

for(int i =0; i<values.length; i++){
      if(values[i].equals("myNewItem")){
            values[i] = "A_Newer_One";
       }
 }
 adapter.notifyDataSetChanged();

New_Image