修改listView的元素如何访问它

时间:2014-04-21 12:57:08

标签: android listview

我了解到为了修改listView中的行,我需要获取访问权限,例如通过adapter.getItem(position),但我不知道如何解决这个问题。我应该发布任何代码,请告诉我。 这是我的EditListItemDialog文件:

package com.example.classorganizer;

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

import com.example.classorganizer.Monday.DiaryAdapter;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

class EditListItemDialog extends Dialog implements View.OnClickListener {

private View editText;
private DiaryAdapter adapter;

  //  public EditListItemDialog(Context context, List<String> fragment_monday) {         //first constructor
  //      super(context);
  //      this.fragment_monday = fragment_monday;
  //  }

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.edit_text_dialog);//here is your xml with EditText and 'Ok' and 'Cancel' buttons
View btnOk = findViewById(R.id.button_ok);
editText = findViewById(R.id.edit_text);
btnOk.setOnClickListener(this);

}

private List<String> fragment_monday;


public EditListItemDialog(Context context, DiaryAdapter adapter) {
super(context);
this.fragment_monday = new ArrayList<String>();
this.adapter = adapter;
}

@Override
public void onClick(View v) {
fragment_monday.add(((TextView) v).getText().toString());//here is your updated(or not updated) text
adapter.notifyDataSetChanged();
dismiss();
}
}

2 个答案:

答案 0 :(得分:0)

您必须实现OnItemClickListener,它将能够告诉您已单击的项目。例如:

public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    //Position is the number of the item clicked
    //You can use your adapter to modify the item
    adapter.getItem(position); //Will return the clicked item
}

有关详细信息,请参阅官方Android documentation

答案 1 :(得分:0)

      package com.example.classorganizer;

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

      import com.example.classorganizer.Monday.DiaryAdapter;

      import android.app.Dialog;
      import android.content.Context;
      import android.os.Bundle;
      import android.view.View;
      import android.widget.TextView;
   public class EditListItemDialog extends Activity {
     ListView listView ;
     private View editText;

   @Override
   protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
     setContentView(R.layout.edit_text_dialog);
     View btnOk = findViewById(R.id.button_ok);
       editText = findViewById(R.id.edit_text);
      btnOk.setOnClickListener(this);
    listView = (ListView) findViewById(R.id.list)

     String[] values = new String[] { "Automotive", "Banking", "Consumer Electronics",  
     "Education", "HealthCare and Life Sciences","Industrial Automation","Printing and 
      Imaging","Manufacturing","Media and 
      Entertainment","Networking","Retail","Telecom"
                                    };

    // 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

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
      android.R.layout.simple_expandable_list_item_1, values);


    // Assign adapter to ListView
    listView.setAdapter(adapter); 
     }
    }

将此代码用于简单的Listview ..