2013-07-05 58 views
0

我使用的是一個listview。在我的listview中,我有文本,圖像和複選框。我想以新的活力打開選定的項目。但是當我點擊按鈕選擇項目後,整個列表再次顯示。我只想顯示選定的項目。如何將所選項目從複選框發送到android中的其他活動?

我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) { 

} 

}

先謝謝了!

回答

0

實施OnItemClickListner在列表視圖,當你在列表視圖存儲中的信息的任何項目點擊關於你的文字,圖片和複選框..

穿過intent到另一個活動的信息,並顯示它有..

希望這會對你有幫助...

0

在你的ListViewAdapter的GetView方法中設置數組。當用戶選中複選框插入你的數組中的數據。最後,根據需要訪問該數組。

相關問題