I'm working in my personal project, and I want to build an activity to activate some functions correspond to the items in listview. Just like this :
(Sorry I'm very brand new here so I cannot attach picture)
What we should have in previous activity's listview
My problem is :
- I dont know how to store and load the listview multiple choice when I go back
- I cannot show the name of choices in textview of the single row in listview of previous acitivity
I did try with some options in our forum such as this link Android ListView Refresh Single Row but I have to use button to click, it cannot work automatically when I used getIntent(), always pops up error and close my app.
Now I have to force to getIntent() from a StringArray and set new Adapter to the listview with String[] pass from multiple choices listview's activity
Here is the code I use so far :
main_listview.xml and list_devices.xml are exactly the same
<Button
android:id="@+id/getchoice"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Get Choice"
/>
<ListView
android:id="@+id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
AndroidListViewActivity.java => Main activity (Activity A)
String[] listContent = {
"January",
"February",
"March",
"April",
"May",
};
String[] subtitle ={
"Tap to set","Tap to set",
"Tap to set","Tap to set",
"Tap to set",
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_listview);
Bundle extras = getIntent().getExtras();
myList = (ListView)findViewById(R.id.list);
getChoice = (Button)findViewById(R.id.getchoice);
if (extras != null)
{
String[] getExtra = extras.getStringArray("part1");
adapter = new MyListAdapter(this, listContent, getExtra);
}
else
adapter = new MyListAdapter(this, listContent, subtitle);
myList.setAdapter(adapter);
getChoice.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View v) {
int start = myList.getFirstVisiblePosition();
View view = myList.getChildAt(1 -start);
myList.getAdapter().getView(1, view, myList);
TextView getabc = (TextView)view.findViewById(R.id.subtitle);
getabc.setText(getText);
// break;
}});
myList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
// TODO Auto-generated method stub
if(position == 0) {
//code specific to first list item
Toast.makeText(getApplicationContext(),"Place Your First Option Code",Toast.LENGTH_SHORT).show();
Intent mainact = new Intent (AndroidListViewActivity.this, ListDevices.class);
startActivity(mainact);
}
else if(position == 1) {
Toast.makeText(getApplicationContext(),"Place Your First Option Code",Toast.LENGTH_SHORT).show();
Intent mainact = new Intent (AndroidListViewActivity.this, ListDevices.class);
startActivity(mainact);
Toast.makeText(getApplicationContext(),"Place Your Second Option Code",Toast.LENGTH_SHORT).show();
}
else if(position == 2) {
Toast.makeText(getApplicationContext(),"Place Your Third Option Code",Toast.LENGTH_SHORT).show();
}
else if(position == 3) {
Toast.makeText(getApplicationContext(),"Place Your Forth Option Code",Toast.LENGTH_SHORT).show();
}
else if(position == 4) {
Toast.makeText(getApplicationContext(),"Place Your Fifth Option Code",Toast.LENGTH_SHORT).show();
}
}
});
}
mylist.xml
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Medium Text"
android:textStyle="bold"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_marginLeft="10dp"
android:layout_marginTop="5dp"
android:padding="2dp"
android:textColor="#4d4d4d" />
<TextView
android:id="@+id/subtitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Tap to set"
android:layout_marginLeft="10dp"/>
ListDevices.java ===> Second Activity (Activity B)
public class ListDevices extends Activity {
ListView myList;
Button getChoice;
String selected ="", selected1="", selected2="", selected3="", selected4="";
String[] abc = { selected,selected1,selected2,selected3,selected4 };
Bundle extras;
Intent intent_new;
String[] listContent = {
"January",
"February",
"March",
"April",
"May",
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_devices);
myList = (ListView)findViewById(R.id.list);
getChoice = (Button)findViewById(R.id.getchoice);
ArrayAdapter<String> adapter
= new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_multiple_choice,
listContent);
intent_new = new Intent (ListDevices.this, AndroidListViewActivity.class);
extras = new Bundle();
myList.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
myList.setAdapter(adapter);
getChoice.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
int cntChoice = myList.getCount();
SparseBooleanArray sparseBooleanArray = myList.getCheckedItemPositions();
for(int i = 0; i < cntChoice; i++){
if(sparseBooleanArray.get(i)) {
abc[0] += myList.getItemAtPosition(i).toString() + "\n";
}
}
for (int i=0 ; i < abc.length ; i++)
{
if (abc[i] == "")
abc[i] = abc[i].replace("","Tap to set");
}
intent_new.putExtra("part1",abc);
startActivity(intent_new);
}});
}
Here is my ArrayAdapter : MyListAdapter.java
public class MyListAdapter extends ArrayAdapter<String> {
private final Activity context;
private final String[] maintitle;
private final String[] subtitle;
public MyListAdapter(Activity context, String[] maintitle,String[] subtitle) {
super(context, R.layout.mylist, maintitle);
this.context=context;
this.maintitle=maintitle;
this.subtitle=subtitle;
}
@Override
public int getCount() {
return maintitle.length;
}
@Override
public String getItem(int position) {
return maintitle[position];
}
@Override
public long getItemId(int position) {
return position;
}
public View getView(int position, View view, ViewGroup parent) {
LayoutInflater inflater=context.getLayoutInflater();
View rowView=inflater.inflate(R.layout.mylist, null,true);
TextView titleText = (TextView) rowView.findViewById(R.id.title);
TextView subtitleText = (TextView) rowView.findViewById(R.id.subtitle);
titleText.setText(maintitle[position]);
subtitleText.setText(subtitle[position]);
return rowView;
};
Here is what I have so far :
Multiple choices in Activity B
I'm still wonder for a better solution to expand my knowledge and get more experiences, please kindly give me some because I think mine is not good and convenient for future use.
Thank you for watching and sorry for my English too.