1

我正在使用列表视图。在我的列表视图中,我有文本、图像和复选框。我想在新的活动中打开选定的项目。但是当我点击按钮选择项目后,整个列表再次显示。我只想显示选定的项目。

我的 MainActivity.java 代码是

包 com.androidbegin.customimagelistview;

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

import android.os.Bundle;
import android.app.Activity;
import android.content.ClipData.Item;
import android.content.Intent;
import android.graphics.Path.FillType;
import android.util.SparseBooleanArray;
import android.view.Menu;
import android.view.View.OnClickListener;
import android.webkit.WebView.FindListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.view.View;

public class MainActivity extends Activity implements OnItemClickListener,                     OnClickListener ,OnCheckedChangeListener {

ArrayList<Model> model = new ArrayList<Model>();


// Declare Variables
ListView list;
ListViewAdapter adapter;
String[] rank;
String[] country;
String[] population;
int[] flag;
//ArrayList<String> rank = new ArrayList<String>();
List<Model> list2=new ArrayList<Model>();
Button btn;
Button getChoice;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.listview_main);
    btn=(Button)findViewById(R.id.button1);




    btn.setOnClickListener(this);


    //getChoice = (Button)findViewById(R.id.button1);
    //ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_multiple_choice,country);

//adapter = new ListViewAdapter(this, model);


    // Generate sample data
    rank = new String[] { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10" };

    country = new String[] { "China", "India", "United States",
            "Indonesia", "Brazil", "Pakistan", "Nigeria", "Bangladesh",
            "Russia", "Japan" };

    population = new String[] { "1,354,040,000", "1,210,193,422",
            "315,761,000", "237,641,326", "193,946,886", "182,912,000",
            "170,901,000", "152,518,015", "143,369,806", "127,360,000"                         };

    flag = new int[] { R.drawable.china, R.drawable.india,
            R.drawable.unitedstates, R.drawable.indonesia,
            R.drawable.brazil, R.drawable.pakistan, R.drawable.nigeria,
            R.drawable.bangladesh, R.drawable.russia, R.drawable.japan };

    // Locate the ListView in listview_main.xml
    list = (ListView) findViewById(R.id.listview);

    // Pass results to ListViewAdapter Class
    adapter = new ListViewAdapter(this, rank, country, population, flag);
    // Binds the Adapter to the ListView
    list.setAdapter(adapter);
    // Capture button clicks on ListView items
    list.setOnItemClickListener(new OnItemClickListener() {

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

            SparseBooleanArray checked = list.getCheckedItemPositions();
            ArrayList<String> selectedItems= new ArrayList<String>();
            for(int i=0; i<checked.size();i++)

            {
                // Item position in adapter
                int position1 = checked.keyAt(i);
                 // Add sport if it is checked i.e.) == TRUE!
                if(checked.valueAt(i))
                    selectedItems.add((String) adapter.getItem(position1));
                //oncheckedchANGElistner

            }

             String[] outputStrArr = new String[selectedItems.size()];
             for (int i = 0; i < selectedItems.size(); i++) {
                    outputStrArr[i] = selectedItems.get(i);
                }
             Intent intent = new Intent(getApplicationContext(),SingleItemView.class);
              Bundle b = new Bundle();
                b.putStringArray("selectedItems", outputStrArr);
                // Add the bundle to the intent.
                intent.putExtras(b);

                // start the ResultActivity
                startActivity(intent);

            TextView label = (TextView) view.getTag(R.id.rank);
            CheckBox checkbox= (CheckBox) view.getTag(R.id.check);
            Toast.makeText(view.getContext(), label.getText().toString()+ " " + isCheckedOrNot(checkbox), Toast.LENGTH_LONG).show();



            // Send single item click data to SingleItemView Class
            Intent i = new Intent(MainActivity.this, SingleItemView.class);
            // Pass all data rank
            i.putExtra("rank", rank);
            // Pass all data country
            i.putExtra("country", country);
            // Pass all data population
            i.putExtra("population", population);
            // Pass all data flag
            i.putExtra("flag", flag);
            // Pass a single position
            i.putExtra("position", position);
            // Open SingleItemView.java Activity
            startActivity(i);

        CheckBox chk = (CheckBox)view.findViewById(R.id.check);

        final ArrayList<Integer> checkedPositions = new ArrayList<Integer>();
        chk.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) 
            {

                if(isChecked=true)
                {
                    checkedPositions.add(position);

                }

            }

        });

    }

        private String isCheckedOrNot(CheckBox checkbox) {
            if (checkbox.isChecked())
                return "is checked";
            else
                return "is not checked";
        }
    });
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}

@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
} 

@Override
public void onClick(View arg0) {
    Intent i = new Intent(getApplicationContext(),SingleItemView.class);
    i.putExtra("rank", rank);
    i.putExtra("country", country);
    i.putExtra("population", population);
    i.putExtra("flag", flag);
    startActivity(i);

}

@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

}

}

提前致谢!!!

4

2 回答 2

0

在列表视图上实施OnItemClickListner,当您单击列表视图的任何项目时,存储有关您的文本、图像和复选框的信息。

将该信息传递intent给另一个活动并在那里显示..

希望对你有帮助...

于 2013-07-05T10:24:06.593 回答
0

在 ListviewAdapter 的 GetView 方法中设置数组。当用户选中复选框时,将数据插入数组。最后,根据您的需要访问该数组。

于 2013-07-05T10:28:22.437 回答