我有一個圖標button
和一個listView
在活動A.如何編輯以前的listView而不是添加新的?
圖標按鈕----->意圖B,返回值爲A,添加到新的列表
的ListView點擊 - >意圖B,返回更改值返回列表查看
我使用mClickedPosition=-1;
來驗證圖標是否被點擊或listView,但它仍然添加到新列表中,即使listView被點擊。
活動A
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.addDetails:
mClickedPosition=-1; // if icon is clicked
View menuItemView = findViewById(R.id.addDetails);
PopupMenu po = new PopupMenu(this, menuItemView); //for drop-down menu
po.getMenuInflater().inflate(R.menu.popup_details, po.getMenu());
po.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
public boolean onMenuItemClick(MenuItem item) {
Toast.makeText(getApplication(), "You Clicked : " + item.getTitle(), Toast.LENGTH_SHORT).show();
if ("Add Work Details".equals(item.getTitle()))
{
Intent intent = new Intent(getApplication(), B.class); // go to Activity B
startActivityForResult(intent, PROJECT_REQUEST_CODE);
}
return true;
}
});
po.show(); //showing popup menu
}
return super.onOptionsItemSelected(item);
}
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() { // if listView is clicked
@Override
public void onItemClick(AdapterView<?> a, View v, int position, long id) {
mClickedPosition=position;
Object o = listview.getItemAtPosition(position);
SearchResults fullObject = (SearchResults) o;
String temp = fullObject.getDescription();
String Project=fullObject.getProject();
String [] ReceiveProject=Project.split(":");
String[] ReceiveDescription = temp.split(":");
Intent i = new Intent(getApplication(), B.class);
i.putExtra("ReceiveProject", ReceiveProject[1].trim());
i.putExtra("ReceiveDescription", ReceiveDescription[1]);
startActivityForResult(i,PROJECT_REQUEST_CODE);
}
});
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) { // receive from Activity B and populate ListView A
ReceiveProject = data.getStringExtra("Project");
ReceiveDescription = data.getStringExtra("Description");
if(mClickedPosition==-1) { // if icon clicked
MyCustomBaseAdapter objMyCustomBaseAdapter = (MyCustomBaseAdapter) listview.getAdapter();
objMyCustomBaseAdapter.addNewItem(ReceiveProject, ReceiveDescription, ReceiveProgress, ReceiveTimeIn, ReceiveTimeOut); // add list
}
else
{
// change the current list
}
}
MyCustomBaseAdapter
public class MyCustomBaseAdapter extends BaseAdapter{ // for ListView in Activity A
private static ArrayList<SearchResults> searchArrayList;
private LayoutInflater mInflater;
public MyCustomBaseAdapter(Context context, ArrayList<SearchResults> results) {
searchArrayList = results;
mInflater = LayoutInflater.from(context);
}
public int getCount() {
return searchArrayList.size();
}
public Object getItem(int position) {
return searchArrayList.get(position);
}
public long getItemId(int position) {
return position;
}
public void addNewItem(String P,String D,int Per,String I,String O)
{
SearchResults obj=new SearchResults();
obj.setProject(" Project/Service/Training : "+P);
obj.setDescription(" Work Description : " + D);
searchArrayList.add(obj);
this. notifyDataSetChanged();
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.custom_row_view, null);
holder = new ViewHolder();
holder.txtProject= (TextView) convertView.findViewById(R.id.ListProject);
holder.txtDescription = (TextView) convertView.findViewById(R.id.ListDescription);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.txtProject.setText(searchArrayList.get(position).getProject());
holder.txtDescription.setText(searchArrayList.get(position).getDescription());
return convertView;
}
static class ViewHolder {
TextView txtProject;
TextView txtDescription;
}
}
因此,您點擊菜單選項,但其添加新列表 – Pankaj
@Clairvoyant沒有。它應該添加一個新的列表,如果菜單選項被點擊。現在的問題是它添加新的列表,雖然listView點擊。 – John
所以你想實現的是將項目添加到以前的列表,如果用戶點擊列表視圖,如果點擊菜單選項,然後使新列表 – Pankaj