编辑 listView 值

Posted

技术标签:

【中文标题】编辑 listView 值【英文标题】:Edit listView value 【发布时间】:2016-02-17 08:00:17 【问题描述】:

我有 3 个活动,A,B 和 C。活动的流程是从 A 到 B,然后是 C。从 C,所有图像和值将返回到 B,然后 A。所有返回的值和图像将是加载在listViewA.

listView是可点击的,一旦被点击,它将intent给B做一些编辑。但是现在的问题是当我在 B 中编辑文本值并返回 A 时,我得到一个新列表而不是更新旧列表。 这里有什么问题?任何人都可以帮忙吗?

活动 C

 public void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.image_fit_screen);
        selectImage();
        b = (ImageView) findViewById(R.id.imageView3);
        t = (EditText) findViewById(R.id.editText38);
        cancel=(Button)findViewById(R.id.button15);
        ok=(Button)findViewById(R.id.button16);

        cancel.setOnClickListener(new View.OnClickListener()
        
            public void onClick(View arg0) 
                finish();
            
        );

        ok.setOnClickListener(new View.OnClickListener()
          // return to B
            public void onClick(View arg0)
            
                Intent returnIntent=new Intent();
                text=t.getText().toString();
                b.setDrawingCacheEnabled(true);
                b.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
                        View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
                b.layout(0, 0, b.getMeasuredWidth(), b.getMeasuredHeight());
                b.buildDrawingCache(true);
                returnIntent.putExtra("text", text);
                if (b.getDrawingCache() != null) 
                    Bitmap bitmap = Bitmap.createBitmap(b.getDrawingCache());
                    if (bitmap == null) 
                        Log.e("TAG", "getDrawingCache() == null");
                    
                    Global.img = bitmap;
                
                setResult(Activity.RESULT_OK, returnIntent);
                finish();
            
        );
    

活动 B

    ImageButton imageButton;
    ImageView viewImage;
    public void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.project);
        txt = (EditText) findViewById(R.id.editText36);
        txt1=(TextView)findViewById(R.id.textView57);
        Button b = (Button) findViewById(R.id.button17);
        addListenerOnButton();
        viewImage = (ImageView) findViewById(R.id.imageView2);

        if(getIntent().getExtras()!=null)  //if  has value pass from A
            final String Amount = getIntent().getExtras().getString("result");
            final String description1 = getIntent().getExtras().getString("description");

            txt1.setText(description1);
            txt.setText(Amount);
        

                b.setOnClickListener(new View.OnClickListener()   // return to A
                    public void onClick(View arg0) 
                        Intent returnIntent = new Intent();
                        a = "Project";
                        text = txt.getText().toString(); // amount
                        returnIntent.putExtra("text", text);
                        returnIntent.putExtra("a", a);
                        returnIntent.putExtra("c", c);
                        setResult(Activity.RESULT_OK, returnIntent);
                        finish();
                    
                );
                viewImage.setImageBitmap(Global.img);

                



    public void addListenerOnButton() 
         imageButton = (ImageButton) findViewById(R.id.imageButton);
         imageButton.setOnClickListener(new View.OnClickListener() 

            @Override
            public void onClick(View arg0) 
                Global.img=null;
                Intent i = new Intent(B.this,C.class);
                startActivityForResult(i, PROJECT_REQUEST_CODE);
            

        );

    

    public void onActivityResult(int requestCode,int resultCode, Intent data)
    
        if(requestCode==PROJECT_REQUEST_CODE)  // receive fom C
            if(data!=null&&data.hasExtra("text")) 
                c = data.getStringExtra("text");
                txt1.setText(c);
                viewImage.setImageBitmap(Global.img);  // image from C can be shown here
            


        
        else if (requestCode==CAMERA_REQUEST_CODE)
        

        
    


活动 A

public class Claims1 extends Fragment 

    ListView listV;
    String description;
    String result="";
    String name;
    long as=0;
    TextView c;
    ImageView v;
  ArrayAdapter<String> adapter;
    ArrayList<String> m_listItems = new ArrayList<String>();
    String Text;

  public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        Bundle bundle = this.getArguments();
        setHasOptionsMenu(true);
        View claims = inflater.inflate(R.layout.receipt_text, container, false);
        v=(ImageView)claims.findViewById(R.id.imageView4);
        listV = (ListView) claims.findViewById(R.id.listView1);
        android.support.v7.app.ActionBar myActionBar = ((AppCompatActivity) getActivity()).getSupportActionBar();
        myActionBar.setHomeButtonEnabled(true);
        ImageView imageView = new ImageView(myActionBar.getThemedContext());
        imageView.setScaleType(ImageView.ScaleType.CENTER);
        imageView.setImageResource(R.mipmap.create);


        adapter=new ArrayAdapter<String>(getActivity(),R.layout.claims,R.id.textView1,m_listItems);

        android.support.v7.app.ActionBar.LayoutParams layoutParams = new android.support.v7.app.ActionBar.LayoutParams(
                android.support.v7.app.ActionBar.LayoutParams.WRAP_CONTENT,
                android.support.v7.app.ActionBar.LayoutParams.WRAP_CONTENT, Gravity.RIGHT
                | Gravity.CENTER_VERTICAL);  // for icon in action bar
        layoutParams.rightMargin = 40;
        imageView.setLayoutParams(layoutParams);
        myActionBar.setCustomView(imageView);

        listV.setOnItemClickListener(new AdapterView.OnItemClickListener() 
            public void onItemClick(AdapterView<?> listView, View view,
                                    int position, long id) 
              if(name.equals("Project")) 
                    Intent intent = new Intent(Claims1.this.getActivity(), B.class);
                    intent.putExtra("bitmap",true);
                    intent.putExtra("name",name);
                    intent.putExtra("result",result);
                    startActivityForResult(intent,0);
                

            
        );
        return claims;
    

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) 
        inflater.inflate(R.menu.my_menu, menu);
        super.onCreateOptionsMenu(menu, inflater);

    

    @Override
    public boolean onOptionsItemSelected(MenuItem item) 
        switch (item.getItemId()) 
            case R.id.create1:
                AlertDialogRadio();
                return true;
        
        return (super.onOptionsItemSelected(item));
    


    public void AlertDialogRadio() 
        final CharSequence[] ClaimsModel = "B", "Petrol";

        AlertDialog.Builder alt_bld = new AlertDialog.Builder(getActivity());
        alt_bld.setTitle("Select a Class");
        alt_bld.setSingleChoiceItems(ClaimsModel, -1, new DialogInterface
                .OnClickListener() 

            public void onClick(DialogInterface dialog, int item) 
                if (item == 0) 
                    Intent intent = new Intent(getActivity().getApplicationContext(),B.class);
                    startActivityForResult(intent, 0);
                 else if (item == 1) 
                    Intent intent = new Intent(getActivity().getApplicationContext(), Petrol.class);
                    startActivityForResult(intent, 1);
                 
                dialog.dismiss();

            
        );
        AlertDialog alert = alt_bld.create();
        alert.show();
    

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) 

        switch (requestCode) 
                case 0:
                    result = data.getStringExtra("text");
                    name = data.getStringExtra("a");
                    description = data.getStringExtra("c");
                    as = Long.parseLong(result);
                    Log.d("FIRST", "result:"+result);
                    Text="  "+name+"                                  "+"RM"+result+"";
                    m_listItems.add(Text);
                    adapter.notifyDataSetChanged();
                    listV.setAdapter(adapter);

                    break;

                case 1:
                    result = data.getStringExtra("text");
                    name = data.getStringExtra("a");
                    description = data.getStringExtra("c");
                    if (Global.img != null) 
                        v.setImageBitmap(Global.img);
                    
                    as = Long.parseLong(result);
                    c.setText("                            " + name + "------" + "RM " + result);
                    break;
        

    

已编辑

在A中添加一个全局变量——private int mClickedPosition; 在 OnItemClick 中添加mClickedPosition = position;

然后

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) 

    switch (requestCode) 
            case 0:
                result = data.getStringExtra("text");
                name = data.getStringExtra("a");
                description = data.getStringExtra("c");
                as = Long.parseLong(result);
                Log.d("FIRST", "result:"+result);
                Text="  "+name+"                                  "+"RM"+result+"";
                m_listItems.set(mClickedPosition,Text);
                adapter.notifyDataSetChanged();
                listV.setAdapter(adapter);

                break;

11-16 18:15:54.751 23044-23044/com.example.project.project E/AndroidRuntime: FATAL EXCEPTION: main 进程:com.example.project.project,PID:23044 java.lang.RuntimeException: 传递结果失败 ResultInfowho=android:fragment:0, request=0, result=-1, data=Intent (有额外的)活动 com.example.project.project/com.example.project.project.MainActivity: java.lang.IndexOutOfBoundsException:索引 0 无效,大小为 0 在 android.app.ActivityThread.deliverResults(ActivityThread.java:3681) 在 android.app.ActivityThread.handleSendResult(ActivityThread.java:3724)

 m_listItems.set(mClickedPosition,Text);

【问题讨论】:

您的描述似乎具有误导性。从 A 类你去 B 而不是 C。引用你的话:'活动流程是从 A 到 C,'请再次阅读你的描述,并编辑任何误导性陈述。 为列表视图提供更多代码 新列表?意味着什么?您不想更新列表吗?解释您的问题,以便我们为您提供帮助 我认为适配器中的数据集没有更新。在活动 A 中尝试添加 adapter.notifyDataSetChanged();在 listV.setAdapter(adapter); 之前 请显示您如何创建adapter?谢谢 【参考方案1】:

在您的活动 A onActivityResult 方法中,当您在编辑后从活动 B 返回时,您正在使用此代码 m_listItems.add(Text) 将新项目添加到数组列表中;

您使用此代码更新数组列表,而不是添加新项目

m_listItems.set(position Text);

【讨论】:

m_listItems.set(position Text);m_listItems.set(position,Text); @John 如果你想更新适配器更新数组列表而不是在数组列表中添加新项目 我需要将位置创建为全局变量吗? 这是您单击活动 A 列表视图的位置,您可以在单击活动 A 列表视图时使用意图将此位置发送到 B 并在您从活动 B 回到 A 时发回。 Intent intent = new Intent(Claims1.this.getActivity(), B.class);在此意图中发送位置并在 B 的板条箱上接收它,然后在我们返回 A 和 A 中的 onactivityResult 方法时发回,您将能够获得位置【参考方案2】:

尝试像这样编辑您的代码:

case 0:
    result = data.getStringExtra("text");
    name = data.getStringExtra("a");
    description = data.getStringExtra("c");
    as = Long.parseLong(result);
    Log.d("FIRST", "result:"+result);
    Text="  "+name+"                                  "+"RM"+result+"";
    if (m_listItems.size() == 0) 
        m_listItems.add(Text);
     else 
        m_listItems.set(mClickedPosition,Text);
    
    adapter.notifyDataSetChanged();
    listV.setAdapter(adapter);

    break;

【讨论】:

等等...现在我只能更新列表..如何添加新列表 请看***.com/questions/33741464/…

以上是关于编辑 listView 值的主要内容,如果未能解决你的问题,请参考以下文章

C#如何让listview可编辑 winform

如何使用edittexts在自定义ListView中实现监听器文本更改?

c# 如何双击listview 的项可变成编辑状态?

带有可点击/可编辑小部件的 ListView

Android如何在ListView中使EditText可编辑

c# listview 怎么添加图片