可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I am making an android application that needs to use a ListView
. I want to add a menubutton that says "Add to list" and once the user presses that menubutton, it pops up a popupwindow containing a TextView
, EditText
and two Buttons
, "Ok" and "Cancel". Once the user presses "Ok", the text inside the EditText
should be added to the ListView
. And the cancel Button
is obvious. I also want to be able to long press on a ListView
item to open a popupwindow containing a delete Button
. I want to design the ListView
screen using XML. How can i make this possible??? Please help me and thanks SO much in advance! I am using this code so far:
ListView activity:
public class NotesActivity extends ListActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
Main screen XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/background"
android:orientation="vertical" >
<ListView
android:id="@+id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
回答1:
You have to create a layout containing a listView.
You have to create an xml lyout corresponding to one row of your list view.
You have to create an adapter which will populate data to insert in your listView
You have to create an onClickListener on your button to add data in your list
If you are using an ArrayAdapter or a CursorAdapter, add the new
item you created to the list or the cursor used by your adapter and
(notifyDataSetChanged()
is automatically called), so your adapter will update the
listview
Source :
http://developer.android.com/resources/tutorials/views/hello-listview.html
Previous topic on it : Dynamic ListView in Android app
回答2:
more details:- http://www.listviewinandroid.blogspot.in/
public class SimpleListViewActivity extends Activity {
private ListView mainListView ;
private ArrayAdapter<String> listAdapter ;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Find the ListView resource.
mainListView = (ListView) findViewById( R.id.mainListView );
//
// Create and populate a List of planet names.
String[] planets = new String[] { "Mercury", "Venus", "Earth", "Mars",
"Jupiter", "Saturn", "Uranus", "Neptune"};
ArrayList<String> planetList = new ArrayList<String>();
planetList.addAll( Arrays.asList(planets) );
// Create ArrayAdapter using the planet list.
listAdapter = new ArrayAdapter<String>(this, R.layout.simplerow, planetList);
// Add more planets. If you passed a String[] instead of a List<String>
// into the ArrayAdapter constructor, you must not add more items.
// Otherwise an exception will occur.
listAdapter.add( "Ceres" );
listAdapter.add( "Pluto" );
listAdapter.add( "Haumea" );
listAdapter.add( "Makemake" );
listAdapter.add( "Eris" );
// Set the ArrayAdapter as the ListView's adapter.
mainListView.setAdapter( listAdapter );
mainListView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int pos,
long arg3) {
//Log.i("m", "-"+pos);
Intent myIntent = new Intent(SimpleListViewActivity.this, MainActivity.class);
myIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
myIntent.putExtra("pos", pos);
startActivity(myIntent);
}
});
}
}
MainActivity.java
package com.windrealm.android;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
public class MainActivity extends Activity {
//TextView tv;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mainactivity);
TextView tv = (TextView) findViewById(R.id.textView1);
Bundle b = getIntent().getExtras();
int pos = b.getInt("pos");
Log.i("pos=", "-"+pos);
if(pos==0)
{
tv.setText("MERCURY \n Aphelion \n69,816,900 km\n0.466 697 AU\nPerihelion\n 46,001,200 km\n");
}
if(pos==2)
{
tv.setText("earth");
}
}
}
回答3:
//Adapter
package com.example.androidlistview;
import java.util.ArrayList;
import android.app.Activity;
import android.content.Context;
import android.os.AsyncTask;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;
import com.squareup.picasso.Picasso;
public class CustomAdapter extends BaseAdapter{
ProgressBar pro;
private Activity activity;
private ArrayList<ListGetterSetter> Arr_List;
private LayoutInflater inflate;
public CustomAdapter(Activity a,ArrayList<ListGetterSetter> arr)
{
activity = a;
Arr_List = arr;
inflate = (LayoutInflater)a.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return Arr_List.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return Arr_List.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ViewHolder holder = null;
String url = null;
Log.i("getView","called");
if(convertView==null){
holder = new ViewHolder();
url ="ImagePath";
convertView = inflate.inflate(R.layout.tbrow, null);
holder.txtname = (TextView)convertView.findViewById(R.id.txt_name);
holder.txtdist = (TextView)convertView.findViewById(R.id.txt_dist);
holder.txtaddress = (TextView)convertView.findViewById(R.id.iRestaddress);
holder.image = (ImageView)convertView.findViewById(R.id.img_listimage);
holder.txtname.setText(Arr_List.get(position).getName());
holder.txtdist.setText(Arr_List.get(position).getD_dist());
holder.txtaddress.setText(Arr_List.get(position).getvAddress2());
Log.i("URLLRLRLRRL",""+url);
Picasso.with(activity).load(url).placeholder(R.drawable.listimg).noFade().into(holder.image);
convertView.setTag(holder);
}
else
{
holder = (ViewHolder) convertView.getTag();
}
return convertView;
}
public static class ViewHolder {
public TextView txtname,txtaddress,txtdist;
public ImageView image;
}
}
//Call Main Activity
package com.example.androidlistview;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Parcelable;
import android.util.Log;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.LinearLayout;
import android.widget.ListView;
public class MainActivity extends Activity implements OnScrollListener{
int currentFirstVisibleItem,currentVisibleItemCount,currentScrollState,total;
int selection=0;
ProgressDialog pd_load;
ListView lst_list;
String MSG;
LinearLayout ll;
ArrayList<ListGetterSetter> ListArray;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListArray = new ArrayList<ListGetterSetter>();
new GetList().execute();
lst_list = (ListView)findViewById(R.id.lst_list);
lst_list.setOnScrollListener(this);
}
public class GetList extends AsyncTask<Void, Void, Void>
{
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
// pd_load = new ProgressDialog(MainActivity.this);
// pd_load.setMessage("Loading...");
// pd_load.show();
ll = (LinearLayout)findViewById(R.id.ll_load);
ll.setVisibility(View.VISIBLE);
}
@Override
protected Void doInBackground(Void... params) {
// TODO Auto-generated method stub
getdata();
return null;
}
@Override
protected void onPostExecute(Void result) {
// TODO Auto-generated method stub
// if(pd_load.isShowing())
// {
// pd_load.dismiss();
// }
ll.setVisibility(View.GONE);
CustomAdapter cst = new CustomAdapter(MainActivity.this, ListArray);
lst_list.setAdapter(cst);
lst_list.setSelection(selection);
super.onPostExecute(result);
}
public void getdata()
{
try
{
URL url = new URL("url");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.connect();
int code = connection.getResponseCode();
if (code != 200) {
MSG = "Internet connection not available";
//setContentView(R.layout.webimg);
} else {
BufferedReader dis = new BufferedReader(
new InputStreamReader(connection.getInputStream()));
String myString, Result = dis.readLine();
while ((myString = dis.readLine()) != null) {
Result += myString;
}
JSONObject jsonObject = new JSONObject(Result);
JSONArray jsarr = jsonObject.getJSONArray("posts");
int arraysize = jsarr.length();
for(int i=0;i<arraysize;i++)
{
ListGetterSetter ls = new ListGetterSetter();
JSONObject jsobj = jsarr.getJSONObject(i);
ls.setName(jsobj.getJSONObject("post").getString("vName"));
ls.setvAddress1(jsobj.getJSONObject("post").getString("vAddress1"));
ls.setD_dist(jsobj.getJSONObject("post").getString("d_dist"));
ls.setvAddress2(jsobj.getJSONObject("post").getString("vAddress2"));
ls.setImagepath(jsobj.getJSONObject("post").getString("vImage"));
ListArray.add(ls);
}
}
} catch (MalformedURLException e1) {
// TODO Auto-generated catch block
MSG = "MalformedURLException";
} catch (IOException e) {
// TODO Auto-generated catch block
MSG = "IOException";
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
Log.i("Scroll End","Call1222");
this.currentFirstVisibleItem = firstVisibleItem;
this.currentVisibleItemCount = visibleItemCount;
this.total = totalItemCount;
// TODO Auto-generated method stub
Log.i("Scroll End","Call1"+currentVisibleItemCount);
}
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
// TODO Auto-generated method stub
this.currentScrollState = scrollState;
Log.i("Scroll End","Call2");
this.isScrollCompleted();
}
public void isScrollCompleted()
{
final int lastItem = currentFirstVisibleItem + currentVisibleItemCount;
if(lastItem == total) {
// Last item is fully visible.
selection=total;
new GetList().execute();
}
if (this.currentVisibleItemCount ==ListArray.size() && this.currentScrollState == SCROLL_STATE_IDLE) {
/*** In this way I detect if there's been a scroll which has completed ***/
/*** do the work for load more date! ***/
Log.i("Scroll End","Call");
}
}
}
//List Row
<?xml version="1.0" encoding="utf-8"?>
<TableRow xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tr_main"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="115dp"
android:layout_weight="1"
android:background="@drawable/listingbg"
android:orientation="horizontal"
android:paddingBottom="5dp"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:paddingTop="5dp" >
<FrameLayout
android:id="@+id/img_image"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
>
<ImageView android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/img_listimage"
android:src="@drawable/listimg"/>
<ProgressBar
android:id="@+id/progressBar1"
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"/>
</FrameLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/img_image"
android:layout_marginLeft="8dp"
android:layout_toLeftOf="@+id/relativeLayout1"
android:gravity="center_vertical|center_horizontal"
android:minWidth="100dp" >
<TextView
android:id="@+id/txt_dist"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:text="5 km"
android:textColor="#000000"
android:textSize="14sp"
android:textStyle="bold" />
</LinearLayout>
<RelativeLayout
android:id="@+id/relativeLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginRight="30dp"
android:layout_toRightOf="@+id/img_image"
android:paddingLeft="5dp" >
<TextView
android:id="@+id/txt_name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:layout_toLeftOf="@+id/iv_fav_tbrow"
android:ellipsize="end"
android:gravity="left"
android:singleLine="true"
android:text="Restaurant namefbfbfbfbfdbfbfb"
android:textColor="#2e1317"
android:textSize="19dp"
android:textStyle="bold" />
<TextView
android:id="@+id/iRestaddress"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/txt_name"
android:layout_marginLeft="5dp"
android:ellipsize="end"
android:lines="2"
android:maxEms="5"
android:maxWidth="170dp"
android:text="202, asharam road. Ahmedabad - 3000001"
android:textColor="#2e1317"
android:textSize="14dp" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/iRestaddress"
android:layout_marginLeft="7dp"
android:layout_marginTop="5dp"
android:paddingBottom="1dp" >
<ImageView
android:id="@+id/iv_rating1"
android:layout_width="25dp"
android:layout_height="25dp"
android:src="@drawable/rat_star_off" />
<LinearLayout
android:layout_width="6dp"
android:layout_height="wrap_content" >
</LinearLayout>
<ImageView
android:id="@+id/iv_rating2"
android:layout_width="25dp"
android:layout_height="25dp"
android:src="@drawable/rat_star_off" />
<LinearLayout
android:layout_width="6dp"
android:layout_height="wrap_content" >
</LinearLayout>
<ImageView
android:id="@+id/iv_rating3"
android:layout_width="25dp"
android:layout_height="25dp"
android:src="@drawable/rat_star_off" />
<LinearLayout
android:layout_width="6dp"
android:layout_height="wrap_content" >
</LinearLayout>
<ImageView
android:id="@+id/iv_rating4"
android:layout_width="25dp"
android:layout_height="25dp"
android:src="@drawable/rat_star_off" />
<LinearLayout
android:layout_width="6dp"
android:layout_height="wrap_content" >
</LinearLayout>
<ImageView
android:id="@+id/iv_rating5"
android:layout_width="25dp"
android:layout_height="25dp"
android:src="@drawable/rat_star_off" />
<LinearLayout
android:layout_width="6dp"
android:layout_height="wrap_content" >
</LinearLayout>
</LinearLayout>
<ImageView
android:id="@+id/iv_fav_tbrow"
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_above="@+id/iRestaddress"
android:layout_alignParentRight="true"
android:layout_marginRight="5dp"
android:background="@drawable/ic_favorite"
android:visibility="gone" />
</RelativeLayout>
</RelativeLayout>
</TableRow>
//main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="328dp"
android:layout_weight="0.79"
android:orientation="vertical" >
<ListView
android:id="@+id/lst_list"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</ListView>
</LinearLayout>
<LinearLayout
android:id="@+id/ll_load"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:gravity="center"
android:orientation="horizontal"
android:visibility="gone"
android:background="@drawable/listingbg"
>
<ProgressBar
android:id="@+id/progressBar1"
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Loading..."
android:textSize="16dp"
android:textColor="@android:color/white"
android:textStyle="bold"/>
</LinearLayout>
</LinearLayout>
回答4:
Sample List view:
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import com.example.beans.SongInfo;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
public class BuddiesActivity extends Activity {
int statusClass = 2;
String taskResult="test";
ListView list;
LazyAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
new HttpAsyncTask().execute("http://en.wikipedia.org/w/api.php?format=json&action=query&titles=Main%20Page&prop=revisions&rvprop=content");
do {
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
} while (statusClass == 2);
setContentView(R.layout.activity_buddies);
// TextView textview=(TextView)findViewById(R.id.buddies);
// textview.setText(taskResult);
ArrayList<SongInfo> songsList1 = new ArrayList<SongInfo>();
System.out.println("Emply ArrayList Created");
songsList1=getListData();
System.out.println("Data added to arrayList :"+songsList1.size()+" Data is: "+songsList1);
list=(ListView)findViewById(R.id.list);
System.out.println("ListView Got");
// Getting adapter by passing xml data ArrayList
adapter=new LazyAdapter(this, songsList1);
System.out.println("Object Of Lazy Adapter created");
list.setAdapter(adapter);
System.out.println("adapter set..");
// Click event for single list row
/*list.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
initiatePopupWindow();
}
});*/
}
public ArrayList<SongInfo> getListData(){
ArrayList<SongInfo> list=new ArrayList<SongInfo>();
list.add(new SongInfo("Song", "1", "Someone Like You", "Adele", "4:47", "http://api.androidhive.info/music/images/adele.png"));
list.add(new SongInfo("Song", "2", "Space Bound", "Eminem", "4:34", "http://api.androidhive.info/music/images/adele.png"));
list.add(new SongInfo("Song", "3", "Stranger In Moscow", "Michael Jackson", "5:55", "http://api.androidhive.info/music/images/adele.png"));
list.add(new SongInfo("Song", "4", "Love The Way You Lie", "Rihanna", "4:23", "http://api.androidhive.info/music/images/adele.png"));
list.add(new SongInfo("Song", "5", "Someone Like You", "Adele", "4:47", "http://api.androidhive.info/music/images/adele.png"));
list.add(new SongInfo("Song", "6", "Space Bound", "Eminem", "4:34", "http://api.androidhive.info/music/images/adele.png"));
list.add(new SongInfo("Song", "7", "Stranger In Moscow", "Michael Jackson", "5:55", "http://api.androidhive.info/music/images/adele.png"));
list.add(new SongInfo("Song", "8", "Love The Way You Lie", "Rihanna", "4:23", "http://api.androidhive.info/music/images/adele.png"));
list.add(new SongInfo("Song", "9", "Someone Like You", "Adele", "4:47", "http://api.androidhive.info/music/images/adele.png"));
list.add(new SongInfo("Song", "10", "Space Bound", "Eminem", "4:34", "http://api.androidhive.info/music/images/adele.png"));
list.add(new SongInfo("Song", "11", "Stranger In Moscow", "Michael Jackson", "5:55", "http://api.androidhive.info/music/images/adele.png"));
list.add(new SongInfo("Song", "12", "Love The Way You Lie", "Rihanna", "4:23", "http://api.androidhive.info/music/images/adele.png"));
return list;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.buddies, menu);
return true;
}
private class HttpAsyncTask extends AsyncTask<String, Void, String> {
AlertDialog.Builder builder;
protected void onPreExecute() {
super.onPreExecute();
builder = new AlertDialog.Builder(BuddiesActivity.this);
}
@Override
protected String doInBackground(String... urls) {
return POST(urls[0]);
}
@Override
protected void onPostExecute(String result) {
}
}
public String POST(String url) {
System.out.println("I am in post data./..............................");
HttpClient httpclient = new DefaultHttpClient();
HttpGet httppost = new HttpGet(url);
String result = null;
try {
HttpResponse response = httpclient.execute(httppost);
int status = response.getStatusLine().getStatusCode();
System.out.println("Status is : " + status);
// ParseProductJson parseJson = new ParseProductJson();
if (status == 200) {
result = EntityUtils.toString(response.getEntity());
System.out.println("################result1###############################"+ result);
/*FileOutputStream fos = openFileOutput("productsJson.json",
Context.MODE_PRIVATE);
fos.write(result.getBytes());
fos.close();*/
System.out.println("Done");
taskResult=result;
statusClass = status;
} else {
statusClass = 400;
result = "Did not work!";
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return result;
}
}
回答5:
Adapter:
import java.util.ArrayList;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.example.beans.SongInfo;
public class LazyAdapter extends BaseAdapter {
private Activity activity;
private ArrayList<SongInfo> data1;
private static LayoutInflater inflater=null;
//public ImageLoader imageLoader;
public LazyAdapter(Activity a, ArrayList<SongInfo> d) {
activity = a;
data1=d;
inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// imageLoader=new ImageLoader(activity.getApplicationContext());
}
public int getCount() {
return data1.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
View vi=convertView;
if(convertView==null)
vi = inflater.inflate(R.layout.list_row, null);
TextView title = (TextView)vi.findViewById(R.id.title); // title
TextView artist = (TextView)vi.findViewById(R.id.artist); // artist name
TextView duration = (TextView)vi.findViewById(R.id.duration); // duration
ImageView thumb_image=(ImageView)vi.findViewById(R.id.list_image); // thumb image
SongInfo song=data1.get(position);
// Setting all values in listview
title.setText(song.getTitle());
artist.setText(song.getArtist());
duration.setText(song.getDuration());
if(song.getArtist().equalsIgnoreCase("Adele")){
thumb_image.setImageResource(R.drawable.rihanna);
}else if(song.getArtist().equalsIgnoreCase("Rihanna")){
thumb_image.setImageResource(R.drawable.rihanna);
}else if(song.getArtist().equalsIgnoreCase("Eminem")){
thumb_image.setImageResource(R.drawable.rihanna);
}else if(song.getArtist().equalsIgnoreCase("Michael Jackson")){
thumb_image.setImageResource(R.drawable.rihanna);
}
// imageLoader.DisplayImage(song.get(CustomizedListView.KEY_THUMB_URL), thumb_image);
return vi;
}
}
回答6:
Try with this way
1 Mainlayout
<ListView
android:id="@+id/listviews"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></ListView>
2 listitemlayout
<TextView
android:id="@+id/tviname"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/tvinum"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
3 MainActivity
ListView listviews;
ArrayList<String> listname=new ArrayList<>();
ArrayList<String> listnum=new ArrayList<>();
listviews=(ListView)findViewById(R.id.listviews);
listname.add("amit");
listnum.add("12334444444");
listname.add("amit");
listnum.add("12334444444");
listviews.setAdapter(new Myadapter(MainActivity.this,listname,listnum));
4 AdapterClass
public class Myadapter implements ListAdapter {
Activity activity;
ArrayList<String> listname, listnum;
int size;
public Myadapter(Activity activity, ArrayList<String> listname, ArrayList<String> listnum) {
this.listname = listname;
this.listnum = listnum;
size = listname.size();
this.activity = activity;
}
@Override
public View getView(int position, View view, ViewGroup viewGroup) {
LayoutInflater inflater = (LayoutInflater) activity
.getSystemService(activity.LAYOUT_INFLATER_SERVICE);
View v = inflater.inflate(R.layout.mylayout, null);
TextView tviname = (TextView) v.findViewById(R.id.tviname);
TextView tvinum = (TextView) v.findViewById(R.id.tvinum);
tviname.setText(listname.get(position));
tvinum.setText(listnum.get(position));
return v;
}
@Override
public void registerDataSetObserver(DataSetObserver observer) {
// TODO Auto-generated method stub
}
@Override
public void unregisterDataSetObserver(DataSetObserver observer) {
// TODO Auto-generated method stub
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return size;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
@Override
public boolean hasStableIds() {
// TODO Auto-generated method stub
return false;
}
@Override
public int getItemViewType(int position) {
// TODO Auto-generated method stub
return 0;
}
@Override
public int getViewTypeCount() {
// TODO Auto-generated method stub
return size;
}
@Override
public boolean isEmpty() {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean areAllItemsEnabled() {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean isEnabled(int position) {
// TODO Auto-generated method stub
return false;
}
}