ArrayList<HashMap<String, String>> arraylist
结构我正面临的错误:
ResultActivity.java
行ArrayAdapter<String>
adapter=new
ArrayAdapter<String>(ResultActivity.this,R.layout.custom_single_list,R.id.textView1,
arraylist);
The constructor
ArrayAdapter<String>(ResultActivity, int, int,
ArrayList<HashMap<String,String>>) is undefined
ListViewAdapterForAtomicListItemtype.java
BLD_IndividualListOfItems_Starters.java
public class BLD_IndividualListOfItems_Starters extends Activity{
// Declare Variables
JSONObject jsonobject;
JSONArray jsonarray;
ListView listview;
ListViewAdapterForAtomicListItemtype adapter;
ProgressDialog mProgressDialog;
ArrayList<HashMap<String, String>> arraylist;
static String NAME = "rank";
Button btn;
String TYPE_FILTER;
StringBuilder result;
MyApplication mApplication;
@Override
public void onCreate(Bundle savedInstanceState) {
mApplication = (MyApplication)getApplication();
super.onCreate(savedInstanceState);
// Get the view from listview_main.xml
setContentView(R.layout.listview_main);
TYPE_FILTER = getIntent().getExtras().getString("key_title");
Log.v("---- Value-Start---", TYPE_FILTER);
// Locate the listview in listview_main.xml
listview = (ListView) findViewById(R.id.listview);
btn = (Button) findViewById(R.id.button1);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
result = new StringBuilder();
for (int i = 0; i < arraylist.size(); i++) {
if (adapter.mysparse.get(i) == true) {
result.append(arraylist.get(i).get(BLD_IndividualListOfItems_Starters.NAME));
result.append("\n");
}
}
Intent n = new Intent(BLD_IndividualListOfItems_Starters.this, ResultActivity.class);
n.putExtra("buffer", result.toString());
startActivity(n);
}
});
// Execute DownloadJSON AsyncTask
new DownloadJSON().execute();
}
// DownloadJSON AsyncTask
private class DownloadJSON extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
// Create a progressdialog
mProgressDialog = new ProgressDialog(BLD_IndividualListOfItems_Starters.this);
// Set progressdialog title
//mProgressDialog.setTitle("Fetching the information");
// Set progressdialog message
mProgressDialog.setMessage("Loading...");
mProgressDialog.setIndeterminate(false);
// Show progressdialog
mProgressDialog.show();
}
@Override
protected Void doInBackground(Void... params) {
// Create an array
arraylist = new ArrayList<HashMap<String, String>>();
String newurl = "?" + "Key=" + TYPE_FILTER;
// Retrieve JSON Objects from the given URL address
jsonobject = JSONfunctions.getJSONfromURL("http://54.218.73.244:7005/RestaurantAtomicListItemType/"+newurl);
try {
// Locate the array name in JSON
jsonarray = jsonobject.getJSONArray("restaurants");
for (int i = 0; i < jsonarray.length(); i++) {
HashMap<String, String> map = new HashMap<String, String>();
jsonobject = jsonarray.getJSONObject(i);
// Retrive JSON Objects
map.put(BLD_IndividualListOfItems_Starters.NAME, jsonobject.getString("MasterListMenuName"));
// Set the JSON Objects into the array
arraylist.add(map);
}
} catch (JSONException e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void args) {
// Pass the results into ListViewAdapter.java
adapter = new ListViewAdapterForAtomicListItemtype(BLD_IndividualListOfItems_Starters.this, arraylist);
// Set the adapter to the ListView
mApplication.setArrayListMapData(arraylist);
listview.setAdapter(adapter);
// Close the progressdialog
mProgressDialog.dismiss();
}
}
}
ResultActivity.java
public class ResultActivity extends Activity {
ListView lv;
ArrayList<String> myList;
ArrayList<HashMap<String, String>> arraylist;
String myName;
MyApplication mApplication;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
mApplication = (MyApplication)getApplication();
arraylist = mApplication.getArrayListMapData();
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
Intent n = getIntent();
myName = n.getStringExtra("buffer");
myList = new ArrayList<String>();
lv = (ListView) findViewById(R.id.listViewData);
myList.add(myName);
ArrayAdapter<String> adapter=new ArrayAdapter<String>(ResultActivity.this,R.layout.custom_single_list,R.id.textView1, arraylist);
lv.setAdapter(adapter);
}
}
ListViewAdapterForAtomicListItemtype.java
public class ListViewAdapterForAtomicListItemtype extends BaseAdapter implements OnCheckedChangeListener {
// Declare Variables
Context context;
LayoutInflater inflater;
ArrayList<HashMap<String, String>> data;
HashMap<String, String> resultp = new HashMap<String, String>();
SparseBooleanArray mysparse;
public ListViewAdapterForAtomicListItemtype(Context context,
ArrayList<HashMap<String, String>> arraylist) {
this.context = context;
data = arraylist;
mysparse = new SparseBooleanArray(data.size());
}
@Override
public int getCount() {
return data.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
public View getView(final int position, View convertView, ViewGroup parent) {
// Declare Variables
TextView name;
CheckBox chk;
inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View itemView = inflater.inflate(R.layout.listview_item_for_atomic_list_item_type, parent, false);
// Get the position
resultp = data.get(position);
// Locate the TextViews in listview_item.xml
name = (TextView) itemView.findViewById(R.id.textView_id_atomic_list_item_type);
chk = (CheckBox) itemView.findViewById(R.id.checkBox_atomic_list_item_type_id);
// Capture position and set results to the TextViews
name.setText(resultp.get(BLD_IndividualListOfItems_Starters.NAME));
chk.setTag(position);
chk.setChecked(mysparse.get(position, false));
chk.setOnCheckedChangeListener(this);
return itemView;
}
public boolean isChecked(int position) {
return mysparse.get(position, false);
}
public void setChecked(int position, boolean isChecked) {
mysparse.put(position, isChecked);
}
public void toggle(int position) {
setChecked(position, !isChecked(position));
}
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
mysparse.put((Integer) buttonView.getTag(), isChecked);
}
}
MyApplication.java
public class MyApplication extends Application {
ArrayList<HashMap<String, String>> arraylist;
@Override
public void onCreate() {
super.onCreate();
}
public void setArrayListMapData(ArrayList<HashMap<String, String>> setData)
{
arraylist = setData;
}
public ArrayList<HashMap<String, String>> getArrayListMapData()
{
return arraylist;
}
}
custom_single_list.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>
ResultActivity.java
public class ResultActivity extends ListActivity {
ListView lstView;
ArrayList<HashMap<String,String>> arraylist = new ArrayList<HashMap<String,String>>();
String myName;
MyApplication mApplication;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
for(int i=0;i<10;i++)
{
HashMap<String,String> map = new HashMap<String,String>();
map.put("key", "value"+i);
arraylist.add(map);
}
String[] from = { "key" };
int[] to = { R.id.textView1 };
SimpleAdapter adapter= new SimpleAdapter(this, arraylist,R.layout.custom_single_list, from, to);
setListAdapter(adapter);
}
}
日志 ::
01-03 12:48:40.088: D/AndroidRuntime(267): Shutting down VM
01-03 12:48:40.098: W/dalvikvm(267): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
01-03 12:48:40.138: E/AndroidRuntime(267): FATAL EXCEPTION: main
01-03 12:48:40.138: E/AndroidRuntime(267): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.multitabcheckboxselection/com.example.multitabcheckboxselection.BreakfastLunchDinnerIndividualListOfItems}: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.multitabcheckboxselection/com.example.multitabcheckboxselection.BLD_IndividualListOfItems_Starters}: java.lang.ClassCastException: android.app.Application
01-03 12:48:40.138: E/AndroidRuntime(267): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
01-03 12:48:40.138: E/AndroidRuntime(267): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
01-03 12:48:40.138: E/AndroidRuntime(267): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
01-03 12:48:40.138: E/AndroidRuntime(267): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
01-03 12:48:40.138: E/AndroidRuntime(267): at android.os.Handler.dispatchMessage(Handler.java:99)
01-03 12:48:40.138: E/AndroidRuntime(267): at android.os.Looper.loop(Looper.java:123)
01-03 12:48:40.138: E/AndroidRuntime(267): at android.app.ActivityThread.main(ActivityThread.java:4627)
01-03 12:48:40.138: E/AndroidRuntime(267): at java.lang.reflect.Method.invokeNative(Native Method)
01-03 12:48:40.138: E/AndroidRuntime(267): at java.lang.reflect.Method.invoke(Method.java:521)
01-03 12:48:40.138: E/AndroidRuntime(267): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
01-03 12:48:40.138: E/AndroidRuntime(267): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
01-03 12:48:40.138: E/AndroidRuntime(267): at dalvik.system.NativeStart.main(Native Method)
01-03 12:48:40.138: E/AndroidRuntime(267): Caused by: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.multitabcheckboxselection/com.example.multitabcheckboxselection.BLD_IndividualListOfItems_Starters}: java.lang.ClassCastException: android.app.Application
01-03 12:48:40.138: E/AndroidRuntime(267): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
01-03 12:48:40.138: E/AndroidRuntime(267): at android.app.ActivityThread.startActivityNow(ActivityThread.java:2503)
01-03 12:48:40.138: E/AndroidRuntime(267): at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:127)
01-03 12:48:40.138: E/AndroidRuntime(267): at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:339)
01-03 12:48:40.138: E/AndroidRuntime(267): at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:651)
01-03 12:48:40.138: E/AndroidRuntime(267): at android.widget.TabHost.setCurrentTab(TabHost.java:323)
01-03 12:48:40.138: E/AndroidRuntime(267): at android.widget.TabHost.addTab(TabHost.java:213)
01-03 12:48:40.138: E/AndroidRuntime(267): at com.example.multitabcheckboxselection.BreakfastLunchDinnerIndividualListOfItems.onCreate(BreakfastLunchDinnerIndividualListOfItems.java:36)
01-03 12:48:40.138: E/AndroidRuntime(267): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
01-03 12:48:40.138: E/AndroidRuntime(267): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
01-03 12:48:40.138: E/AndroidRuntime(267): ... 11 more
01-03 12:48:40.138: E/AndroidRuntime(267): Caused by: java.lang.ClassCastException: android.app.Application
01-03 12:48:40.138: E/AndroidRuntime(267): at com.example.multitabcheckboxselection.BLD_IndividualListOfItems_Starters.onCreate(BLD_IndividualListOfItems_Starters.java:43)
01-03 12:48:40.138: E/AndroidRuntime(267): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
01-03 12:48:40.138: E/AndroidRuntime(267): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
01-03 12:48:40.138: E/AndroidRuntime(267): ... 20 more
01-03 12:53:40.568: I/Process(267): Sending signal. PID: 267 SIG: 9
答案 0 :(得分:1)
ArrayAdapter<String> adapter=new ArrayAdapter<String>(ResultActivity.this,R.layout.custom_single_list,R.id.textView1, arraylist)
您的ArrayList是ArrayList<HashMap<String, String>> arraylist
。它不是ArrayList<String>
。但你有new ArrayAdapter<String>
。
http://developer.android.com/reference/android/widget/ArrayAdapter.html
您有自定义适配器。所以我想你需要将自定义适配器设置为listview。
您也可以使用自定义适配器。
编辑:
String[] from = { "key" }; // map key
int[] to = { R.id.textView1 };// id of textview
SimpleAdapter adapter= new SimpleAdapter(ActivityName.this,arraylist ,R.layout.custom_single_list, from, to);
〔实施例:
public class MainActivity extends ListActivity
{
ListView lstView;
ArrayList<HashMap<String,String>> arraylist = new ArrayList<HashMap<String,String>>();
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
lstView = getListView();
lstView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
for(int i=0;i<10;i++)
{
HashMap<String,String> map = new HashMap<String,String>();
map.put("key", "value"+i);
arraylist.add(map);
}
String[] from = { "key" };
int[] to = { R.id.textView1 };
SimpleAdapter adapter= new SimpleAdapter(this, arraylist,R.layout.row, from, to);
setListAdapter(adapter);
}
}
我有一个TextView
的row.xml,标识为textView1
。您可以根据需要获得更多文本视图并显示数据。
对齐