如何在 listView 中更新和添加新列表?

Posted

技术标签:

【中文标题】如何在 listView 中更新和添加新列表?【英文标题】:How to update and add a new list in listView? 【发布时间】:2016-02-17 21:51:14 【问题描述】:

在我的活动 A 中,它有一个图标 button 和一个 listView。图标button 用于intent 到B 和C,而listView 用于intent B 进行一些编辑。

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

现在我想要实现的是将所有从 C 和 B 获取的值加载到 A。接下来,可以在单击 listView 时更新列表。

活动 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 
    private int mClickedPosition;
    ListView listV;
    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() 
        mClickedPosition = position;
              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 boolean onOptionsItemSelected(MenuItem item) 
        switch (item.getItemId()) 
            case R.id.create1: // icon is clicked
                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+"";
            if (m_listItems.size() == 0) 
                m_listItems.add(Text);
             else 
                m_listItems.set(mClickedPosition,Text);
            
            adapter.notifyDataSetChanged();
            listV.setAdapter(adapter);

            break;
         
      

但是现在,当我单击图标 button 以意图类 B 和 C 并将值从 C 和 B 返回到 A 时,它只会更新 listView 而不是创建一个新的。为什么会这样?

【问题讨论】:

在下面查看我的答案。添加了一个名为 setAdapter() 的方法;确保替换您的 onCreateView() 和 onActivityResult() 并添加 setAdapter() 究竟什么是无法解决的?为我复制并粘贴该行。 【参考方案1】:

检查我的编辑

public class Claims1 extends Fragment 
private int mClickedPosition;
ListView listV;
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);

    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);

    setMyAdapter();
    return claims;


public void setMyAdapter() 
    adapter=new ArrayAdapter<String>(getActivity(),R.layout.claims,R.id.textView1,m_listItems);
    listV.setAdapter(adapter);

    listV.setOnItemClickListener(new AdapterView.OnItemClickListener() 
          public void onItemClick(AdapterView<?> listView, View view,
                                int position, long id) 
          mClickedPosition = position;
          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);
            
        
    );


@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.clear();
        if (m_listItems.size() == 0) 
            m_listItems.add(Text);
         else 
            m_listItems.set(mClickedPosition, Text);
        
        setMyAdapter();
        break;
    


【讨论】:

感谢您的回答,但返回值仍在更新旧列表,而不是创建新列表 @John 嗯,如果你想要一个新的列表来解决问题,那么文本至少会改变吗?你这样做的方式将很难改变比文本更多的东西。 我只更改了文本...我也想添加新列表 我迷路了,它基本上是一个新列表,一切都被清除了 我想我已经修复了一分钟我正在做一个大编辑

以上是关于如何在 listView 中更新和添加新列表?的主要内容,如果未能解决你的问题,请参考以下文章

调用 notifyDataSetChanged 时未更新带有 customAdapter 的 ListView?

如何在 Flutter 的 ListView 中添加列?

如何在已填充列表的 listView 中添加列表?

在 Flutter 中构建列表项时如何更新 ListView.builder 的 itemCount?

如何滚动 ListView 中的项目使其可见?

拖放事件后如何在ListView中检索ListModel的新有序列表