Reputation: 1
I spent quite some time trying to apply what I learn but whenever I run this example on my phone, it just crashes.
What i was trying to make is just a ImageView with 2 textview on the right just like how the contacts look on the phone.
Here's my code:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Resources res = getResources();
names = res.getStringArray(R.array.contacts);
info = res.getStringArray(R.array.descript);
int [] images = {R.drawable.pic0, R.drawable.pic1, R.drawable.pic2, R.drawable.pic3, R.drawable.pic4};
list = (ListView) findViewById(R.id.listView);
myAdapter adapter = new myAdapter(this,names,images,info);
list.setAdapter(adapter);
}
class myAdapter extends ArrayAdapter<String> {
Context context;
int[] pictures;
String[] conArray;
String[] infoArray;
myAdapter(Context c,String[] contacts, int images[],String[] descript){
super(c,R.layout.single_row,R.id.textView,contacts);
this.context = c;
this.pictures=images;
this.conArray=contacts;
this.infoArray=descript;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(LAYOUT_INFLATER_SERVICE);
View v =inflater.inflate(R.layout.single_row,parent,false);
ImageView image = (ImageView) findViewById(R.id.imageView);
TextView name = (TextView) findViewById(R.id.textView);
TextView des = (TextView) findViewById(R.id.textView2);
image.setImageResource(pictures[position]);
name.setText(conArray[position]);
des.setText(infoArray[position]);
return v;
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, 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();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Upvotes: 0
Views: 82
Reputation: 117
Check the array length are equal
Resources res = getResources();
names = res.getStringArray(R.array.contacts);
info = res.getStringArray(R.array.descript);
int [] images = {R.drawable.pic0, R.drawable.pic1, R.drawable.pic2,R.drawable.pic3, R.drawable.pic4};
contact string array and names String array should be equal to int[] images
Upvotes: 0
Reputation: 47807
Change in getView(....)
ImageView image = (ImageView) findViewById(R.id.imageView);
TextView name = (TextView) findViewById(R.id.textView);
TextView des = (TextView) findViewById(R.id.textView2);
to
ImageView image = (ImageView) v.findViewById(R.id.imageView);
TextView name = (TextView) v.findViewById(R.id.textView);
TextView des = (TextView) v.findViewById(R.id.textView2);
find a View
from inflated rootview.
and also used View Holder pattern
for smooth scrolling http://www.javacodegeeks.com/2013/09/android-viewholder-pattern-example.html
Upvotes: 2