我的代码应该打开项目的列表视图。单击某个项目时,将打开相应的对话框。列表视图出现但根本没有显示任何对话框。而且我没有在logcat中看到任何错误。请帮忙
import android.app.Dialog;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import com.playmaker.BudgetOh.R;
import com.playmaker.BudgetOh.manager.ExpenseManager;
import com.playmaker.BudgetOh.model.Expense;
import java.util.ArrayList;
public class ViewExpense extends ListActivity {
ExpenseManager expenseManager;
ArrayList<Expense> allExpenses;
ArrayList<String> allTitles;
ListView listView;
String titlePosition;
Dialog dialog;
Expense expense;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
expenseManager = new ExpenseManager(this);
allTitles = new ArrayList<String>();
allExpenses = expenseManager.getAllExpenses();
listView = getListView();
listView.setBackgroundResource(R.color.viewBackground);
listView.setPadding(10, 20, 10, 15);
listView.setFooterDividersEnabled(true);
listView.setHeaderDividersEnabled(true);
// getting all the titles
for (Expense expense : allExpenses) {
allTitles.add(expense.getTitle());
}
listView.setAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, allTitles));
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
titlePosition = allTitles.get(position);
expense = expenseManager.getExpense(titlePosition);
dialog = new Dialog(ViewExpense.this);
dialog.setContentView(R.layout.view_details);
TextView tvTime, tvTitle, tvAmount, tvCategory, tvComment;
tvTitle = (TextView) dialog.findViewById(R.id.tvViewTitle);
tvAmount = (TextView) dialog.findViewById(R.id.tvViewAmount);
tvCategory = (TextView) dialog.findViewById(R.id.tvViewCategory);
tvComment = (TextView) dialog.findViewById(R.id.tvViewComment);
tvTime = (TextView) dialog.findViewById(R.id.tvViewTime);
dialog.setTitle(expense.getTitle());
tvTitle.setText(expense.getTitle());
tvAmount.setText(expense.getAmount());
tvCategory.setText(expense.getCategory());
tvComment.setText(expense.getComment());
tvTime.setText("Created on " + expense.getDate() + " At "
+ expense.getTime());
dialog.show();
}
}
这是我的对话框布局
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
style="@style/Cream_Back"android:layout_width="match_parent"android:layout_height="match_parent"android:alpha="0.5"android:orientation="vertical"><LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#E3CF57"
android:gravity="center"
android:text="Expense Title"
android:textSize="20sp"
android:textStyle="italic|bold" />
<TextView
android:id="@+id/tvViewTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="fill|center"
android:padding="20dp"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#E3CF57"
android:gravity="center"
android:text="Expense Amount"
android:textSize="20sp"
android:textStyle="italic|bold" />
<TextView
android:id="@+id/tvViewAmount"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="fill|center"
android:padding="20dp"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#E3CF57"
android:gravity="center"
android:text="Expense Category"
android:textSize="20sp"
android:textStyle="italic|bold" />
<TextView
android:id="@+id/tvViewCategory"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="fill|center"
android:padding="20dp"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#E3CF57"
android:gravity="center"
android:text="Time Recorded"
android:textSize="20sp"
android:textStyle="italic|bold" />
<TextView
android:id="@+id/tvViewTime"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="fill|center"
android:padding="20dp"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#E3CF57"
android:gravity="center"
android:text="Comments"
android:textSize="20sp"
android:textStyle="italic|bold" />
<TextView
android:id="@+id/tvViewComment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="fill|center"
android:padding="20dp"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
</LinearLayout>
</ScrollView>
答案 0 :(得分:0)
无法发表评论,但我在你的xml中看到的是你没有正确关闭。您需要使用</Scrollview>
代替</LinearLayout>
关闭。还要确保ScrollView只托管一个View,所以只需用RelativeLayout包装LinearLayout-Views。
试一试,看看对话框是否打开。
答案 1 :(得分:0)
Android的 ScrollView 声明(link):
ScrollView是一个FrameLayout,意味着你应该放置一个孩子 它包含要滚动的全部内容;
根据你的.xml,ScrollView中有几个LinearLayouts(多个子元素),因此FrameLayout的规则不接受它们;我看到所有的LinearLayouts都是垂直方向的,所以你可以将一体化聚集在一起;
此外,ScrollView未正确关闭(</ScrollView>
缺失)。
答案 2 :(得分:0)
请检查您是否获得“allTitles”的值。
修改代码的值,下面是代码。它工作正常。
公共类MainActivity扩展了ListActivity {
//ExpenseManager expenseManager;
ArrayList<Expense> allExpenses;
ArrayList<String> allTitles;
ListView listView;
String titlePosition;
Dialog dialog;
Expense expense;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_main);
//expenseManager = new ExpenseManager(this);
allTitles = new ArrayList<String>();
//allExpenses = expenseManager.getAllExpenses();
listView = getListView();
listView.setBackgroundResource(R.color.colorAccent);
listView.setPadding(10, 20, 10, 15);
listView.setFooterDividersEnabled(true);
listView.setHeaderDividersEnabled(true);
// getting all the titles
allTitles.add("ANup");
allTitles.add("Tamal");
allTitles.add("Aditya");
listView.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, allTitles));
}
@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);
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
titlePosition = allTitles.get(position);
//expense = expenseManager.getExpense(titlePosition);
dialog = new Dialog(MainActivity.this);
dialog.setContentView(R.layout.dialog);
TextView tvTime, tvTitle, tvAmount, tvCategory, tvComment;
tvTitle = (TextView) dialog.findViewById(R.id.tvViewTitle);
tvAmount = (TextView) dialog.findViewById(R.id.tvViewAmount);
tvCategory = (TextView) dialog.findViewById(R.id.tvViewCategory);
tvComment = (TextView) dialog.findViewById(R.id.tvViewComment);
tvTime = (TextView) dialog.findViewById(R.id.tvViewTime);
dialog.setTitle("Hello");
tvTitle.setText("Expenase Test");
tvAmount.setText("$ 100");
tvCategory.setText("Travel");
tvComment.setText("Enjoyed!");
tvTime.setText("Created on " + "Apr 18th" + " At "
+ "4:20 PM");
dialog.show();
}
}