我是Android的新手。 我的要求是,在ViewAll活动中,当我单击特定的行/单元格时,它必须从Tablelayout获取该单元格值并将其放入另一个活动中以更新这些值。如果我犯了任何错误,请纠正我 我附上了布局&类文件FYR。
ViewAll.xml
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/maintable"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="48dp"
android:layout_marginTop="39dp"
android:shrinkColumns="1">
</TableLayout>
ViewAll.java
package com.bharath.myidentity;
import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.EditText;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.Toast;
public class ViewAll extends Activity implements OnClickListener {
String fname,lname,email;
SQLiteDatabase db;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_all);
db=openOrCreateDatabase("MyDB1",MODE_PRIVATE, null);
try{
Bundle extras = getIntent().getExtras();
String UserID ="";
if(extras !=null)
{
db=openOrCreateDatabase("MyIdentity",MODE_PRIVATE, null);
UserID = extras.getString("Id");
String UserName = extras.getString("UserName");
//Toast.makeText(getApplicationContext(),UserID+UserName,
Toast.LENGTH_LONG).show();
// TableLayout tableLayout = new TableLayout(getApplicationContext());
TableLayout tableLayout =(TableLayout)findViewById(R.id.maintable);
tableLayout.setVerticalScrollBarEnabled(true);
//tableLayout.setShrinkAllColumns(true);
TableRow tableRow;
TextView textHeading,textView,textView1,textView2,textView3,textView4,textView5;
tableRow = new TableRow(getApplicationContext());
tableRow.setClickable(true);
//tableRow.setOnClickListener(this);
//Heading - Starts Here
textHeading=new TextView(getApplicationContext());
textHeading.setGravity(Gravity.CENTER);
//textHeading.setClickable(true);
textHeading.setText("Hi, "+UserName);
textHeading.setTextColor(Color.BLUE);
textHeading.setTypeface(null, Typeface.BOLD);
textHeading.setPadding(20, 20, 20, 20);
tableRow.addView(textHeading);
tableLayout.addView(tableRow);
//Heading - Ends Here
Cursor c=db.rawQuery("SELECT * from identities where userid='"+UserID+"'", null);
int count= c.getCount();
c.moveToFirst();
tableRow = new TableRow(getApplicationContext());
tableRow.setClickable(true);
textView=new TextView(getApplicationContext());
//textView.setClickable(true);
textView.setText("Sl.");
textView.setTextColor(Color.RED);
textView.setTypeface(null, Typeface.BOLD);
textView.setPadding(20, 20, 20, 20);
tableRow.addView(textView);
textView4=new TextView(getApplicationContext());
//textView4.setClickable(true);
textView4.setText("Title");
textView4.setTextColor(Color.RED);
textView4.setTypeface(null, Typeface.BOLD);
textView4.setPadding(20, 20, 20, 20);
tableRow.addView(textView4);
textView5=new TextView(getApplicationContext());
//textView5.setClickable(true);
textView5.setText("Descriptions");
textView5.setTextColor(Color.RED);
textView5.setTypeface(null, Typeface.BOLD);
textView5.setPadding(20, 20, 20, 20);
tableRow.addView(textView5);
tableLayout.addView(tableRow);
for (Integer j = 0; j < count; j++)
{
tableRow = new TableRow(getApplicationContext());
tableRow.setClickable(true);
textView1 = new TextView(getApplicationContext());
textView1.setText(c.getString(c.getColumnIndex("id")));
textView2 = new TextView(getApplicationContext());
textView2.setText(c.getString(c.getColumnIndex("title")));
//textView2.setClickable(true);
textView3 = new TextView(getApplicationContext());
//textView3.setClickable(true);
textView3.setText(c.getString(c.getColumnIndex("description")));
textView1.setPadding(20, 20, 20, 20);
textView2.setPadding(20, 20, 20, 20);
textView3.setPadding(20, 20, 20, 20);
tableRow.addView(textView1);
tableRow.addView(textView2);
tableRow.addView(textView3);
tableLayout.addView(tableRow);
c.moveToNext() ;
}
setContentView(tableLayout);
db.close();
Toast.makeText(getApplicationContext(),"Loaded Completely...", Toast.LENGTH_LONG).show();
}
} catch (Exception e) {
Toast.makeText(getApplicationContext(),e.getMessage(), Toast.LENGTH_LONG).show();
}
final TableLayout contact_table = (TableLayout)findViewById(R.id.maintable);
final View row=contact_table.getChildAt(2);
row.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
final TableLayout contact_table = (TableLayout)findViewById(R.id.maintable);
final View row=contact_table.getChildAt(2);
int row_id;
row_id=0;
row_id=contact_table.indexOfChild(row);
Toast.makeText(getApplicationContext(),row_id + " Row ID", Toast.LENGTH_LONG).show();
}
}
的welcome.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_centerInParent="true"
android:layout_marginTop="18dp"
android:text="@string/Welcome" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView3"
android:layout_below="@+id/textView1"
android:layout_marginTop="46dp"
android:text="@string/id" />
<EditText
android:id="@+id/editTextId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/textView2"
android:layout_alignBottom="@+id/textView2"
android:layout_alignLeft="@+id/textView1"
android:ems="10"
android:inputType="text" >
<requestFocus />
</EditText>
<Button
android:id="@+id/btnSave"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignRight="@+id/textView3"
android:layout_marginBottom="64dp"
android:onClick="Insert"
android:text="@string/save" />
<Button
android:id="@+id/btnViewAll"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/btnSave"
android:layout_alignBottom="@+id/btnSave"
android:layout_alignLeft="@+id/editTextDescr"
android:layout_marginLeft="26dp"
android:onClick="onClick"
android:text="@string/viewall" />
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView2"
android:layout_below="@+id/editTextId"
android:layout_marginTop="69dp"
android:text="@string/title" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/editTextDescr"
android:layout_alignBottom="@+id/editTextDescr"
android:layout_marginRight="14dp"
android:layout_toLeftOf="@+id/editTextDescr"
android:text="@string/descr" />
<EditText
android:id="@+id/editTextDescr"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="@+id/editTextTitle"
android:layout_marginTop="64dp"
android:ems="10"
android:inputType="textMultiLine"
android:onClick="Insert" />
<EditText
android:id="@+id/editTextTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/textView4"
android:layout_alignBottom="@+id/textView4"
android:layout_toRightOf="@+id/textView3"
android:ems="10"
android:inputType="text" />
</RelativeLayout>
Welcome.java
package com.bharath.myidentity;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.content.res.Resources;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class Welcome extends Activity {
// This is the Screen for - Entering Identities & "I WOULD LIKE TO GET VALUES HERE FROM ViewALL ACTIVITY"
String Title="",Descr="";
SQLiteDatabase db;
String UserID ="";
String UserName ="";
public Welcome() {
}
@Override
protected void onCreate(Bundle savedInstanceState) {
try {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_welcome);
Bundle extras = getIntent().getExtras();
if(extras !=null)
{
UserID = extras.getString("Id");
UserName = extras.getString("UserName");
Resources res = getResources();
String text = String.format(res.getString(R.string.Welcome), UserName);
TextView TextUserName = (TextView) findViewById(R.id.textView1);
TextUserName.setText(text);
if (UserID.equals(""))
{
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
}
else
{
db=openOrCreateDatabase("MyIdentity",MODE_PRIVATE, null);
//db.execSQL("DROP TABLE login");
db.execSQL("CREATE TABLE IF NOT EXISTS identities(id INTEGER PRIMARY KEY
AUTOINCREMENT,userid VARCHAR,title VARCHAR,description VARCHAR, FOREIGN KEY(userid) REFERENCES login(id));");
db.close();
}
}
}catch (Exception e) {
Toast.makeText(getApplicationContext(),e.getMessage(), Toast.LENGTH_LONG).show();
}
}
public void Insert(View view)
{
try {
EditText edittextTitle=(EditText)findViewById(R.id.editTextTitle);
EditText edittextDescr=(EditText)findViewById(R.id.editTextDescr);
Title=edittextTitle.getText().toString();
Descr=edittextDescr.getText().toString();
if (Title.trim().equals("")){
Toast.makeText(getApplicationContext(), "Please Enter UserName...",
Toast.LENGTH_LONG).show();
edittextTitle.setFocusable(true);
//here, return is like a break & it will skip the TRY block...
return;
}
if (Descr.trim().equals("")){
Toast.makeText(getApplicationContext(), "Please Enter Password...",
Toast.LENGTH_LONG).show();
edittextDescr.setFocusable(true);
return;
}
if ((!Title.trim().equals("")) && (!Descr.trim().equals("")))
{
db=openOrCreateDatabase("MyIdentity",MODE_PRIVATE, null);
//System.out.print(UserName);
Bundle extras = getIntent().getExtras();
String UserID ="";
if(extras !=null)
{
UserID = extras.getString("Id");
Cursor c=db.rawQuery("SELECT * from identities where userid='"+UserID+"' and
title='"+Title+"'", null);
if (c.getCount()==0)
{
db.execSQL("INSERT INTO identities(userid,title,description)
VALUES('"+UserID+"','"+Title+"','"+Descr+"');");
Toast.makeText(getApplicationContext(), "Identity Saved Successfully...",
Toast.LENGTH_LONG).show();
}
else
{
Toast.makeText(getApplicationContext(), "Identity Already Exist for this
User...", Toast.LENGTH_LONG).show();
}
}
else
{
Toast.makeText(getApplicationContext(), "Identity Already Exist for this User...",
Toast.LENGTH_LONG).show();
}
db.close();
}
} catch (Exception e) {
Toast.makeText(getApplicationContext(),e.getMessage(), Toast.LENGTH_LONG).show();
}
}
public void onClick(View v)
{
Intent intent = new Intent(this, ViewAll.class);
intent.putExtra("Id", UserID);
intent.putExtra("UserName", UserName);
startActivity(intent);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.my_identity, menu);
return true;
}
}
如果需要任何细节请问我会提供。
在此先感谢您的帮助。