我的MainActivity
:
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
ArrayList<Liste> items;
ListView listView;
ArrayAdapter arrayAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
BottomNavigationView bottomNavigationView=(BottomNavigationView) findViewById(R.id.navigation);
bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
Fragment selectedFragment=null;
switch (item.getItemId()){
case R.id.item1:
selectedFragment=ItemOneFragment.newInstance();
fragment1();
break;
case R.id.item2:
selectedFragment=ItemTwoFragment.newInstance();
fragment2();
break;
case R.id.item3:
selectedFragment=ItemThreeFragment.newInstance();
fragment3();
break;
}
FragmentTransaction transaction=getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.frame_layout,selectedFragment);
transaction.commit();
return true;
}
});
FragmentTransaction transaction=getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.frame_layout,ItemOneFragment.newInstance());
transaction.commit();
}
public void fragment1(){
}
public void fragment2(){
}
public void fragment3(){
items=new ArrayList<>();
items.add(new Liste("Hiiii",android.R.drawable.ic_dialog_info));
// items.add(new Liste("Hiiii",R.drawable.));
listView=(ListView) findViewById(R.id.listv);
arrayAdapter=new Adapter(this,items);
listView.setAdapter(arrayAdapter);
}
}
我的ArrayAdapter
:
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.ArrayList;
class Adapter extends ArrayAdapter<Liste> {
Adapter(Context context, ArrayList<Liste> item) {
super(context, R.layout.fragment3,item);
}
@Override
public View getView(int position,View convertView,ViewGroup parent){
LayoutInflater inflater= LayoutInflater.from(getContext());
View customView=inflater.inflate(R.layout.fragment3,parent,false);
String nam= getItem(position).getName();
int resource=getItem(position).getResource();
TextView viewTitel= customView.findViewById(R.id.textView);
ImageView imageView= customView.findViewById(R.id.imageView);
viewTitel.setText(nam);
imageView.setImageResource(resource);
return customView;
}
}
我的Liste
:
class Liste {
private String name;
private int resource;
Liste(String name, int resource){
this.name=name;
this.resource=resource;
}
public String getName(){
return name;
}
public void setName(String name){
this.name=name;
}
public int getResource(){
return resource;
}
public void setResource(int resource){
this.resource=resource;
}
}
我的ListView
xml与ImageView
和TextView
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/linearlayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_alignTop="@+id/imageView"
android:layout_marginLeft="20dp"
android:layout_marginStart="20dp"
android:layout_toEndOf="@+id/imageView"
android:layout_toRightOf="@+id/imageView"
android:textColor="@color/bottom"
android:textColorLink="@color/colorAccent"
android:textSize="30sp" />
<ImageView
android:id="@+id/imageView"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginLeft="27dp"
android:layout_marginStart="27dp"
android:layout_marginTop="24dp" />
</RelativeLayout>
</LinearLayout>
和我的Fragment
ListView
:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/holo_blue_bright"
tools:context="de.kruemelopment.org.lustigewitze.ItemThreeFragment">
<ListView
android:id="@+id/listv"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true" />
</RelativeLayout>
我运行它并在listview.setAdapter(arrayAdapter)
上显示错误;
但当我删除item.add(...)
;它有效,我认为问题是ImageView
,但我现在不知道在哪里以及为什么以及如何解决它。
所以请帮助我。
帮助
答案 0 :(得分:0)
在适配器设置图像中包含以下代码
imageView.setImageResource(ContextCompat.getDrawable(mContext,
resource);
或者您可以使用此代码
imageView.setImageResource(getResources().getDrawable(resource)); // Deprecated Now after API level 22