我已经创建了一个带有自定义视图列表的应用程序。但是现在我想要通过database填充List.List也包含图像。我该怎么办呢。 这是代码:
CustomListViewAndroidExample.java
public class CustomListViewAndroidExample extends ActionBarActivity {
ListView list;
String[] web = {
"HealthCareTeam",
"HealthCareGoals",
"TestResults",
"Medication",
"Exercise",
"Diet",
"FindHospitals",
"Settings"
} ;
Integer[] imageId = {
R.drawable.image0,
R.drawable.image1,
R.drawable.image2,
R.drawable.image3,
R.drawable.image4,
R.drawable.image5,
R.drawable.image6,
R.drawable.image7
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_custom_list_view_android_example);
CustomList adapter = new CustomList(CustomListViewAndroidExample.this, web, imageId);
list=(ListView)findViewById(R.id.list);
list.setAdapter(adapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
switch (position) {
case 0:
Intent HealthTeamList= new Intent (CustomListViewAndroidExample.this , HealthTeamList.class);
startActivity(HealthTeamList);
break;
case 1 :
Intent SimpleListActivity = new Intent (CustomListViewAndroidExample.this , SimpleListActivity.class);
startActivity(SimpleListActivity);
break;
case 2 :
Intent TestResults = new Intent (CustomListViewAndroidExample.this ,TestResults.class);
startActivity(TestResults);
break;
case 3 :
Intent Medications= new Intent (CustomListViewAndroidExample.this ,Medications.class);
startActivity(Medications);
break;
case 4 :
Intent Exercise = new Intent (CustomListViewAndroidExample.this ,Exercise.class);
startActivity(Exercise);
break;
case 5 :
Intent Diet = new Intent (CustomListViewAndroidExample.this , Diet.class);
startActivity(Diet);
break;
case 6 :
Intent FindHospital = new Intent (CustomListViewAndroidExample.this , Settings.class);
startActivity(FindHospital);
break;
case 7 :
Intent hdiwali = new Intent (CustomListViewAndroidExample.this , Settings.class);
startActivity(hdiwali);
break;
};
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater()
.inflate(R.menu.custom_list_view_android_example, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(
R.layout.fragment_custom_list_view_android_example,
container, false);
return rootView;
}
}
}
customList.java
public class CustomList extends ArrayAdapter<String>
{
private final Activity context;
private final String[] web;
private final Integer[] imageId;
public CustomList(Activity context,
String[] web, Integer[] imageId)
{
super(context, R.layout.list_single, web);
this.context = context;
this.web = web;
this.imageId = imageId;
}
@Override
public View getView(int position, View view, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View rowView= inflater.inflate(R.layout.list_single, null, true);
TextView txtTitle = (TextView) rowView.findViewById(R.id.text);
ImageView imageView = (ImageView) rowView.findViewById(R.id.image);
txtTitle.setText(web[position]);
imageView.setImageResource(imageId[position]);
return rowView;
}
}
activity_custom_list_view_android_example.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginRight="3dp"
tools:context=".CustomListViewAndroidExample" >
<ListView
android:id="@+id/list"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"/>
</RelativeLayout>
list_single.xml
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:background="@drawable/bar_bg"
android:paddingTop="0dip" android:layout_gravity="top"
>
<TableRow>
<LinearLayout
android:layout_width="100dp"
android:layout_height="50dp"
android:paddingLeft="10dp"
android:paddingTop="5dp"
android:paddingBottom="5dp"
>
<ImageView
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:layout_gravity="left|top"
android:scaleType="centerCrop"
android:id="@+id/image"
/>
</LinearLayout>
<TableLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingTop="0dip" android:layout_gravity="top"
>
<TableRow>
<TextView
android:id="@+id/text"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_weight="1" android:layout_gravity="left|center_vertical"
android:textSize="16sp"
android:layout_marginLeft="10dip"
android:layout_marginTop="4dip"
android:textColor="#000000"
android:layout_span="1"
/>
</TableRow>
</TableLayout>
</TableRow>
</TableLayout>
ManifestFile.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.customlistview1"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.SEND_SMS" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/Theme.AppCompat" >
<activity
android:name="com.example.customlistview1.CustomListViewAndroidExample"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.customlistview1.Good_mornig"
android:label="@string/title_activity_login" >
</activity>
<activity
android:name="com.example.customlistview1.SimpleListActivity"
android:label="@string/title_activity_simple_list" >
</activity>
<activity
android:name="com.example.customlistview1.HealthTeamList"
android:label="@string/title_activity_health_team_list" >
</activity>
<activity
android:name="com.example.customlistview1.Info"
android:label="@string/title_activity_info" >
</activity>
<activity
android:name="com.example.customlistview1.Call"
android:label="@string/title_activity_call" >
</activity>
<activity
android:name="com.example.customlistview1.Message"
android:label="@string/title_activity_message" >
</activity>
<activity
android:name="com.example.customlistview1.Email1"
android:label="@string/title_activity_email1" >
</activity>
<activity
android:name="com.example.customlistview1.Medications"
android:label="@string/title_activity_medications" >
</activity>
<activity
android:name="com.example.customlistview1.TestResults"
android:label="@string/title_activity_test_results" >
</activity>
<activity
android:name="com.example.customlistview1.Settings"
android:label="@string/title_activity_settings" >
</activity>
<activity android:name="com.example.customlistview1.Exercise" >
</activity>
<activity
android:name="com.example.customlistview1.Diet"
android:label="@string/title_activity_diet" >
</activity>
</application>
</manifest>