我有一个listview,其中的项目来自数据库。我试图将listview项目的图像图标放在xml中。但是,它没有显示icon.i尝试了很多东西,但似乎没有什么可以解决这个问题?
这是我的Listview:
public class TypeMenu extends AppCompatActivity {
private String TAG = TypeMenu.class.getSimpleName();
String bid;
private ProgressDialog pDialog;
private ListView lv;
private static final String TAG_BID = "bid";
// URL to get contacts JSON
private static String url = "http://cloud....com/brtemp/index.php";
ArrayList<HashMap<String, String>> contactList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_type_menu);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
contactList = new ArrayList<>();
lv = (ListView) findViewById(R.id.list);
new GetContacts().execute();
// on seleting single product
// launching Edit Product Screen
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// getting values from selected ListItem
HashMap<String, String> selected = contactList.get(position);
String selectedId= selected.get("id");
Intent in = new Intent(getApplicationContext(), SubMenu.class);
// sending pid to next activity
in.putExtra("id",selectedId);
startActivityForResult(in, 100);
Toast.makeText(getApplicationContext(),"Toast" +selectedId ,Toast.LENGTH_SHORT).show();
}
});
}
/**
* Async task class to get json by making HTTP call
*/
private class GetContacts extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
// Showing progress dialog
pDialog = new ProgressDialog(TypeMenu.this);
pDialog.setMessage("Please wait...");
pDialog.setCancelable(false);
pDialog.show();
// Toast.makeText(getApplicationContext(),"Toast",Toast.LENGTH_LONG).show();
}
@Override
protected Void doInBackground(Void... arg0) {
HttpHandler sh = new HttpHandler();
// Making a request to url and getting response
String jsonStr = sh.makeServiceCall(url);
Log.e(TAG, "Response from url: " + jsonStr);
if (jsonStr != null) {
try {
JSONArray jsonArry = new JSONArray(jsonStr);
for (int i = 0; i < jsonArry.length(); i++)
{
JSONObject c = jsonArry.getJSONObject(i);
String id = c.getString("id");
String type = c.getString("type");
HashMap<String, String> contact = new HashMap<>();
contact.put("id", id);
contact.put("type", type);
contactList.add(contact);
}
} catch (final JSONException e) {
Log.e(TAG, "Json parsing error: " + e.getMessage());
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(),
"Json parsing error: " + e.getMessage(),
Toast.LENGTH_LONG)
.show();
}
});
}
} else {
Log.e(TAG, "Couldn't get json from server.");
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(),
"Couldn't get json from server. Check LogCat for possible errors!",
Toast.LENGTH_LONG)
.show();
}
});
}
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
// Dismiss the progress dialog
if (pDialog.isShowing())
pDialog.dismiss();
/**
* Updating parsed JSON data into ListView
* */
ListAdapter adapter = new SimpleAdapter(
TypeMenu.this, contactList,
R.layout.list_item, new String[]{ "type","id"},
new int[]{
R.id.type,R.id.arrow1});
lv.setAdapter(adapter);
}
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
onBackPressed();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
以上代码如果我放&#34; R.id.Type,R.id.arrow1&#34;然后列表lis没有打开并给我错误。
这是我的Listview.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="wrap_content"
android:orientation="vertical"
>
<!-- Product id (pid) - will be DDEN - used to pass to other activity -->
<TextView
android:id="@+id/id"
android:layout_width="30dp"
android:layout_height="30dp"/>
<TextView
android:id="@+id/type"
android:textSize="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<ImageView
android:id="@+id/arrow1"
android:layout_width="match_parent"
android:layout_height="15dp"
android:layout_gravity="center"
android:layout_weight="3"
android:src="@drawable/dropdown" />
</LinearLayout>
这是我的logcat:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.zeba.broccoli, PID: 1231
android.content.res.Resources$NotFoundException: Resource ID #0x1
at android.content.res.Resources.getValue(Resources.java:2452)
at android.support.v7.widget.AppCompatDrawableManager.loadDrawableFromDelegates(AppCompatDrawableManager.java:330)
at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:195)
at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:188)
at android.support.v7.content.res.AppCompatResources.getDrawable(AppCompatResources.java:100)
at android.support.v7.widget.AppCompatImageHelper.setImageResource(AppCompatImageHelper.java:73)
at android.support.v7.widget.AppCompatImageView.setImageResource(AppCompatImageView.java:81)
at android.widget.SimpleAdapter.setViewImage(SimpleAdapter.java:262)
at android.widget.SimpleAdapter.bindView(SimpleAdapter.java:192)
at android.widget.SimpleAdapter.createViewFromResource(SimpleAdapter.java:126)
at android.widget.SimpleAdapter.getView(SimpleAdapter.java:114)
at android.widget.AbsListView.obtainView(AbsListView.java:2828)
at android.widget.ListView.measureHeightOfChildren(ListView.java:1292)
at android.widget.ListView.onMeasure(ListView.java:1204)
at android.view.View.measure(View.java:18870)
at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:728)
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:464)
at android.view.View.measure(View.java:18870)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5908)
at android.support.design.widget.CoordinatorLayout.onMeasureChild(CoordinatorLayout.java:703)
at android.support.design.widget.HeaderScrollingViewBehavior.onMeasureChild(HeaderScrollingViewBehavior.java:90)
at android.support.design.widget.AppBarLayout$ScrollingViewBehavior.onMeasureChild(AppBarLayout.java:1367)
at android.support.design.widget.CoordinatorLayout.onMeasure(CoordinatorLayout.java:768)
at android.view.View.measure(View.java:18870)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5908)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:436)
at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:139)
at android.view.View.measure(View.java:18870)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5908)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1435)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:721)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:612)
at android.view.View.measure(View.java:18870)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5908)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:436)
at android.view.View.measure(View.java:18870)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5908)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1435)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:721)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:612)
at android.view.View.measure(View.java:18870)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5908)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:436)
at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:3076)
at android.view.View.measure(View.java:18870)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2392)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1416)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1661)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1301)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:7016)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:777)
at android.view.Choreographer.doCallbacks(Choreographer.java:590)
at android.view.Choreographer.doFrame(Choreographer.java:560)
at android.view.Choreographer$FrameDisplayEventRec
I/Process: Sending signal. PID: 1231 SIG: 9
Application terminated.
答案 0 :(得分:1)
@ z.al我了解你的情况,但为了确保你,SimpleAdapter只是帮助将字符串类型值填充到ID指定的文本视图中。
但在您的情况下,您应该实现自定义ArrayAdapter,并且在getView()方法中,您可以将图像图标设置为imageView。
以下是简单的代码片段,可以让您清楚:
private List<TrelloCard> ProcessJobs(IQueryable<IGrouping<CardGrouping, Job>> jobs)
{
ConcurrentBag<TrelloCard> cards = new ConcurrentBag<TrelloCard>();
Parallel.ForEach(jobs.ToList(), (job) =>
{
card = ProcessCards(job); // I would like to run multiple instances of the processing
cards.Add(card); //Once each instance is finshed it adds it to the list
});
return cards.ToList();
}
private TrelloCard ProcessCards(Job job)
{
return new TrelloCard();
}
答案 1 :(得分:0)
问题是SimpleAdapter
对于您的用例来说太简单了。文档解释说它最终会调用setViewImage(ImageView, String)
来设置图像;它没有告诉你的是它期望通过Integer.parseInt()
将String转换为整数(遗憾的是文档在这方面并不是很好)。
您需要通知适配器如何将数据绑定到ListView中的行。您可以通过调用ViewBinder
为setViewBinder()
提供https://github.com/electron/electron-rebuild来最低限度地执行此操作。但是,我的建议是通过扩展BaseAdapter
来编写自定义适配器实现,因为我的经验是人们很快就超出了框架适配器提供的非常简单的用法。
顺便说一下,也许您应该利用这个机会切换到RecyclerView
,因为ListView
已经陈旧而笨重。