我正在个人项目中工作,我想构建一个活动来激活一些与列表视图中的项目相对应的功能。就是这样:
(对不起,我在这里很陌生,所以我无法附上图片)
What we should have in previous activity's listview
我的问题是:
我确实尝试了论坛中的某些选项,例如此链接Multiple Choices in ListView,但我必须使用按钮才能单击,当我使用getIntent()时它无法自动运行,总是弹出错误并关闭我的应用程序。
现在,我必须强制从StringArray中获取getIntent(),并使用String []从多项选择listview的活动中传递新的Adapter到listview
这是我到目前为止使用的代码:
main_listview.xml 和 list_devices.xml 完全相同
<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 =>主要活动(活动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 ===>第二个活动(活动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);
}});
}
这是我的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;
};
这是我到目前为止所拥有的:
Android ListView Refresh Single Row
Multiple choices in Activity B
我仍然想找到一个更好的解决方案来扩展我的知识并获得更多的经验,请给我一些信息,因为我认为我的产品不好并且不方便将来使用。
感谢您的观看,也为我的英语感到抱歉。